VC++ Workshop Session 5 R. Eckert Child and Popup Windows Predefined window controls (buttons, static text, etc.)--examples of child windows OK if controls have exact features required But sometimes custom child windows needed Child Window-- Most common type of custom window Always attached to parent window Always on top of parent Parent minimized child disappears Reappears when parent restored Parent destroyed child also destroyed Used to deal with a specific task e.g., getting user input Can be used as programming tool to break large screen area into smaller pieces Each has its own message-processing function Popup window-- Same general properties as child window, but: Not physically attached to parent Can be positioned anywhere on screen Handy if the user needs to move things around on client area Creating and Using a Child Window-- 1. Register a new window class for child using RegisterClass() Could be done in WinMain(() or when needed in WndProc() 2. Create child window using CreateWindow() Should have WS_CHILD style 3. Write separate message-processing function for child window Sending Messages to a Child Window-- Use SendMessage() and specify: Child window's handle Obtained when the child window was created Message ID & parameters Can use WM_USER Defined in Windows.h as a number not used by predefined messages All higher numbers also unused by Windows Can use WM_USER and above for any type of activity Example--could have a header file containing: #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 Could be used in child's msg-processing function switch statement Child windows can send messages to parent or to other child windows THE CHILD EXAMPLE PROGRAM--User clicks "Create" menu item Child window appears with "Destroy Me" button and some text User clicks "Send Message" menu Caption on child window changes User clicks "Destroy Me" button in child window Child window disappears Both parent and child window have a line of text displayed in client areas Registering Child Window Class with RegisterClass()-- Message processing function: ChildProc() Will receive messages from any windows based on this class Class Icon: LoadIcon() loads standard IDI_APPLICATION icon When child minimized, that icon will appear inside parent window Cursor shape: LoadCursor() loads standard IDC_CROSS cursor Background: LTGRAY_BRUSH background brush is used Menu: None will be used in child windows based on this class Creating the Child Window with CreateWindow()-- User clicks "Create" menu item (WM_COMMAND, IDM_CREATE) Program's WndProc() executes: if(!hChild) hChild = CreateWindow ("ChildClass", "Child Window", WS_CHILD | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU, 10, 30, 200, 150, hWnd, NULL, hInstance, NULL); Logic allows only one child window at a time Window style flags Child window is sizable, iconifiable, maximizable, movable Has its own system menu button Sending a Message to the Child Window-- User clicks "Send Message" menu item WndProc() uses SendMessage() to send a WM_USER msg to child window ChildProc()'s response to WM_USER msg from parent: Uses SetWindowText() to set its caption bar to "Got message from parent" Other ChildProc() Actions-- Response to creation: uses CreateWindow() to create a "Destroy Me" pushbutton 3-deep nesting of windows: Parent (main window) Child window Button Control Response to expose events: outputs a line of text to child window client area Response to user clicking the pushbutton: Uses GetParent(hChild) to get the parent's window handle Destroys itself with a call to DestroyWindow(hChild) Sends a USER+1 msg to parent using handle obtained from GetParent() WndProc()'s response to this: Sets hChild to NULL so another child can be created WndProc() also responds to expose events by outputting a line of text to main window's client area So text in both windows is visible whenever either is exposed POPUP WINDOWS-- Main distinction--not restricted to the parent window's client area Can appear anywhere on screen Handy for small utility programs, e.g.: Window that shows current cursor position in a painting program Ideal for applications with multiple independent sections, e.g.: Communications pgm with simultaneous terminal sessions in different popup windows Create with CreateWindow() WS_POPUP style (mutually exclusive with WS_CHILD) Coordinates are screen coordinates (relative to upper lefthand corner of screen) The Big Picture-- Child window behavior Main program window behavior ------------------------------- = ----------------------------------------- Parent window behavior Windows desktop behavior Windows desktop is just another window (lacking title bar and border) All running main program windows behave like children of desktop window Using same code for main program windows and child windows Keeps Windows system small Gives the environment a consistent behavior