Now let's take a look at CreateDialog (), which is the sister function of DialogBox. the difference is that DialogBox () has its own message loop and is not returned until the dialog box is closed. CreateDialog () is more like a window created by createmediawex, return immediately and send messages to your messages cyclically, just like the messages sent in your main window. this is called modal-free mode, while DialogBox () creates a modal dialog box.
The created resources are as follows:
[Cpp]
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My Dialog Toolbar"
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "& Press This Button", IDC_PRESS, 7,7, 84,14
PUSHBUTTON "& Or This One", IDC_OTHER, 7,31, 84,14
END
You can see that the resource editor converts DIALOG into DIALOGEX, indicating that we need to set the extended style for our DIALOG box.
Next, you want to create a dialog box while the program is running. to visualize the dialog box, create it in message processing of WM_CREATE. we also need to declare a global variable to keep the window handle returned from CreateDialog () so that it can be used later. dialogBox () does not return a handle to us because DialogBox () returns a handle only when the window is sold out.
[Cpp] view plaincopyprint?
HWND g_hToolbar = NULL;
Case WM_CREATE:
G_hToolbar = CreateDialog (GetModuleHandle (NULL), MAKEINTRESOURCE (IDD_TOOLBAR ),
Hwnd, ToolDlgProc );
If (g_hToolbar! = NULL)
{
ShowWindow (g_hToolbar, SW_SHOW );
}
Else
{
MessageBox (hwnd, "CreateDialog returned NULL", "Warning! ",
MB_ OK | MB_ICONINFORMATION );
}
Break;
It is always a good habit to check the return value. If it is correct (not NULL), we will use ShowWindow () to display the window. If it is DiaglogBox (), this step is unnecessary because the system calls ShowWindow () for us ().
Now I need to write a dialog process for the toolbar.
[Cpp]
Bool callback ToolDlgProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
Switch (Message)
{
Case WM_COMMAND:
Switch (LOWORD (wParam ))
{
Case IDC_PRESS:
MessageBox (hwnd, "Hi! "," This is a message ",
MB_ OK | MB_ICONEXCLAMATION );
Break;
Case IDC_OTHER:
MessageBox (hwnd, "Bye! "," This is also a message ",
MB_ OK | MB_ICONEXCLAMATION );
Break;
}
Break;
Default:
Return FALSE;
}
Return TRUE;
}
Most message processing rules applicable to DialogBox () also apply to CreateDialog (). If DefWindowProc () is not called, FALSE is returned for unprocessed messages and TRUE is returned for processing messages.
An EndDialog () is not called for a non-modal window. You can call DestroyWindow () Like a regular window. Write it in WndProc () in the main window...
[Cpp]
Case WM_DESTROY:
DestroyWindow (g_hToolbar );
PostQuitMessage (0 );
Break;
Another point is that we want to display or hide our toolbar at any time, so I added two commands to the menu to do this and handle it like this:
[Cpp] view plaincopyprint?
Case WM_COMMAND:
Switch (LOWORD (wParam ))
{
Case ID_DIALOG_SHOW:
ShowWindow (g_hToolbar, SW_SHOW );
Break;
Case ID_DIALOG_HIDE:
ShowWindow (g_hToolbar, SW_HIDE );
Break;
//... Other command handlers
}
Break;
If you run your program and try to switch between the two buttons at this time, you will notice that no, press Alt + P and Alt + O to activate the button. Why? Because DialogBox () has its own message loop and handles these events by default, CreateDialog () has no. however, you can call the IsDialogMessage () that can be processed by default in the message loop to process them by yourself.
[Cpp]
While (GetMessage (& Msg, NULL, 0, 0 ))
{
If (! IsDialogMessage (g_hToolbar, & Msg ))
{
TranslateMessage (& Msg );
DispatchMessage (& Msg );
}
}
Here, we first pass the message to IsDialogMessage (). If the message is in the toolbar (indicated by the handle of the window we pass in), the system performs the default processing and returns TRUE. in this case, the message has been processed, so we do not need to call TranslateMessage () or DispatchMessage. if the message is in another window, it will be processed as usual.
It is worth mentioning that IsDialogMessage () can also be used in a dialog box rather than a window to provide them with functions like a dialog box. remember, a dialog box is a window, and most (if not all) APIs of the dialog box can work in any window.
Paste the complete code a little long:
The resource file is created as above.
[Cpp]
# Include <windows. h>
# Define IDD_TOOLBAR 101
# Define IDC_OTHER 9003
# Define IDC_PRESS 9004
# Define ID_DIALOG_SHOW 9005
# Define ID_DIALOG_HIDE 9006
HWND g_hToolbar = NULL;
Const char g_szClassName [] = "myWindowClass ";
Bool callback AboutDlgProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
Switch (Message)
{
Case WM_INITDIALOG:
Return TRUE;
Case WM_COMMAND:
Switch (LOWORD (wParam ))
{
Case IDC_PRESS:
MessageBox (hwnd, "you press a button! "," This is message ", MB_ OK | MB_ICONINFORMATION );
Break;
Case IDC_OTHER:
MessageBox (hwnd, "you press a other button! "," This is message ", MB_ OK | MB_ICONINFORMATION );
Case IDOK:
EndDialog (hwnd, IDOK );
Break;
Case IDCANCEL:
EndDialog (hwnd, IDCANCEL );
Break;
}
Break;
Default:
Return FALSE;
}
Return TRUE;
}
Lresult callback WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Switch (msg)
{
Case WM_CREATE:
{
HMENU hmenu, hSubMenu;
Hmenu = CreateMenu ();
HSubMenu = CreatePopupMenu ();
AppendMenu (hSubMenu, MF_STRING, ID_DIALOG_SHOW, "& Show ");
AppendMenu (hSubMenu, MF_STRING, ID_DIALOG_HIDE, "& Hide ");
AppendMenu (hmenu, MF_STRING | MF_POPUP, (UINT) hSubMenu, "& Dialog ");
SetMenu (hwnd, hmenu );
G_hToolbar = CreateDialog (GetModuleHandle (NULL), MAKEINTRESOURCE (IDD_TOOLBAR), hwnd, AboutDlgProc );
If (g_hToolbar! = NULL)
ShowWindow (g_hToolbar, SW_HIDE );
Else
MessageBox (hwnd, "CreateDialog return NULL", "Warning", MB_ OK | MB_ICONINFORMATION );
}
Break;
Case WM_COMMAND:
Switch (LOWORD (wParam ))
{
Case ID_DIALOG_SHOW:
ShowWindow (g_hToolbar, SW_SHOW );
Break;
Case ID_DIALOG_HIDE:
ShowWindow (g_hToolbar, SW_HIDE );
Break;
}
Break;
Case WM_LBUTTONDOWN:
{
Char szFileName [MAX_PATH];
HINSTANCE hInstance = GetModuleHandle (NULL );
GetModuleFileName (hInstance, szFileName, MAX_PATH );
MessageBox (hwnd, szFileName, "This program is:", MB_ OK |
MB_ICONINFORMATION );
}
Break;
Case WM_CLOSE:
DestroyWindow (hwnd );
Break;
Case WM_DESTROY:
DestroyWindow (g_hToolbar );
PostQuitMessage (0 );
Break;
Default:
Return DefWindowProc (hwnd, msg, wParam, lParam );
}
Return 0;
}
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
Wc. cbSize = sizeof (WNDCLASSEX );
Wc. style = 0;
Wc. lpfnWndProc = WndProc;
Wc. cbClsExtra = 0;
Wc. cbWndExtra = 0;
Wc. hInstance = hInstance;
Wc. hIcon = LoadIcon (NULL, IDI_APPLICATION );
Wc. hCursor = LoadCursor (NULL, IDC_ARROW );
Wc. hbrBackground = (HBRUSH) (COLOR_WINDOW + 1 );
Wc. lpszMenuName = NULL;
Wc. lpszClassName = g_szClassName;
Wc. hIconSm = LoadIcon (NULL, IDI_APPLICATION );
If (! RegisterClassEx (& wc ))
{
MessageBox (NULL, "Window Registration Failed! "," Error! ",
MB_ICONEXCLAMATION | MB_ OK );
Return 0;
}
Hwnd = createdomainwex (
WS_EX_CLIENTEDGE,
G_szClassName,
"The title of my window ",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400,400,
NULL, NULL, hInstance, NULL );
If (hwnd = NULL)
{
MessageBox (NULL, "Window Creation Failed! "," Error! ",
MB_ICONEXCLAMATION | MB_ OK );
Return 0;
}
ShowWindow (hwnd, nCmdShow );
UpdateWindow (hwnd );
While (GetMessage (& Msg, NULL, 0, 0)> 0)
{
If (! IsDialogMessage (g_hToolbar, & Msg ))
{
TranslateMessage (& Msg );
DispatchMessage (& Msg );
}
}
Return Msg. wParam;
}