Child Window Controls: Scroll Bars, List Boxes, Combo Boxes, Edit Controls Messages from Controls Scroll Bar Notification Codes

Child Window Controls: Scroll Bars, List Boxes, Combo Boxes, Edit Controls Messages from Controls Scroll Bar Notification Codes

Messages from Controls Child Window Controls: ? Most work as follows: – User interacts with the control Scroll Bars, List Boxes, – WM_COMMAND message sent to parent Combo Boxes, Edit Controls window – LOWORD(wParam) = Control ID – lParam = control’s window handle – HIWORD(wParam) = notification code • identifies what the user action was ? Scroll Bars are a bit different Scroll Bar Controls Scroll Bar Notification Codes ? User interacts with a scroll bar – WM_HSCROLL or WM_VSCROLL message – Not WM_COMMAND as for other controls • lParam=scroll bar window handle (for stand-alone) • lParam=0 (for attached scroll bar) • LOWORD(wParam)=notification code: user action – SB_LINEUP (up/left arrow pressed) – SB_PAGEUP (scroll area above/left of “thumb”) – SB_LINEDOWN (down/left arrow pressed) – SB_PAGEDOWN (scroll area beneath/right of “thumb”) – SB_THUMBTRACK (scroll “thumb” pressed) – SB_THUMBPOSITION (scroll “thumb” released) – For either, HIWORD(wParam)=current thumb position ? Lots of Scroll bar styles when creating it CScrollbar Class for Standalones – See online help on SBS_ ? In Create() member function, include SB_HORZ – Default alignment for attached scroll bar: right side and bottom of window or SB_VERT style ? Some Useful Scrollbar Functions: ? Make calls to member functions: – GetScrollPos()--retrieve current position of thumb – SetScrollPos(), SetScrollRange(), ShowScrollBar(), GetScrollRange()--Retrieve min/max value range etc. SetScrollPos()--Set position of thumb ? Include ON_WM_HSCROLL or SetScrollRange()--Set min/max value range ON_WM_VSCROLL message mapping macros ShowScrollBar()--Display scroll bar ? Override Handler, e.g.: • 1st params : hWnd or hScrollBar void OnHScroll (UINT nCode, UINT nPos, CScrollBar* • 2nd param: SB_CTL (standalone) or pSBar); SB_VERT/SB_HORZ (attached scroll bar) • nCode= SB_*** notification code (user action) • Others: position, range (2 values), etc…, visibility flag • nPos=latest thumb position for drags/releases • pSBar: pointer to the scroll bar (NULL if attached) 1 The SCROLL1 Example The SCROLL2 Example ? Win32 API Application ? Stand-alone scrollbar allows user to enter an integer value between 0 and 50 ? Scroll Bar Attached to a Window ? Current value is continually displayed in a ? Creates a window with a vertical scroll bar static control ? Puts 3 lines of text in client area ? See Scroll1 code on Example Programs web ? User can scroll through the client area using scroll page for Win32 API version bar ? See Scroll1_mfc on Example Programs web – Opposite direction from “normal” scrolling page for MFC version ? See Scroll2 code on Example Programs web page Attached Vertical Scroll Bar in List Box Controls Doc/View MFC Apps ? Lots of styles: see on-line help on LBS_ – LBS_STANDARD very common ? Override View class’s OnCreate(...) member • can send messages to parent function to set range and position of vertical ? Program communicates with list box by sending it messages; scroll bar some common button messages: ? Use Class Wizard to add: – LB_RESETCONTENTS, LB_ADDSTRING, LB_GETCURSEL, ON_WM_VSCROLL() message mapping LB_GETTEXT, LB_DELETESTRING macro and OnVScroll(...) handler function in ? Some List Box Notification codes: View class: – LBN_SELCHANGE, LBN_DBLCLK – void OnVScroll(UINTnSBCode, UINTnPos, ? Combo boxes much like list boxes (CBS_, CB_, CBN_) CScrollBar* pScrollBar); ? MFC: Add to message map and add handler: • ON_LBN_*** ( id, memberFtn ) – Add switch/case statements to handle SB_codes of interest…in OnVScroll() handler function • void memberFtn( ); ? See Scroll2_mfc Example Program ? Program examples: listbox, combo EDIT CONTROLS ? For viewing and editing text Edit Control Styles ? Current location kept track of with a "carat” – A small vertical line ? Some common styles – ES_LEFT, ES_CENTER, ES_RIGHT, ? Backspace, Delete, arrow keys, highlighting ES_MULTILINE, ES_AUTOVSCROLL, work as expected ES_PASSWORD ? Scrolling possible (use WS_HSCROLL, • See Online Help on “Edit Styles” WS_VSCROLL styles ? No ability to format text with different fonts, sizes, character styles, etc. – Use Rich Edit Control for this 2 Edit Control Text Edit Control Messages ? User interacts with edit control, ? Text in an edit control stored as one long – WM_CONTROL message to parent character string – LOWORD(wParam) = Control ID – lParam= control’s window handle ? Carriage return <CR> is stored as ASCII – HIWORD(wParam) = EN_** notification code code (0x0D,0x0A) • identifies what the user action was • e.g., EN_CHANGE ? <CR> inserted automatically if a line • See Online Help EN_*** doesn’t fit and wraps ? MFC: Add to message map and add handler: • ON_EN_*** ( id, memberFtn ) ? NULL character inserted only at end of last • void memberFtn( ); line of text Sending Messages to an Edit Box MFC’s CEdit Class ? Some important member/inherited functions ? As with other controls use SendMessage() – SetWindowText(LPSTR) ? Some important messages • Sends a WM_SETTEXT message – EM_GETLINECOUNT(multiline edit boxes) • Copy text in buffer to the control • Returns number of lines in the control • Replaces current contents – EM_GETLINE: Copy a line to a buffer • Parameter could be a CString – EM_LINEINDEX: Get a line’s character index – GetWindowText(LPSTR) • Number of characters from the beginning of • Sends a WM_GETTEXT message edit control to start of specified line • Copies all lines in the control to the specified buffer – EM_LINELENGTH to get length of line • Parameter could be a CString ? See Edit1 example program – Lots of others, see Online Help on CEdit Child Window Child and Popup Windows ? Most common type of custom window ? Always attached to parent window ? Child Window Controls are predefined – Always on top of parent window controls (buttons, static text, etc.) – Parent minimized ? child disappears – These are examples of child windows – Reappears when parent restored ? OK if controls have exact features required – Parent destroyed ? child also destroyed ? But sometimes we need custom child ? Used to deal with a specific task windows – e.g., getting user input – Where we can have a WndProc() that does ? Each has its own message-processing exactly what we want it to WndProc function 3 Creating and Using a Child Popup window Window – Win32 API ? 1. Register a new window class for child ? Same general properties as child window, using RegisterClass() but: – Could be done in WinMain(() or when needed ? Not physically attached to parent in WndProc() ? Can be positioned anywhere on screen ? 2. Create child window using ? Handy if the user needs to move things CreateWindow() around on client area – Should have WS_CHILD style ? 3. Write separate message-processing function for child window Sending Messages to a Child WM_USER Messages Window ? Defined in Windows.h as a number not used by predefined messages ? Use SendMessage() and specify: ? All higher numbers also unused by Windows – Child window's handle ? Can use WM_USER + # for any type of activity • Obtained when the child window was created ? Example--could have a header file containing: – Message ID & parameters #define WM_MYKILLCHILD WM_USER // tell child window to vanish #define WM_MYMAXCHILD WM_USER+1 // tell child window to maximize ? Use in child's WndProc() function’s switch/case ? Child windows can send messages to parent or to other child windows CHILD EXAMPLE PROGRAM Details of CHILD Application ? User clicks "Create" menu item? ? 1. Register Child Window Class with – Child window appears with "Destroy Me" RegisterClass() button and some text – Message processing function: ChildProc() ? User clicks "Send Message" menu item? – ChildProc()will receive messages from any windows based on this class – Caption on child window changes – Class Icon: IDI_APPLICATION icon ? User clicks "Destroy Me" button in child – Cursor shape: Load standard IDC_CROSS cursor window? – Background: LTGRAY_BRUSH background brush – Menu: None to be used here – Child window disappears ? 2. Create Child Window using CreateWindow() ? Both parent and child window have a line of text displayed in client areas 4 ? 3. Menu item response – User clicks "Create" menu item Sending Messages (WM_COMMAND, IDM_CREATE)? • Program's WndProc()executes: ? In Main Window’s WndProc() if(!hChild) – User clicks "Send Message" menu item? hChild = CreateWindow ("ChildClass", "Child Window", WS_CHILD | – WndProc() uses SendMessage() to send a WS_THICKFRAME | WS_MINIMIZEBOX | WM_USER msg to child window WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU, 10, 30, 200, 150, hWnd, NULL, hInstance, NULL); – Logic allows only one child window at a time ? In Child’s ChildProc() – ChildProc()'s response to WM_USER from parent: ? Main Window’s WndProc()'s response to • Uses SetWindowText() to set its caption bar this (WM_USER+1): – Response to creation (WM_CREATE): – Set hChild to NULL so another child can be • CreateWindow() to create a "Destroy Me" pushbutton created • 3-deep nesting of windows: Parent (main window), Child – WndProc() also responds to expose events Window, Button Control (WM_COMMAND) by outputting a line of text – Response to expose event (WM_PAINT): to main window's client area • Output a line of text to child window client area – So text in both windows is visible whenever either is exposed – Response to user clicking button (WM_COMMAND): • Use GetParent(hChild) to get the parent's window handle • Destroys itself with a call to DestroyWindow(hChild) • Send USER+1 message to parent Child Windows -- MFC Child_mfc Example 1. Register window class with: AfxRegisterWndClass( UINTnClassStyle,

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us