Now we take a look at
We have create the dialog resource using the Stony Brook Resource Editor and it is included in the download.
We want to create the dialog when our program runs, and we want the dialog visible right
away so we do this in
VAR g_hToolbar : HWND;
This is some new code in our
VAR ptr : LPTSTR; BEGIN CASE msg OF | WM_CREATE : ptr := MAKEADR(IDD_TOOLBAR); g_hToolbar := CreateDialog(Instance, ptr^, hwnd, ToolDlgProc); IF g_hToolbar # NIL THEN FUNC ShowWindow(g_hToolbar, SW_SHOW); ELSE FUNC MessageBox(hwnd, "CreateDialog returned NIL", "Warning!", MB_OK BOR MB_ICONINFORMATION); END; (* ... *)
We check the return value, which is ALWAYS a good idea, and if it's valid (not
Now we need a dialog procedure for our toolbar.
PROCEDURE ToolDlgProc(hwnd : HWND; msg : UINT; wParam : WPARAM; lParam : LPARAM) : BOOL [EXPORT, OSCall]; BEGIN CASE msg OF | WM_COMMAND : CASE LOWORD(wParam) OF | IDC_PRESS : FUNC MessageBox(hwnd, "Hi!", "This is a message", MB_OK BOR MB_ICONEXCLAMATION); | IDC_OTHER : FUNC MessageBox(hwnd, "Hi!", "This is also a message", MB_OK BOR MB_ICONEXCLAMATION); ELSE END; ELSE RETURN FALSE; END; RETURN TRUE; END ToolDlgProc;
Most of the same message handling rules apply to dialogs created with
One change is that we don't call
| WM_DESTROY : DestroyWindow(g_hToolbar); PostQuitMessage(0);
Last but not least, we want to be able to display and hide our toolbar whenever we choose so I've added two commands to my menu to do this, and handled them so:
| WM_COMMAND : CASE LOWORD(wParam) OF | ID_FILE_EXIT : FUNC PostMessage(hwnd, WM_CLOSE, 0, 0); | ID_DIALOG_SHOW : FUNC ShowWindow(g_hToolbar, SW_SHOW); | ID_DIALOG_HIDE : FUNC ShowWindow(g_hToolbar, SW_HIDE); ELSE RETURN DefWindowProc(hwnd, msg, wParam, lParam); END;
You should be able to create your own menu using the resource editor, but if not (as always) take a look at the example project dlg_two provided with the tutorial.
Now when you run the program, you should be able to access both the dialog window, and main window at the same time.
If you've run the program at this point and tried tabbing between the two buttons, you have probably
noticed it doesn't work, neither does hitting Alt-P or Alt-O to activate the buttons.
Why not? Whereas
WHILE GetMessage( Msg, NIL, 0, 0) DO IF ~IsDialogMessage(g_hToolbar, Msg) THEN FUNC TranslateMessage(Msg); FUNC DispatchMessage(Msg); END; END;
Here we first pass the message to
It's also worth noting that
And that is pretty much all there is to modeless dialogs! One issue that may arise is if
you have more than one toolbar... what do you do? Well one possible solution is to have
a list (either an array, an STL
Copyright © 1998-2011, Brook Miles. All rights reserved. Adapted for Modula-2 by Frank Schoonjans, with permission.