Child Window Controls
? Windows created by a parent window
? An app uses them in conjunction with parent
? Normally used for simple I/O tasks
Child Window Controls ? Properties, appearance, behavior determined by
predefined class definitions
– But behavior can be customized
– Easy to set them up as common Windows objects
• buttons, scroll bars, etc.
? Can also define custom Child Window Controls
? Allow user to display/select info in standard ways Six “Classic” Control Types
? Windows Environment does most of work in: ? Go back to first versions of Windows
– painting/updating a Control's screen area ? Implemented in User.exe
– determining what user is doing
? Can do the "dirty work" for the main window
Type Window Class MFC Class
? Often used as input devices for parent window ----------------------------------------------------
? Are the "working components" of Dialog Boxes Static Text “STATIC” CStatic
? Windows OS contains each control's WinProc Button “BUTTON” CButton
– so messages to controls are processed in predefined way Edit Control “EDIT” CEdit
? Parent window communicates with controls by List Box “LISTBOX” CListBox
sending/receiving messages Combo Box “COMBOBOX” CComboBox
Scroll Bar “SCROLLBAR” CScrollBar
? All are windows
The Common Controls
? Windows Environment automatically repaints a
TYPE WINDOW CLASS MFC CLASS
Control upon exposure ---------------------------------------------------
? Example: NotePad ("File"|“Page Setup") Animation “SysAnimate32” CAnimateCtrl
– Contains most of “classic” controls ComboBoxEx “ComboBoxEx32” CComboBoxEx
Date-Time “SysDateTimePick32” CDateTimeCtrl
– There are 20 other predefined "Common Controls”
Header “SysHeader” CHeaderCtrl
– Most first appeared in Windows 95 Hotkey “msctls_hotkey32” CHotKeyCtrl
– Some came with Internet Explorer Image List N/A CImageList
– Implemented in Comctl32.dll IP Address “SysIPAdress32” CIPAddressCtrl
List View “SysListView32” CListCtrl
Month Calendar “SysMonthCal32” CMonthCalCtrl
Progress “msctls_progress32” CProgressCtrl
Property Sheet N/A CPropertySheet
ReBar “ReBarWindows32” CReBarCtrl
1
TYPE WINDOW CLASS MFC CLASS Classic Window Controls
--------------------------------------------------- ? Static
Rich Edit “RichEdit20A” CRichEditCtrl – Primarily to display text
Slider “msctls_trackbar32” CSliderCtrl
Spin Button “msctls_updown32” CSpinButtonCtr – Can also display icon images and rectangles
Status Bar “msctls_statusbar32” CStatusBarCtrl – Automatically redrawn if exposed
Tab “SysTabControl32” CTabCtrl – Often used as labels for other controls
Toolbar “ToolbarWindow32” CToolBarCtrl
ToolTip “tooltips_class32” CToolTipCtrl ? Button
Tree View “SysTreeView32” CTreeCtrl
– “Clicked” by user to indicate desired actions or choices
made
– Lots of different styles (e.g., pushbutton, check, radio,
group)
– Typically notify parent window when user chooses the
button
? List Box ? Scroll Bar
– Contains lists of items that can be selected – Lets user choose direction/distance to move a “thumb”
– Entire list is shown – Two types:
• Control attached to edge of a parent window
– User selects items
• Allows user to "scroll" the information in a parent
– Selected item is highlighted window's client area
? Combo Box – Stand-alone child window control
– Edit box combined with a list box • Allows user to enter/change a value by moving
– List box can be displayed at all times or pulled down scroll bar "thumb ”
– User selects item from list & item is copied to edit box ? Edit
– One type allows user to type into edit box – To enter/view/edit/delete text
• If text matches item in list, it is highlighted & scrolled into view – Single or multiline control
– Another type doesn’t allow user to type in edit box – Lots of word processing capability
– Also Clear/Copy/Cut/Paste/Undo capability
Creating Controls--Win32 API ? 3. Window style
WS_, SS_, BS_, ES_, LBS_, CBS_, SBS_ (see
? CreateWindow( ) CreateWindow help)
– For any kind of window, including a control – Several styles can be combined with the bitwise or
– Typically called in response to WM_CREATE operator ( | )
? Parameters: – All controls should include WS_CHILD style
– 1. Predefined control class names: ? Parameters 4-7:
• "STATIC", "BUTTON", “EDIT”, “LISTBOX”, – X,Y position (Relative to the upper left corner of parent
“COMBOBOX”, ”SCROLLBAR”, others
window client area)
– 2. Name of the window
• BUTTON, EDIT, STATIC classes: – Width & Height
– text in center of control ? 8. Handle to the parent window
• COMBOBOX, LISTBOX, SCROLLBAR classes:
– ignored (use "")
2
? 9. Handle to “menu” Example (Win32 API)
– Controls don’t have menus
– So hMenu parameter used to hold control’s integer ID ? In response to WM_CREATE in Main Window’s
– ID value passed with WM_COMMAND message WndProc( ):
generated when user interacts with the control
– Allows program to identify which control was activated HWND hMyButton;
? 10. Handle to instance of program creating control HINSTANCE hInstance;
– GetWindowLong () usually used to get this value hInstance = (HINSTANCE) GetWindowLong (hWnd,
GWL_HINSTANCE);
? 11. Pointer to window creation data hMyButton = CreateWindow (“BUTTON”, “Push Me”,
– Normally NULL WS_CHILD | BS_PUSHBUTTON, 10, 10, 130, 60, hWnd,
(HMENU)ID_MYBUTTON, hInstance, NULL);
ShowWindow (hMyButton, SW_SHOWNORMAL);
Creating Controls -- MFC Using a Child Window
– CWnd is the parent class of controls Control, MFC
– Define control in a related class or handler, e.g.:
CStatic myCtrl; ? Manipulate the control using its (and CWnd
– Use the control's override of CWnd::Create() to create the parent class) member functions
control (typically in OnCreate() handler)
• Mostly same parameters as CreateWindow(), e.g.:
– See Online help
RECT r; ? When finished with the control, use
r.left = r.right = 10; r.right = 200; r.bottom = 30; CWnd::DestroyWindow() to destroy the
myCtrl.Create (“Hello”, WS_CHILD | WS_VISIBLE |
control
SS_LEFT, r, this, ID_MYSTATIC);
• Last parameter the control ID (defined in a .h file)
Messages from Controls Win32 API Control Message
? Most work as follows:
Handlers
– User interacts with the control ? Put Control message handlers in same
– WM_COMMAND message sent to parent window switch/case statement with menu handlers
– LOWORD(wParam) = Control ID (WM_COMMAND)
– lParam = control’s window handle
? Done just as for menu handlers
– HIWORD(wParam) = notification code
• identifies what the user action was
? Scroll Bars are a bit different
3
MFC Control Message Sending Messages to
Handlers Controls, Win32 API
? Set up message macro for each notification ? SendMessage( )--sends message to a window’s
code of interest WinProc( )
– e.g., for button’s BN_CLICKED notification ? Doesn't return until message has been processed
• ON_BN_CLICKED (ID, OnClickHandler) ? Parameters:
? Declare the handler functions in the .h file – Handle of destination window
? Write the handler functions in .cpp file, e.g. – ID of message to send
void CMyProgView::OnClickHandler() – wParam and lParam values containing message data, if
{ // code goes here }; any
Example, Win32 API Sending Messages to Controls,
? Send a message to hMyControl MFC
SendMessage (hMyControl, WM_SETTEXT, 0, ? Use the Control’s SendMessage() function
(LPARAM) “Hello”) ;
– Here message is WM_SETTEXT to send the control a message
– When received, control's WndProc() changes control’s ? For example, assume m_ myStatic is a
window name (text string displayed) CStatic control object that has been created
– For this message wParam must be 0;
? To change the text displayed:
? There are many messages that can be sent to a
control char cBuf[] = “Hello”;
? Depend on type of control, See online help m_myStatic.SendMessage (WM_SETTEXT, 0,
(LPARAM)cBuf );
Alternatives to SendMessage() Static Controls
? Lots of styles, see online help on “Static Control
? Could use other class member functions Styles”. Some examples:
? For most messages that can be sent to a control, – SS_BITMAP, SS_CENTER,
there is a corresponding function SS_GRAYFRAME, SS_ICON, SS_SIMPLE,
? Most are members of CWnd parent class SS_WHITEFRAME, etc.
? Example sending WM_SETTEXT to a static ? Change text with WM_SETTEXT message or
control SetWindowText( )
– SetWindowText ( ), for example: – May need to format values with wsprintf()
m_ myStatic.SetWindowText (“Hello”);
? Retrieve text with WM_GETTEXT message or
? Could also use PostMessage() GetWindowText()
– Returns immediately
? Program examples: static, static_mfc
4
Button Controls Graphical Push Buttons
? Some Styles: BS_PUSHBUTTON, ? One way: use CBitmapButton class
BS_RADIOBUTTON, BS_CHECKBOX, ? Assume we have a CBitmapButton object called
BS_OWNERDRAW, BS_GROUPBOX, etc. m_bitmapbut and two bitmaps in the resources:
? Button notification codes: – IDB_BMUP: “up state” bitmap
– BN_CLICK, BN_DOUBLECLICK – IDB_BMDOWN: “down state” bitmap
? Some messages you can send to buttons: ? Some code:
– BM_SETCHECK, BM_GETCHECK, m_bitmapbut .Create ("",WS_CHILD | WS_VISIBLE |
BM_SETSTATE, BM_GETSTATE, etc. BS_OWNERDRAW, rect, this,
BITMAP_BUTTON);
? Corresponding CButton member functions: m_bitmapbut .LoadBitmap (IDB_BMUP,
– SetCheck( ), GetCheck( ), SetState( ), GetState( ) IDB_BMDOWN, 0, 0);
? Program examples: button, button_mfc ? Program Example: button_bitmap_mfc
List Box Controls
? Lots of styles: see on-line help on LBS_
– LBS_STANDARD very common
• can send messages to parent
? Program communicates with list box by sending it
messages; some common button messages:
– LB_RESETCONTENTS, LB_ADDSTRING,
LB_GETCURSEL, LB_GETTEXT, LB_DELETESTRING
? Some List Box Notification codes:
– LBN_SELCHANGE, LBN_DBLCLK
? Combo boxes much like list boxes (CBS_, CB_, CBN_)
? Program examples: listbox, combo