模态对话框和非模态对话框的实现(MFC&API)

Eddy 发布于2010-7-13 23:55:40 分类: 程序设计 已浏览loading 网友评论0条 我要评论

MFC定义了一个应用程序基类CWinApp,所有基于MFC的应用程序都会继承这个类。
class CmfctestApp : public CWinApp
{
public:
 CmfctestApp();

// 重写
public:
 virtual BOOL InitInstance();

// 实现

 DECLARE_MESSAGE_MAP()
};

添加基于CDialogEx类的CMyModalDialog和CMyNoModalDialog类。

MFC实现模态对话框
CMyModalDialog dl;
dl.DoModal();

MFC实现非模态对话框
CMyNoModalDialog* pDlg = new CMyNoModalDialog;
pDlg->Create(IDD_NOMODAL_DIALOG,this);
pDlg->ShowWindow(SW_SHOW);

win32API实现模态对话框
int DialogBox(

    HINSTANCE hInstance, // handle to application instance
    LPCTSTR lpTemplate, // identifies dialog box template
    HWND hWndParent, // handle to owner window
    DLGPROC lpDialogFunc  // pointer to dialog box procedure 
   );
int DialogBoxParam(

    HINSTANCE hInstance, // handle to application instance
    LPCTSTR lpTemplateName, // identifies dialog box template
    HWND hWndParent, // handle to owner window
    DLGPROC lpDialogFunc, // pointer to dialog box procedure 
    LPARAM dwInitParam  // initialization value
   );

HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
DialogBox(hInstance,MAKEINTRESOURCE(IDD_API_DIALOG),this->m_hWnd,ApiDialog);

win32API实现非模态对话框
HWND CreateDialog(

    HINSTANCE hInstance, // handle to application instance
    LPCTSTR lpTemplate, // identifies dialog box template name 
    HWND hWndParent, // handle to owner window
    DLGPROC lpDialogFunc  // pointer to dialog box procedure
   );
HWND CreateDialogParam(

    HINSTANCE hInstance, // handle to application instance
    LPCTSTR lpTemplateName, // identifies dialog box template
    HWND hWndParent, // handle to owner window
    DLGPROC lpDialogFunc, // pointer to dialog box procedure 
    LPARAM dwInitParam  // initialization value
   );


HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
HWND hDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_NOMODAL_DIALOG),NULL,ApiDialog);
::ShowWindow(hDlg,SW_SHOW);

窗口回调函数:ApiDialog
BOOL CALLBACK ApiDialog (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch(message)
 {
 case WM_INITDIALOG:
  {

  }
  return (TRUE);
 case WM_CLOSE:
  {
   EndDialog(hDlg,0);
  }
  return (TRUE);
 case WM_COMMAND:
  switch (LOWORD(wParam))
  {
  case IDCANCEL:
   {
    SendMessage(hDlg, WM_CLOSE, 0, 0);
   }
   return (TRUE);
  case IDOK:
   {
   }
   return (TRUE);
  }
  return (FALSE);
 }
 return (FALSE);
}

已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/434.html     欢迎订阅Eddy Blog

关于 模态  非模态  对话框  MFC  API  的相关文章

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言