Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i use vs2010 create a win32 application , add a dialog resource idd_dialog1 . run the exe ,click the ok button ,then show a message box ,but i can not close messagebox ,it has no reponse to my clicks.

here is my code:
C++
 #include "stdafx.h"
#include "testdlg.h"
 
#define MAX_LOADSTRING 100
 
// 全局变量:
HINSTANCE hInst;                                // 当前实例
TCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名
 
BOOL                InitInstance(HINSTANCE, int);
INT_PTR CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
 
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
 
    MSG msg;
 
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }
 
    // 主消息循环:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!IsDialogMessage(msg.hwnd,&msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
 
    return (int) msg.wParam;
}
 
 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
 
   hInst = hInstance; // 将实例句柄存储在全局变量中
 
   hWnd =CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,WndProc);
 
   if (!hWnd)
   {
      return FALSE;
   }
 
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
 
   return TRUE;
}
 
INT_PTR CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
 
    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);

        switch (wmId)
        {
        case IDOK:
            MessageBox(hWnd,_T("can no close!"),_T("confused!"),MB_OK);
            break;
        case IDCANCEL:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}


What I have tried:

i use isdialogmessage function ,but it has no effect.
Posted
Updated 14-Aug-16 21:45pm
v2
Comments
[no name] 14-Aug-16 14:24pm    
Try using this, MessageBox(NULL,_T("can no close!"),_T("confused!"),MB_OK);

Well, on the surface, there doesn't seem to be a whole lot wrong with your code. However:

1) Read (or re-read) GetMessage function (Windows) - specifically where it says, "avoid code like this" (hint: you have code "like this").
2) Kill off the WM_PAINT case - it doesn't do anything.
3) Add a case for the WM_INITDIALOG message in your Dialog Box handler, and transfer your ShowWindow() and UpdateWindow() calls to it.

Last but not least, handling a GetMessage loop is a tricky thing. The GetMessage() function can return -1 on error - your code doesn't handle that. Also, msg.hwnd can be NULL, which your code also doesn't handle.

It looks like a lot of the code that you've shown comes directly from examples that are provided online by Microsoft - well, that doesn't mean that the code actually works - I'll just leave it at that.

Of course, these are just suggestions. There might be something else going on that I couldn't possibly know about..
 
Share this answer
 
Your sample is creating a modeless dialog which requires different handling from a modal type. See Modeless Dialog Boxes[^] for details.
 
Share this answer
 
Comments
Tacitonitus 15-Aug-16 5:29am    
He's already handling it as a modeless Dialog Box - can you say "DefWindowProc"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900