Lecture 7
Menu, controls, scroll bars
Menu• Menu item sends WM_COMMAND message
to the application window• Menu is attached to window when the
window is created• Menu can be created using Visual C++
Resource Editor• Menu can be modified at run time:
– Items can be disabled, enabled, checked (EnableMenuItem, CheckMenuItem, CheckMenuRadioItem)
– Items can be added and removed as required (AppendMenu, DeleteMenu, RemoveMenu)
– Menu item can be modified (ModifyMenu)
• WM_COMMAND handler:case WM_COMMAND:
wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections:switch (wmId){
case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX,
hWnd, (DLGPROC)About); break;case IDM_EXIT: DestroyWindow(hWnd); break;default: return DefWindowProc(hWnd, message,
wParam, lParam);}break;
}
• Menu functions:– LoadMenu – load menu from resources– GetMenu – get menu handle from window handle– GetSystemMenu – get handle to system menu (menu
that has Minimize, Restore, Close commands)– SetMenu – set (or replace) window menu– CreateMenu, CreatePopupMenu – create dynamically
new menu instead of loading it from resources– TrackPopupMenu, TrackPopupMenuEx – display and
track poupu menu on the screen
• Menu functions (cont):– GetMenuItemCount – returns number of items in the
specified menu– GetMenuItemID – get menu item ID (command ID
assigned to that menu item)– GetMenuItemInfo – get information about menu item– GetMenuState – get state of the menu item (if it is
checked, disabled, grayed etc.)– GetMenuString – get menu item string
• Menu functions (cont):– InsertMenuItem – insert new menu item or sub-menu
to the menu– AppendMenu – add new item or sub-menu at the end
of the menu– CheckMenuItem – add or remove check mark from
menu item– CheckMenuRadioItem – position check mark on a
group of menu items– SetMenuItemInfo – modify menu item– RemoveMenu – remove menu item or detach sub-
menu from the specified menu
WM_COMMAND message• wParam
– The high-order word specifies the notification code if the message is from a control. If the message is from an accelerator, this value is 1. If the message is from a menu, this value is zero.
– The low-order word specifies the identifier of the menu item, control, or accelerator.
• lParam– Handle to the control sending the message if the
message is from a control. Otherwise, this parameter is NULL.
Controls• Control - child window that specializes in some task, e.g.:
– button control - can be pressed
– list control - presents list of values
– edit control - allows user to enter some text
• Control must have parent window (because it is a child window)
• Control notifies its parent when important event occurs, e.g.– button control notifies parent when it is pressed
– list control notifies parent when selection changes
– edit control notifies parent every time the text inside the control changes
Controls• Notifications by controls are done using
SendMessage function
• Standard (predefined) controls send WM_COMMAND as the notification message
Predefined controls• BUTTON – button control. This includes:
– push buttons– checkboxes– radio buttons
• LISTBOX – list control
• COMBOBOX – drop down list control
• EDIT – edit control (for editing text)
• STATIC – static text (usually label for other control)
• RichEdit, RICHEDIT_CLASS – rich edit control
• SCROLLBAR – scroll bar control
Creating predefined control HWND hWndButton = CreateWindowEx(
0, // extended style
"BUTTON", // predefined class name
"Button text", // button text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // style
100, 100, // position
70, 30, // size
hWndParent, // parent window
(HMENU)102, // control identifier
hInstance, // instance hande
NULL
);
• WM_COMMAND messages sent by this button will have:– lParam == hWndButton– LOWORD( wParam ) == 102 // (control id)– HIWORD( wParam ) == notification code, e.g.:
• BN_CLICKED - button was clicked
• BN_DOUBLECLICKED
• BN_KILLFOCUS
• BN_SETFOCUS
Customizing predefined controls• To customize predefined control:
– Use one of predefined styles for that control (when creating control), e.g:
• buttons - BS_PUSHBUTTON, BS_RADIOBUTTON
• edit controls - ES_READONLY, ES_UPPERCASE
– Send message to the control, e.g.• list boxes - LB_ADDSTRING, LB_RESETCONTENT
• edit controls - EM_GETLINE, EM_GETLINECOUNT, EM_GETSEL
HWND hWndListBox = CreateWindowEx( 0, "LISTBOX", // class name "", // window title WS_CHILD | WS_VISIBLE, // style 100, 100, // position 170, 150, // size hWndParent, // parent window handle (HMENU)103, // control identifier hInstance, // instance handle NULL );
SendMessage( hWndListBox, LB_ADDSTRING, 0, (LPARAM)"First Item" );
SendMessage( hWndListBox, LB_ADDSTRING, 0, (LPARAM)"Second Item" );
SendMessage( hWndListBox, LB_SETCURSEL, 1, 0 );
• Can be created in two ways:– as a scroll bar attached to a window (standard scroll bar)
– as a control using „SCROLLBAR” class
• Standard scroll bar is created automatically and:– it is automatically resized/moved when the window is resized
– it is created on non-client area of the window
– it does not have window handle
• Scroll bar control must be:– manually created, moved and resized
– it does have normal window handle
– it is created on its parent window client area
Scroll bars
• To create window with scroll bars:hWnd = CreateWindow(szWindowClass, szTitle,
WS_OVERLAPPEDWINDOW
| WS_HSCROLL | WS_VSCROLL,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL);
SCROLLINFO si;
si.cbSize = sizeof( si );
si.fMask = SIF_ALL;
si.nMin = 0; // minimum scrollbar position
si.nMax = 100;// maximum scrollbar position
si.nPos = 50; // current scrollbar position
si.nPage = 10;// page size
SetScrollInfo( hWnd, SB_HORZ, &si, false );
Standard scroll bar
// horizontal scroll bar
CreateWindow( "SCROLLBAR", "",
WS_CHILD | WS_VISIBLE,
30, 50, 100, 20,
hWnd, (HMENU)100, hInstance, NULL );
// vertical scroll bar
CreateWindow( "SCROLLBAR", "",
WS_CHILD | WS_VISIBLE | SBS_VERT,
30, 80, 20, 100,
hWnd, (HMENU)101, hInstance, NULL );
Scroll bar controls
• Scroll bar sends WM_HSCROLL or WM_VSCROLL messages
• wParam can take one of the following values:• SB_ENDSCROLL• SB_LEFT• SB_RIGHT• SB_LINELEFT• SB_LINERIGHT• SB_PAGELEFT• SB_PAGERIGHT• SB_THUMBPOSITION• SB_THUMBTRACK
• lParam handle to the scroll bar control (or NULL)
Scroll bar messages
• Application is responsible for:• Setting scroll bar sizes (SetScrollInfo function)• Handling scroll bar messages (e.g. WM_HSCROLL)
• application should move scrollbar position (using SetScrollInfo or SetScrollPos)
• remember current scrollbar position in some variable• invalidate the window
• Drawing window content scrolled by scrollbar position• Application can scroll window content using ScrollWindow,
ScrollWindowEx or ScrollDC functions and then invalidate only portion of the window
Handling scroll bars
Top Related