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   含有父窗口的对话框程序

Windows编程之非模态对话框_第1张图片

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

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

Windows编程之非模态对话框_第2张图片

<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一样,都是定义好的窗口的类,因而不需要注册。

生成的单独对话框程序如下图所示:

Windows编程之非模态对话框_第3张图片


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

 


你可能感兴趣的:(Win32,非模态对话框)