首页 > Windows开发 > 详细

Windows编程之非模态对话框

时间:2015-12-28 20:17:57      阅读:291      评论:0      收藏:0      [点我收藏+]

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

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

技术分享


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

 


Windows编程之非模态对话框

原文:http://www.cnblogs.com/lcchuguo/p/5083516.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!