| Header file: | dialog.h |
| Object name: | fDIALOG |
| Object index: | 1 of 1 Object |
This class is not a wrapper for any specific thing - rather, it is a class that can manage dialog boxes. It's support is currently rudimentry, and you'll have to do a bit extra to get your job done. This object is a little bit more complicated than some others, so look for the sample source code at the end to see how this one works.
Many thanks go to theForger (http://www.winprog.org) for the parts in his tutorial about dialogs. From the tutorial I learned about the Dialog Message queue, and how to write dialogs into resource files. Thanks!
| Prototype | Description |
| void LoadDialog(char* ResourceName, int ResourceID); | This function loads a dialog from a resource file, and then it is ready to be run. You can either specify the resource by it's name or ID number, depending on what you used when you built the resource file. If you don't use the name, then replace that with NULL, and vica-versa for the ID - if you don't use ID then replace that with 0 (Zero). |
| void LoadDialog(HINSTANCE Instance, char* ResourceName, int ResourceID); | This function is almost exactly the same as the above function. This function takes an extra parameter - a Handle to an INSTANCE, which means that you can, for example, load an extra DLL, pass the handle of that DLL, and then load a dialog from the resources stored in that DLL. |
| void SetDlgProc(DLGPROC DialogProc); | This functions sets the function which acts as the Dialog's message queue. You can not set this while a dialog is running - and you MUST set this before running a dialog. |
| int ExecuteModal(HWND Owner); | This function executes the loaded dialog in a modal fashion - it takes control of your program, and you can not swap back to your other windows. You can make this dialog the child of another window if you so wish. The function returns -1 if it can not create the dialog, and otherwise returns a special value with which to close the dialog with. You do not need to save this value; this is done automatically for you. To close this type of dialog, please see the next function. Please note that this function does not return until the dialog is closed via the message queue. |
| void CloseDialogModal(HWND DialogHandle); | This function closes the currently open modal dialog. Please note that the handle to a window, which belongs to the dialog, must be that which is passed along via the dialog's message queue. Unless you save this handle, you must close the dialog via the message queue - which is most likely what you would ordinarily do anyway. |
| bool Execute(HWND Owner); | This function executes the dialog in a non-modal fashion - in such that you can swap between it and your other windows. This could be useful for things like floating toolbars (give it a stay on top option, and make it small). You will need to close this type of dialog with the following function. |
| void CloseDialog(HWND DialogHandle); | This function closes a dialog created with the above function. You will need to supply the handle of the dialog, which can be obtained via the dialog's message queue. |
| void LobbyHandle(HWND DialogHandle); | To use the following functions to grab the handles to other components, you will need to supply the handle to the dialog, which can only be obtained via the dialog message queue. You will need to call this function from within the message queue before you assimiliate controls. |
| void AssimiliateButton(fBUTTON* Control, int ComponentID); | This function assimiliates a button on the dialog and allows you to work with the button via a fBUTTON interface. You will need to have a fBUTTON component with which to pass along the information, and you will need the ID of the component that you gave it when designing the dialog. After you have used this function, you can use events from it, and use any of the interfaces provided via it to manipulate the button. |
| void AssimiliateEdit(fEDIT* Control, int ID); | This function is exactly like the above, except that it works for fEDIT controls instead. |
| void AssimiliateListBox(fLISTBOX* Control, int ID); | This function works exactly like the AssimiliateButton() function, except that it works for a fLISTBOX. |
| void AssimiliateStatic(fSTATIC* Control, int ID); | This function works exactly like the AssimiliateButton() function, except that it works for a fSTATIC. |
| void AssimiliateComboBox(fCOMBOBOX* Control, int ID); | This function works exactly like the AssimiliateButton() function, except that it works for a fCOMBOBOX. |
| void AssimiliateProgressBar(fPROGRESS* Control, int ID); | This function works exactly like the AssimiliateButton() function, except that it works for a fPROGRESSBAR. |
| void SetItemText(int ID, char* Text); | This function sets the text of the component specified by ID. You must call LobbyHandle() before using this function. |
| void SetItemInt(int ID, int Number, bool Signed); | This function sets the text for an item based on a number. Rather than manually converting it to a string and then applying it to a component, Windows can handle this for you. Replace Number with the number to set, and if the number is a signed integer, then pass TRUE for the Signed parameter. Otherwise, pass FALSE. You must have called LobbyHandle() prior to using this function. |
| UINT GetItemInt(int ID); | This function gets a number representation of the text that was in a component on the dialog. This would be most useful for an edit control - ie. returning a number that was typed into it. The number is returned as an unsigned integer. |
The following snippet gives a basic idea of how to use this component.
//The dialog message queue's function prototype...
BOOL CALLBACK DlgProc(HWND Handle, UINT Message,
WPARAM wParam, LPARAM lParam);
//Then inside a function...
//First we start by loading the dialog.
fDIALOG Dialog;
Dialog.LoadDialog(NULL, DIALOG_ID);
//Then we can display it (modal display in this
//example).
Dialog.SetDlgProc(DlgProc);
Dialog.ExecuteModal(Window.GetHandle());
//Now we go on and do other things. But in the
//dialog message queue...
BOOL CALLBACK DlgProc(HWND Handle, UINT Message,
WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
case WM_INITDIALOG:
//This occurs when the dialog is being created.
//Initialise the dialog here, now.
//An example follows...
Dialog.LobbyHandle(Handle);
Dialog.AssimiliateButton(&Button, IDOK);
Button.OnClick = &ButtonOkClick;
break;
case WM_COMMAND:
//If you have assimiliated controls, then
//pass along the message queue here (ie.
//if you have a GUI controller).
//You would do this as follows:
DOPASSALONG
//Otherwise, you'll check wParam against
//the ID's you assigned your controls, eg.
if (wParam == IDOK) Dialog.CloseDialogModal(Handle);
//(The above line checked for the button
// that was assigned IDOK as an ID)
//Or, if a non-modal dialog,
Dialog.LobbyHandle(Handle);
if (wParam == IDOK) Dialog.CloseDialog(Handle);
break;
};
//You can intercept any other message you want - some other
//good ones would be WM_CLOSE (when the user clicks the 'X'
//button in the top right hand corner). You do not need to return
//DefWindowProc(), just TRUE if you process a message. Anything that
//you don't handle, Windows will.
};
//Now, finally, how do you design a dialog? In the resource file. You
//Might end up with the following... (any line starting with > is not
//an explanatory line)
Basic stuff first... Dialog ID, the word DIALOG, x pos,y pos, width, height.
>ABOUTDLG DIALOG 0, 0, 155, 102
The extra (primary) style for the dialog (in this example, it's a stay
on top option)
>EXSTYLE WS_EX_STAYONTOP
Then the style of window for the dialog (secondary style)
>STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
Then the caption of the dialog box, followed by the font.
>CAPTION "This is my About Box"
>FONT 8, "MS Sans Serif"
Then the controls on the dialog. Like this:
CONTROL "Caption", ControlID, "Class", Style, x,y, Width, Height.
(Note: putting -1 as the ID means that you don't need to access that
object again. Useful for static's that don't need to change - less
ID's to worry about.)
>{
> CONTROL "&OK", IDOK, "BUTTON", BS_DEFPUSHBUTTON | BS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 6, 83, 50, 14
> CONTROL "&Cancel", IDCANCEL, "BUTTON", BS_PUSHBUTTON | BS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 95, 83, 50, 14
> CONTROL "About This Program", -1, "button", BS_GROUPBOX | WS_CHILD | WS_VISIBLE | WS_GROUP, 5, 5, 144, 73
> CONTROL "An example program.", -1, "static", SS_CENTER | WS_CHILD | WS_VISIBLE, 27, 27, 100, 50
>}
//So all up, minus explanation lines, it would look like this...
ABOUTDLG DIALOG 0, 0, 155, 102
EXSTYLE WS_EX_STAYONTOP
STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "This is my About Box"
FONT 8, "MS Sans Serif"
{
CONTROL "&OK", IDOK, "BUTTON", BS_DEFPUSHBUTTON | BS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 6, 83, 50, 14
CONTROL "&Cancel", IDCANCEL, "BUTTON", BS_PUSHBUTTON | BS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 95, 83, 50, 14
CONTROL "About This Program", -1, "button", BS_GROUPBOX | WS_CHILD | WS_VISIBLE | WS_GROUP, 5, 5, 144, 73
CONTROL "An example program.", -1, "static", SS_CENTER | WS_CHILD | WS_VISIBLE, 27, 27, 100, 50
}
//Now you should be able to create dialogs. About one more note:
//you may need to #include "windows.h" inside your resource file,
//so that the resource compiler is able to work out what the
//styles mean (ie. WS_VISIBLE is a number, and it's defined
//inside windows.h).
//One last note, and an interesting little thing to keep in mind.
//Let's say that you have an about dialog - all you have is a whole
//heap of static controls, and one button: ok. Isn't it a pain
//to add another DlgProc just for that? Well, don't. Inside dialog.h
//is this code:
BOOL CALLBACK F_SimpleDlgProc(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam)
{
if (Message == WM_INITDIALOG) return TRUE;
if (Message == WM_COMMAND)
{
if (wParam == IDOK) EndDialog(Handle, 0);
return TRUE;
}
};
//What this code does is implement a small dialog handler for you
//and it is intended for dialogs with only one button with an ID
//of IDOK. So create your dialog, set the ID of the OK/Close button
//to IDOK (1), and then make the Dialog Proc F_SimpleDlgProc.
//Simple! No more extra DlgProcs just for an about box! Hopefully
//you'll find this useful.
| Back to index | The FreeFoote Foundation Classes Documentation |