310 likes | 659 Views
Child and Popup Windows , Dialog Boxes. Child Windows . sometimes we need to create custom windows The most common type of custom window is called a child window attached to its parent only visible if its parent is visible always on top of its parent
E N D
Child Windows • sometimes we need to create custom windows • The most common type of custom window is called a child window • attached to its parent • only visible if its parent is visible • always on top of its parent • If the parent is destroyed, the child window also is destroyed.
Child windows usage • to deal with a specific task, e.g., getting input from the user • programming tool to break up a large screen area into smaller portions , each with its own message- processing function.
Creating a child window • Register a new window class for the child (RegisterClass()). • Create the child window using CreateWindow() with WS_CHILD style. • Write a separate message-processing function for the child window.
Sending messages to a child window • We can use SendMessage() to send messages to a child window • We need to specify the child window's handle and the message ID with its parameters • Frequently the message ID that is used is WM_USER • defined in Windows.h as a number which is greater than any of the predefined Windows messages
USER messages • We can use WM_USER and above to define messages for any type of activity • For example • #define WM_MYKILLCHILD WM_USER /* tell child window to vanish */ • #define WM_MYMAXCHILD WM_USER+1 /* tell child window to maximize */ • #define WM_MYMINCHILD WM_USER+2 /* tell child window to minimize */
Child Window Example • child.cpp
Popup Windows • a type of child window that is not physically attached to its parent • can be positioned anywhere on screen • handy if the user needs to move things around on the screen • ideal for applications that have multiple independent sections, such as a communications program which supports a number of simultaneous terminal sessions
Creating Popup Windows • Popup windows are created with CreateWindow() • The WS_POPUP style (which is mutually exclusive with WS_CHILD) is used • Coordinates are interpreted as screen coordinates, not as client-area coordinates of the parent window
Dialog Boxes • Popup child windows created by Windows • Used for special-purpose input & output • Principal I/O mechanism in Windows • Contain several child window controls • Layout & what it does is are predefined(a resource template) • How it does is determined by a "Dialog box procedure“Destroyed immediately after use
Rule of Thumb: Dialog Boxes vs.programmer-defined child windows • Dialog box: For simple popup windows thatuse normal window controls and do littlepainting on the client area • Popup/child windows: Use when extensivepainting or non-standard behavior needed • Main advantage of dialog boxes: • Ease of construction with the dialog box editor • Ease of communicating with its controls
Steps in using dialogs: • 1. Set up the template in the resources(.rc file) • Specifies controls used, their style/layout • Can be prepared "visually" with VisualStudio dialog box editor • Or "manually" with a text editor
Steps in using dialogs: • 2. Write the dialog box procedure • Code to carry out dialog box's tasks • Placed in .cpp file • Provides message-processing capability • Messages from controls handled inside thisprocedure • Messages can be sent to the dialog box • A callback function like main window procedure WndProc() • But not the same • Part of the callback is inside Windows • It interprets some keystrokes (tab) • It calls our procedure
Types of Dialog Boxes • Modal • Modeless
Modal • While visible, user can't switch back to parentwindow • (Can change to other apps) • User must explicitly end dialog box • Typically by clicking "OK" or "CANCEL" buttonsinside • Most common type of dialog box • Example: "About" box available with mostWindows apps • Message Boxes are simple Modal Dialog Boxes
Modeless • User can switch between dialog box andthe parent window • More like popup windows • Used when dialog box must be visiblewhile user interacts with the parent • Example: dialog box resulting from"Find" or "Replace" menu itemof many Windows apps
Steps in designing, creating, using a Modal Dialog Box: 1. Determine child window controls needed inside 2. Design dialog box template (easiest with dialogbox editor) 3. Write message-processing function 4. Activate dialog box by calling DialogBox() • Typically in response to menu item selection inWndProc() 5. Resulting dialog box stays on screen until callto EndDialog() • from inside dialog box function
DialogBox() • Parameters: • 1. App's instance handle • 2. Dialog box ID name • Specified in dialog box template when .rc file created • 3. Handle of dialog box's parent window • 4. Address of dialog box function that will process itsmessages • A callback function
DialogBox() • Creates modal dialog box from app's dialog boxtemplate resources • Displays dialog box & switches msg-processing to it • Control returned when its msg-processing functionterminates dialog box • By calling EndDialog() ;
WM_INITDIALOG Message • Likean ordinary window'sWM_CREATE message • Processed before window (dialog box) ismade visible • Good place to put dialog boxinitialization code
EndDialog() • Destroys dialog box • Returns control to function (WndProc()) thatcalled DialogBox() • Parameters: • 1. window handle passed to dialog box function(hDlg) • 2. integer value returned by DialogBox() • Way of getting info from dialog box function tocalling program
User Interaction with DialogBox Controls • WM_COMMAND message • LOWORD(wParam) contains control ID (asusual) • lParam, wParam contain message data (asusual)
Exchanging Data with a Dialog Box • Exchanging data between dialog box functionand app's WndProc() • SendMessage() could be used to send messageto control inside, BUT: • Need to know control's handle • Not known since Windows creates the controls • IDs are known--specified in resource template • Use GetDlgItem() to get control's handle: • hControl = GetDlgItem(hDlg, controlID ); • Then SendMessage(hControl, Msg, wParam,lParam);
Exchanging Data with a Dialog Box (cont’d) • Both functions can be combined usingSendDlgItemMessage(): • SendDlgItemMessage(hDlg, controlID,Msg, wParam, lParam); • Sends Msg to control whose ID is controlID
Example • dlg2.cpp
Common dialog boxes • a library of functions that invoke standard dialog boxes • opening/saving files • searching/replacing text • choosing colors • choosing fonts • getting/changing information on printers
How do they work? • A single function from the library is called, instead of having to set up a .rc template and write a dialog box procedure • For each common dialog box, COMMDLG.DLL provides the standard template and the default dialog box procedure which processes messages from the dialog box controls.
Steps in Setting Up and Using a Common Dialog box • Initialize the fields of a structure with data needed by the dialog box. • Set up a pointer to a structure in the library function. • Call the library function, passing it the pointer. • The library function creates/displays the dialog box. • The user interacts with the dialog box and causes it to be removed. • The library function returns control to the application. • The application retrieves the information returned in the structure.
Some Common Dialog Box functions • ChooseColor(CHOOSECOLOR* cc) • ChooseFont(CHOOSEFONT* cf) • GetOpenFileName(OPENFILENAME* ofn) • GetSaveFileName(OPENFILENAME* ofn) • FindText(FINDREPLACE* ft)