Windows API - Resources

Menus and Dialogs

Menus

You create a menu in the resource editor. This can be found via a tab in the file list window in Viz. Creating menus is very simple, you just add items to existing menus or create new ones. You can enter the text that will be displayed and a unique identifier that you use to refer to the menu item in your code. In order for your application to display the menu it is attached to a window in the class definition. Note: the window style must also be one that can support a menu.

If you have a menu created in the resource editor called IDC_MENU you would assign it to the class structure:

wcex.lpszMenuName= (LPCSTR)IDC_MENU;

In order to act on menu selections you need to trap the message in your WndProc. The message is WM_COMMAND and the LOWORD(wParam) contains the identifier of the menu item that was selected

Dialogs

Dialogs are just windows like your main window. Generally they are used to allow the entry of data by the user so will contain text entry boxes, sliders, buttons etc. Dialogs can be modal or modeless. If a dialog is modal it means that no other window can be active until that dialog is closed, modeless means the dialog can be clicked away from and other windows can receive input.

Message Box

If all you want to do is display a yes no box there is a built in function to do it:

if (MessageBox(hWnd,"Do you really want to exit?","Warning",MB_YESNO)==IDYES)
       ... exit

The above puts up a modal dialog with the title 'Warning' containing the text 'Do you really want to exit?' and has two buttons for yes and no. If the user clicks yes the dialog closes and returns IDYES. There are a number of styles you can define.

int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

hWnd - this is the handle of the owner, if it is your main window the dialog will be positioned over it. This can be NULL to represent the desktop where by the dialog will appear centred in the desktop,
lpText - a pointer to the text you wish to display in the dialog box.
lpCaption - a pointer to the text you wish to display in the bar at the top of the dialog.
uType - the type of dialog. This can be a combination of flags. e.g. MB_OK | MB_ICONQUESTION will create a dialog with just an OK button and an icon showing a question mark - look in the MSDN help for other options.

Custom Dialog

To create your own custom dialog you must create it in the resource editor. You can assemble all the components you require and assign unique identifiers to them so you can refer to them in the code. For your own dialog you will need a function to receive messages just like you needed for your main window. To display your dialog:

DialogBox(gInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)AboutDlgProc);

The above creates a dialog from the resource editor defined template called IDD_ABOUTBOX and requests that all messages aimed at that dialog be sent to our function AboutDlgProc.

Note: because this is a modal dialog control will not pass to the next line after this until the dialog is closed.

The full function call  is defined as:

bool DialogBox( HINSTANCE hInstance, LPCTSTR lpTemplate, HWND hWndParent, DLGPROC lpDialogFunc);

  • hInstance - handle to application instance (often saved as a global)
  • lpTemplate - the resource identifier that represents the dialog template as created in the resource editor.
  • hWndParent - the handle of the owner window.
  • lpDialogFunc - a pointer to your function that will receive messages sent to this dialog.

Dialog Callback Function

Your dialog callback function is similar to the WndProc function you created earlier. There are a couple of extra issues though. When your dialog is created the function will receive a WM_INITDIALOG message, this is useful to trap as you can fill dialog text entry values here before the dialog is displayed.  To close the dialog you call EndDialog(hDlg,LOWORD(wParam)). The second parameter is the value you wish returned from the DialogBox call above.

Dialogs are created from resources and Windows handles the drawing of the dialog so there is no need for you to handle the WM_PAINT message. Also you do not need to pass unhandled messages to Windows (DefWindowProc), simply return FALSE if you do not handle a message and TRUE if you do.



© 2004-2016 Keith Ditchburn