局部变量创建非模态对话框

来源:互联网 发布:淘宝西班牙代购真假 编辑:程序博客网 时间:2024/06/05 14:36

不用new创建非模态对话框方法如下:

int DoEvents()
{
    MSG msg;
    // Process existing messages in the application's message queue.
    // When the queue is empty, do clean up and return.
    while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
    {// has msg
        if(msg.message== WM_APP) return 1;
        if (!AfxGetThread()->PumpMessage()) break;
    }
    return 0;
}
//
WNDPROC g_OldProc=0;
LRESULT CALLBACK NewProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    switch(message) 
    
    case WM_CLOSE: 
        PostMessage(GetParent(hwnd),WM_APP,0,0);
    break;
    case WM_COMMAND: // 0x0111
        if(LOWORD(wParam)==IDOK)
        {   
            PostMessage(GetParent(hwnd),WM_APP,0,0);
        }
    break;   
    
    return CallWindowProc(g_OldProc, hwnd, message, wParam, lParam);
//
void CxxxxDlg::OnButton1() 
{
    CAboutDlg dlg;// modeless dlg , not use new !!!
    dlg.Create(IDD_ABOUTBOX);
    dlg.ShowWindow(SW_SHOW);
    g_OldProc = (WNDPROC)SetWindowLong(dlg.m_hWnd, GWL_WNDPROC, (LONG)NewProc);
//
    while (!DoEvents());
    dlg.DestroyWindow();
    afxDump << "Dlg closed()\n";
}

注意
CAboutDlg dlg;// modeless dlg , not use new !!!

0 0
原创粉丝点击