120 likes | 347 Views
Dialog boxes. Modal and modeless dialog boxes Displaying about dialog box: case WM_COMMAND : switch (LOWORD (wParam)) { case IDM_APP_ABOUT : DialogBox (hInstance, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break ; } return 0 ;.
E N D
Dialog boxes • Modal and modeless dialog boxes • Displaying about dialog box: case WM_COMMAND : switch (LOWORD (wParam)) { case IDM_APP_ABOUT : DialogBox (hInstance, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break ; } return 0 ;
BOOL CALLBACK About (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG : return TRUE ; case WM_COMMAND : switch (LOWORD (wParam)) { case IDOK : case IDCANCEL : EndDialog (hDlg, 0) ; return TRUE ; } break ; } return FALSE ; }
Modal dialog box • Created using: • DialogBox, DialogBoxIndirect, DialogBoxParam, DialogBoxIndirectParam • Disables dialog window owner until the dialog is closed • Closed by the EndDialog function. • Result of the DialogBox function is the argument passed to the EndDialog function
Dialog procedure • Similar to window procedure • Returns TRUE if it message was processed FALSE if not • Does not call DefWindowProc • Should not process WM_PAINT, WM_DESTROY messages • Does not receive WM_CREATE, instead it receives WM_INITDIALOG message
WM_INITDIALOG • WM_CREATE is sent to the window right after it has been created • WM_INITDIALOG is sent to the dialog after all its controls have been created. • In WM_INITDIALOG application can access controls, e.g.: • enable or disable controls • fill controls with data (e.g. ListBoxes) • set initial values of Edit controls • set initial state of checkboxes and radio buttons
Controls • Controls are regular windows (child windows) • They have window handle and process messages like any other window • To get window handle for the control use GetDlgItem function HWND GetDlgItem(HWND hDlg, int itemId); itemId is the child window number. For dialogs created from resource template this will be the control id from the resource file - Any function that operates on window handle can be used on a control, e.g.: • MoveWindow- move window on the screen • ShowWindow- show / hide window • EnableWindow - enable / disable window
Using controls • To set/get text from EDIT control: • SetWindowText or SetDlgItemText • GetWindowText or GetDlgItemText • To set/get checkbox and radio button selection: • CheckDlgButton • IsDlgButtonChecked • To add elements to LISTBOX • Send LB_ADDSTRING message to add element to LISTBOX • Send LB_SETCURSEL message to select element in a LISTBOX
Using controls • LISTBOX example: HWND hListWnd = GetDlgItem( hDlg, IDC_LIST ); SendMessage( hListWnd, LB_ADDSTRING, 0, (LPARAM)"item 1" ); SendMessage( hListWnd, LB_ADDSTRING, 0, (LPARAM)"item 2" ); SendMessage( hListWnd, LB_ADDSTRING, 0, (LPARAM)"item 3" ); SendMessage( hListWnd, LB_ADDSTRING, 0, (LPARAM)"item 4" ); SendMessage( hListWnd, LB_ADDSTRING, 0, (LPARAM)"item 5" ); SendMessage( hListWnd, LB_SETCURSEL, 2, 0 );
Dialog boxes • Dialog box is a regular window, so any function that operates on windows works for dialog boxes (e.g. MoveWindow) • Area covered by controls is painted by controls. Dialog cannot paint over the area occupied by controls (usually dialog does not paint at all) • If mouse cursor is over the control, the control handles mouse messages • All keyboard messages are processed by the dialog and some of them are passed to controls • Modal dialog box has its own message loop
Modeless dialog boxes • Modeless dialog boxes don’t disable parent window, so user can work in both windows • To create modeless dialog box use: HWND CreateDialog(HINSTANCE hInstance, LPCSTR template, HWND parent, DLGPROC lpDialogFunc ); • This function does not wait for the dialog box to close (it works like CreateWindow) • The window must be destroyed using DestroyWindow function (don’t call EndDialog!)
Modeless dialog boxes • Modeless dialog box requires change to the application message queue: while (GetMessage (&msg, NULL, 0, 0)) { if (hDlgModeless == 0 ¦¦ !IsDialogMessage (hDlgModeless, &msg)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } }
IsDialogMessage • IsDialogMessage – checks if message is intended for the dialog box and if so, processes the message • TAB key moves focus to the next group of controls • DOWN key moves focus to the next control in a group • ENTER key pushes the default pushbutton for the dialog • IsDialogMessage can be used for any type of window