Child and Popup Windows, Dialog Boxes - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Child and Popup Windows, Dialog Boxes

Description:

Register a new window class for the child (RegisterClass ... Steps in Using: 1. Set up the template in the resources (.rc file) ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 31
Provided by: conest
Category:

less

Transcript and Presenter's Notes

Title: Child and Popup Windows, Dialog Boxes


1
Child and Popup Windows, Dialog Boxes
2
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.

3
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.

4
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.

5
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

6
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_USER1  / tell child
    window to maximize /
  • define WM_MYMINCHILD    WM_USER2  / tell child
    window to minimize /

7
Child Window Example
  • child.cpp

8
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

9
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

10
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 are predefined (template
    --a resource)
  • How it does is determined by a "Dialog box
    procedure Destroyed immediately after use

11
Rule of Thumb Dialog Boxes vs.programmer-defined
child windows
  • Dialog box For simple popup windows that use
    normal window controls and do little painting on
    the client area
  • Popup/child windows Use when extensive painting
    or nonstandard behavior needed
  • Main advantage of dialog boxes
  • Ease of construction with the dialog box editor
  • Ease of communicating with its controls

12
Steps in Using
  • 1. Set up the template in the resources (.rc
    file)
  • Specifies controls used, their style/layout
  • Can be prepared "visually" with Visual Studio
    dialog box editor
  • Or "manually" with a text editor

13
Steps in Using
  • 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 this
    procedure
  • 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

14
Types of Dialog Boxes
  • Modal
  • Modeless
  • System Modal

15
Modal
  • While visible, user can't switch back to parent
    window
  • (Can change to other apps)
  • User must explicitly end dialog box
  • Typically by clicking "OK" or "CANCEL" buttons
    inside
  • Most common type of dialog box
  • Example "About" box available with most Windows
    apps
  • Message Boxes are simple Modal Dialog Boxes

16
System Modal
  • A variety of modal dialog box
  • With these user can't switch to other
    applications while dialog box is active
  • A throwback to Win16

17
Modeless
  • User can switch between dialog box and the parent
    window
  • More like popup windows
  • Used when dialog box must be visible while user
    interacts with the parent
  • Example dialog box resulting from "Find" or
    "Replace" menu item of many Windows apps

18
Steps in Designing, Creating, Using a Modal
Dialog Box
  • 1. Determine child window controls needed inside
  • 2. Design dialog box template (easiest with
    dialog box editor)
  • 3. Write message-processing function
  • 4. Activate dialog box by calling DialogBox()
  • Typically in response to menu item selection in
    WndProc()
  • 5. Resulting dialog box stays on screen until
    call to EndDialog()
  • from inside dialog box function

19
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 its messages
  • A callback function

20
DialogBox()
  • Creates modal dialog box from app's dialog box
    template resources
  • Displays dialog box switches msg-processing to
    it
  • Control returned when its msg-processing function
    terminates dialog box
  • By calling EndDialog()

21
WM_INITDIALOG Message
  • Like an ordinary window's WM_CREATE message
  • Processed before window (dialog box) is made
    visible
  • Good place to put dialog box initialization code

22
EndDialog()
  • Destroys dialog box
  • Returns control to function (WndProc()) that
    called 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 to
    calling program

23
User Interaction with DialogBox Controls
  • WM_COMMAND message
  • LOWORD(wParam) contains control ID (as usual)
  • lParam, wParam contain message data (as usual)

24
Exchanging Data with a Dialog Box
  • Exchanging data between dialog box function and
    app's WndProc()
  • SendMessage() could be used to send message to
    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)

25
Exchanging Data with a Dialog Box (contd)
  • Both functions can be combined using
    SendDlgItemMessage()
  • SendDlgItemMessage(hDlg, controlID, Msg, wParam,
    lParam)
  • Sends Msg to control whose ID is controlID

26
Example
  • dlg2.cpp

27
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

28
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.

29
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.

30
Some Common Dialog Box functions
  • ChooseColor(CHOOSECOLOR cc)
  • ChooseFont(CHOOSEFONT cf)
  • GetOpenFileName(OPENFILENAME ofn)
  • GetSaveFileName(OPENFILENAME ofn)
  • FindText(FINDREPLACE ft)
Write a Comment
User Comments (0)
About PowerShow.com