VC窗口关闭调用顺序

 

模态对话框:

CMyDialog::OnClose() -> CDialog::OnCancel() -> CDialog::EndDialog(IDCANCEL)-> CDialog::DestroyWindow() -> CDialog::OnDestroy()

CMyDialog::OnOK() ->CDialog::EndDialog(IDOK) -> CDialog::DestroyWindow() -> CDialog::OnDestroy()

 

非模态对话框:

重载自己的OnOK或OnCancel,由于在OnOK和OnCancel只是隐藏窗口,所以需在里面调用CDialog::DestroyWindow。

CXXXDialog::OnClose() 会进入 CXXXDialog::OnCancel(),然后在OnCancel()中添加DestroyWindow()调用;(3000的XRayDialog非模态窗口验证过)

 

下面是一个具体的编程问题:

非模态对话框对象一般都是全局的,也就是通过 new 来创建的,是在 stack 上的,这样我们需要通过 delete 来销毁对象,那什么时候 delete 对象呢?

msdn 提供的方法:

我们重载自己的 PostNcDestroy(),实现内容如下:

  CMyDialog::PostNcDestroy() { CDialog::PostNcDestroy(); Delete this; }

这样,我们的类就 Auto-Cleanup 了。

 

 

 附录:

void CloseWindow( );

  Minimizes the window.

 

virtual BOOL DestroyWindow( );

  Destroys the Windows window attached to the CWnd object.

  It sends WM_DESTROY and WM_NCDESTROY messages to the window. It does not destroy the CWnd object.

  DestroyWindow is a place holder for performing cleanup. Because DestroyWindow is a virtual function, it is shown in any CWnd-derived class in Class View.

 

WM_DESTROY:

  Indicates window is about to be destroyed.

 

Windows编程入门一例,摘自:http://www.cnblogs.com/JarvisChu/archive/2012/10/26/2752442.html

/************************************************************************
 * 名  称:Windows_Frist_Code.cpp
 * 功  能:Windows编程入门
 * 描  述:包含WinMain函数、WNDCLASS、消息循环等多种内容
           windows窗口程序的流程如下:【WinMain入口】-->创建和设计窗口类
            -->注册窗口类-->创建、显示和更新窗口-->消息循环-->【窗口过程函数】
 * 作  者:JarvisChu
 * 时  间:2012-10-24
 * 修  订:1. 2012-10-26,Jarvis. 完善代码和注释。
 ***********************************************************************
*/

#include <windows.h>
#include <stdio.h>

//回调函数
LRESULT CALLBACK WinProc(
                            HWND hwnd, 
                            UINT uMsg, 
                            WPARAM wParam, 
                            LPARAM lParam 
);

//入口函数 WinMain
int WINAPI WinMain(HINSTANCE hInstance,    //当前应用程序的句柄
                   HINSTANCE hPrevInstance,//先前应用程序的句柄,总是NULL
                   LPSTR lpCmdLine,        //不包含程序名的命令行,可通过GetCommandLine获取
                   int nShowCmd            //窗口显示方式
                )
{
//-------------------创建和设计窗口类----------------------------------------------------
    WNDCLASS wndclass;    
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
    wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);    //LoadCursor(hInstance,MAKEINTRESOURCE(ID_MYCURSOR)); 
    wndclass.hIcon         = LoadIcon(NULL,IDI_APPLICATION);  //LoadIcon(hInstance,MAKEINTRESOURCE(ID_MYICON)); 
    wndclass.hInstance     = hInstance;
    wndclass.lpfnWndProc   = WinProc;
    wndclass.lpszClassName = "Jarvis";
    wndclass.lpszMenuName  = NULL;
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;

//-------------------注册窗口类----------------------------------------------------
    RegisterClass(&wndclass);
    
//-------------------创建显示更新窗口----------------------------------------------------
    HWND hwnd;
    hwnd=CreateWindow("Jarvis""Jarvis", WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_MAXIMIZE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    
    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

//-------------------消息循环----------------------------------------------------
    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}


//窗口过程函数实现
LRESULT CALLBACK WinProc(
                            HWND hwnd, 
                            UINT uMsg, 
                            WPARAM wParam, 
                            LPARAM lParam 
                            )
{
    switch (uMsg)
    {
    case WM_LBUTTONDOWN:
        MessageBox(hwnd,"LeftButton Clicked!","Prompt",0);
        break;

    case WM_CLOSE:
        if(IDYES == MessageBox(hwnd,"Are you sure to quit?","Prompt",MB_YESNO))
        {
            DestroyWindow(hwnd);
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }

    return 0;

} 

posted on 2013-10-23 17:04  Waaaaaall-E  阅读(405)  评论(0编辑  收藏  举报

导航