Model Dialog in Win32 c++
Creating model dialog is very easy in MFC. It has neat class for dialog , CDialog. The DoModel functions of CDialog provides a way to appear model dialog.
In Win32 this is another story. Many people doesn't know what is a model dialog. Some may thought it is a special type of dialog, like the different window classes(EDIT,BUTTON etc) . Actually model dialog is just a modeless dialog with some additions. Creating a model dialog requires to create modeless dialog ( using CreateDialog ,or even CreateWindow(wnd) wnd = dialog class name).
Following is a sample class , which will show a modle dialog in win32, (Just like the CDialog::DoModal() in MFC).
class name CBaseDlg, In this class i wrote a function DoModel, see below.
The MyDlgProc is the dialog procedure function . In this procedure you can see that it calls the CBaseDialog::DialogProc function (blue color). So the derived class of CBaseDlg can override this function and add their command handlers.
LRESULT CALLBACK MyDlgProc(HWND hDlg,UINT nMsg,WPARAM wParam, LPARAM lParam)
{
CBaseDlg* pWnd = NULL;
pWnd = CBaseDlg::FromHandle(hDlg);
if(pWnd)
return pWnd->DialogProc(hDlg,nMsg,wParam,lParam);
return FALSE;
}
void
CBaseDlg::DoModel(HINSTANCE hInst,HWND hParent,UINT nResourceID)
{
HWND hwnd = CreateDialog(hInst,(LPCTSTR)nResourceID, hParent, (DLGPROC)MyDlgProc);
HwndMaps.insert(std::pair<HWND,CBaseDlg*>(hwnd,this) ); //Add this hwnd for using in MyDlgProc.
ShowWindow(hwnd,SW_SHOWNORMAL); //show dialog.
SendMessage(hwnd,WM_INITDIALOG,0,0); ///send INIT_DIALOG message as usual.
EnableWindow(hParent,FALSE);//Disable parent window
MSG msg;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
EnableWindow(hParent,TRUE);
DestroyWindow(hwnd); //Destroy me
HwndMaps.erase(HwndMaps.begin(),HwndMaps.end()); //Clear the window map , this is just like the MFC way in win32.
}
Here in DoModel Passing the parent window handle and resource id , It will show a model dialog. The inside message loop will prevent the function from returning . The parent window should be disable , otherwise still user can do on parent window. So by disabling the window , the only active window is the dialog.