正文

[WinApi]MessageBox函数不工作的原因2007-11-12 14:39:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/30872.html

分享到:

[WinApi]MessageBox函数不工作的原因

  学过API(windows C) 编程的人不防思考一下下面的MessageBox函数不能显示出对话框的原因 (我的实验平台:window 2000 profession + VC++6.0 )

#include <windows.h>

HINSTANCE ghInstance;
HWND ghMainWnd;
LPCTSTR lpszWndClassName=TEXT("myClass");
LPCTSTR lpszTitle=TEXT("myWindow");

// main window process
LRESULT CALLBACK MainWndProc(
        HWND hwnd,
        UINT uMsg,
        WPARAM wParam,
        LPARAM lParam ){
    PAINTSTRUCT ps;
    HDC hdc;
    switch(uMsg){
        case WM_CREATE:
            return 0;
        case WM_PAINT:
            return 0;
        case WM_CLOSE:
             /*********************  这个对话框无法显示  **********************/
            if(MessageBox(hwnd,TEXT("Close the window ?"),TEXT("close"),
                MB_YESNO|MB_ICONINFORMATION)==IDYES)
             /*****************************************************************/
                DestroyWindow(ghMainWnd);
            else
                return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
            break;
    }
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

// main function
int WINAPI WinMain(
        HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        int nCmdShow ){
    MSG uMsg;
    WNDCLASS wc;
   
    if(hPrevInstance!=NULL)
        return FALSE;
    // fill WNDCLASS
    wc.cbClsExtra=0;
    wc.cbWndExtra=0;
    wc.hbrBackground=GetStockObject(WHITE_BRUSH);
    wc.hCursor=LoadCursor((HINSTANCE)NULL,IDC_ARROW);
    wc.hIcon=LoadIcon((HINSTANCE)NULL,IDI_APPLICATION);
    wc.hInstance=hInstance;
    wc.lpfnWndProc=(WNDPROC)MainWndProc;
    wc.lpszClassName=lpszWndClassName;
    wc.lpszMenuName=lpszWndClassName;
    wc.style=0;
    // register window class
    if(!RegisterClass(&wc))
        return FALSE;
    ghInstance=hInstance;
    ghMainWnd=CreateWindow(lpszWndClassName,lpszTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,
        (HWND)NULL,(HMENU)NULL,
        ghInstance,(LPVOID)NULL);
    if(!ghMainWnd)
        return FALSE;
    ShowWindow(ghMainWnd,nCmdShow);
    UpdateWindow(ghMainWnd);

    /*********  这个不属于主窗口的对话框也无法显示  *****************/
    MessageBox(NULL,TEXT("Close the window ?"),TEXT("close"),
                MB_YESNO|MB_ICONINFORMATION);
    /*****************************************************************/

   
    while(GetMessage(&uMsg,(HWND)NULL,0,0)){
        TranslateMessage(&uMsg);
        DispatchMessage(&uMsg);
    }
    return uMsg.wParam;
}

///////// 下面是可以正确显示的程序

#include <windows.h>

HINSTANCE ghInstance;
HWND ghMainWnd;
LPCTSTR lpszWndClassName=TEXT("myClass");
LPCTSTR lpszTitle=TEXT("myWindow");

// main window process
LRESULT CALLBACK MainWndProc(
        HWND hwnd,
        UINT uMsg,
        WPARAM wParam,
        LPARAM lParam ){
    PAINTSTRUCT ps;
    HDC hdc;
    switch(uMsg){
        case WM_CREATE:
            return 0;
        case WM_PAINT:
            hdc=BeginPaint(hwnd, &ps);
            EndPaint(hwnd,&ps);
            return 0;
        case WM_CLOSE:
   /** 可以工作了 **/
            if(MessageBox(hwnd,TEXT("Close the window ?"),TEXT("close"),
                MB_YESNO|MB_ICONINFORMATION)==IDYES)
                DestroyWindow(ghMainWnd);
            else
                return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
            break;
    }
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

// main function
int WINAPI WinMain(
        HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR lpCmdLine,
        int nCmdShow ){
    MSG uMsg;
    WNDCLASS wc;
   
    if(hPrevInstance!=NULL)
        return FALSE;
    // fill WNDCLASS
    wc.cbClsExtra=0;
    wc.cbWndExtra=0;
    wc.hbrBackground=GetStockObject(WHITE_BRUSH);
    wc.hCursor=LoadCursor((HINSTANCE)NULL,IDC_ARROW);
    wc.hIcon=LoadIcon((HINSTANCE)NULL,IDI_APPLICATION);
    wc.hInstance=hInstance;
    wc.lpfnWndProc=(WNDPROC)MainWndProc;
    wc.lpszClassName=lpszWndClassName;
    wc.lpszMenuName=lpszWndClassName;
    wc.style=0;
    // register window class
    if(!RegisterClass(&wc))
        return FALSE;
    ghInstance=hInstance;
    ghMainWnd=CreateWindow(lpszWndClassName,lpszTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,
        (HWND)NULL,(HMENU)NULL,
        ghInstance,(LPVOID)NULL);
    if(!ghMainWnd)
        return FALSE;
    ShowWindow(ghMainWnd,nCmdShow);
    UpdateWindow(ghMainWnd);
 /** 可以工作了 **/
    MessageBox(NULL,TEXT("Close the window ?"),TEXT("close"),
                MB_YESNO|MB_ICONINFORMATION);
    while(GetMessage(&uMsg,(HWND)NULL,0,0)){
        TranslateMessage(&uMsg);
        DispatchMessage(&uMsg);
    }
    return uMsg.wParam;
}

/*********************************************************************/

原因在这里:

case WM_PAINT:
            return 0;
****************

case WM_PAINT:
            hdc=BeginPaint(hwnd, &ps);
            EndPaint(hwnd,&ps);
            return 0;
****************

拦截了WM_PAINT消息没有做任何事情就直接返回,下面是MSDN上的一段话:

The BeginPaint function automatically sets the clipping region of the device context to exclude any area outside the update region. The update region is set by the InvalidateRect or InvalidateRgn function and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area,If you not use the BeginPaint() function in the WM_PAINT, the message will not remove from message queue !!!

 

 

阅读(3494) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册