Windows编程之非模态对话框

1  创建非模态对话框

<1>  HWNDCreateDialog(  HINSTANCE hInstance,  // handle to module

                LPCTSTRlpTemplate,    // dialog box template name

                HWNDhWndParent,    // handle to owner window

                DLGPROClpDialogFunc  // dialog box procedure);

<2> HWND CreateDialogIndirect(  HINSTANCE hInstance,        // handle to module

                LPCDLGTEMPLATE lpTemplate,   //dialog box template

                HWND hWndParent,           // handle to owner window

                DLGPROC lpDialogFunc         //dialog box procedure);

<3> HWND CreateDialogIndirectParam(  HINSTANCE hInstance,   // handle to module

                LPCDLGTEMPLATE lpTemplate,  // dialog box template

                HWND hWndParent,          // handle to owner window

                DLGPROC lpDialogFunc,       // dialog box procedure

                LPARAM lParamInit           // initialization value);

<4> HWND CreateDialogParam(  HINSTANCE hInstance,     // handle to module

                LPCTSTR lpTemplateName,    //dialog box template

                HWND hWndParent,         // handle to owner window

                DLGPROC lpDialogFunc,      //dialog box procedure

                LPARAM dwInitParam        //initialization value);

         參数跟非模态对话框一致。CreateDialogParam会调用CreateWindowExe去创建一个窗体,所以这种对话框事实上就是窗体。当然单独一个对话框能够没有父窗体直接单独存在。

2   含有父窗体的对话框程序


3 自己定义窗体类(无父窗体)的对话框程序

<1>在脚本中指定对话框的类型以及各种控件假定脚本名称为new.dlg


<2>然后在rc文件里加入脚本


<3>在WinMain函数注冊窗体类

         注意这里的CreateDialog的最后一个參数DLGPROC为NULL,由于在窗体类中指定了窗体过程函数。

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCEhPrevInstance,PSTR szCmdLine, intiCmdShow)     
{   
    staticTCHAR szAppName[] = TEXT ("HexCalc");
    HWND                    hwnd ;
    MSG                     msg ;
    WNDCLASS                wndclass ;
       
    wndclass.style              = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc        = WndProc ;
    wndclass.cbClsExtra         = 0 ;
    wndclass.cbWndExtra         = DLGWINDOWEXTRA ; // note here
    wndclass.hInstance          = hInstance ;
    wndclass.hIcon              = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor            = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground      = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName       = NULL ;
    wndclass.lpszClassName      =szAppName ;
       
    if(!RegisterClass (&wndclass))   
         {
        MessageBox (  NULL, TEXT ("Thisprogram requires Windows NT!"), szAppName, MB_ICONERROR) ; 
        return0 ;
    }
       
         hwnd =CreateDialog(hInstance,szAppName,NULL,NULL);    //note here
       
    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;
       
    while(GetMessage (&msg, NULL, 0, 0))       
         {
                   TranslateMessage (&msg) ;
                   DispatchMessage (&msg) ;
    }
       
    returnmsg.wParam ;      
}
生成的窗体程序程序如图所看到的:


12 从资源载入(无父窗体)的对话框程序

LRESULT CALLBACKWndProc(HWND,UINT,WPARAM,LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCEhPrevInstance,PSTR szCmdLine, intiCmdShow)     
{
    HWND                    hwnd ;
    MSG                     msg ;
  
         //hwnd =CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)WndProc);
    hwnd =CreateDialogParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)WndProc,NULL);
 
    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;
       
    while(GetMessage (&msg, NULL, 0, 0))       
         {
                   TranslateMessage (&msg) ;
                   DispatchMessage (&msg) ;
    }   
    returnmsg.wParam ;      
}
能够看到这里没有窗体的设计,窗体的注冊,由于这样的从资源载入的对话框的窗体类是已经定义的,就像那种PushButton一样。都是定义好的窗体的类,因而不须要注冊。

生成的单独对话框程序例如以下图所看到的:


以上程序的下载地址:下载


原文地址:https://www.cnblogs.com/lcchuguo/p/5083516.html