Lecture 7

19
Lecture 7 Menu, controls, scroll bars

description

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: - PowerPoint PPT Presentation

Transcript of Lecture 7

Page 1: Lecture 7

Lecture 7

Menu, controls, scroll bars

Page 2: Lecture 7

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)

Page 3: Lecture 7

• 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;

}

Page 4: Lecture 7

• 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

Page 5: Lecture 7

• 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

Page 6: Lecture 7

• 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

Page 7: Lecture 7

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.

Page 8: Lecture 7

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

Page 9: Lecture 7

Controls• Notifications by controls are done using

SendMessage function

• Standard (predefined) controls send WM_COMMAND as the notification message

Page 10: Lecture 7

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

Page 11: Lecture 7

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

);

Page 12: Lecture 7

• 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

Page 13: Lecture 7

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

Page 14: Lecture 7

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 );

Page 15: Lecture 7

• 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

Page 16: Lecture 7

• 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

Page 17: Lecture 7

// 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

Page 18: Lecture 7

• 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

Page 19: Lecture 7

• 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