Creating a Floating Window



A window is referred to as floating when it can be positioned anywhere on the screen.


Any application that displays on the screen without being maximized is a floating window. The first attribute of a floating window is that it must have a parent. An application that displays on the screen has the desktop window as its parent. The second attribute of a floating window is that, since it has a parent, when the parent gets closed or destroyed, the floating window also gets closed or destroyed.

You can create a window that has a parent of your application. In fact every "physical" object that is part of your application as a parent. Like a non-maximized application presents a floating window, you can create a window as part of your application and make it float anywhere on the screen as long as its parent is available. That is the case for toolbars, mini frames, and dialog bars, etc.

Creating the Application

In this small how-to topic, we will create an application that uses the document/view architecture to create a framed window. Then we will add a floating dialog box to it. All of that is easy as long as you know how to add a dialog box to an application. The main trick we will need to make sure that only one instance of the floating window can be displayed at one given time.


Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++ and create a new project named Floating1
  2. Create the project as a Single Document
  3. To add a dialog box, display the Insert Resource dialog box. Click Dialog and click New
  4. On the dialog, delete the TODO line, the OK and the Cancel buttons
  5. Change the ID of the dialog box to IDD_FLOATER_DLG and change its Caption to Floater (optionally, you can make it a tool window by checking its Tool Window check box or setting it to True)
  6. Add a class for the dialog box. Name it CFloaterDlg and make sure it is based on CDialog
  7. To make sure that only one instance of the floating window can be displayed at a time, we will prevent the user from closing the dialog box. To do this, access the WM_CLOSE message of the dialog box and implement its OnClose event as follows:
    void CFloaterDlg::OnClose() 
    	// TODO: Add your message handler code here and/or call default
    	// If the user attempts to close the dialog box, we will instead hide it
  8. Now that the user cannot inherently close the dialog box, we will control its availability.
    To start, in the header file of the CMainFrame class, declare a pointer to CFloaterDlg:
    // MainFrm.h : interface of the CMainFrame class
    . . .
    #include "FloaterDlg.h"
    class CMainFrame : public CFrameWnd
    protected: // create from serialization only
    // Attributes
    	CFloaterDlg *Floater;
    // Operations
    . . .	
  9. Next, use the OnCreate() event of the CMainFrame class to formally create the floating window:
    	Floater = new CFloaterDlg;
    	delete Floater;
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    		TRACE0("Failed to create toolbar\n");
    		return -1;      // fail to create
    	if (!m_wndStatusBar.Create(this) ||
    		TRACE0("Failed to create status bar\n");
    		return -1;      // fail to create
    	Floater->Create(IDD_FLOATER_DLG, this);
    	return 0;
  10. Finally, to allow the user to display the floating window, open the IDR_MAINFRAME menu. Add a separator under the Status Bar menu item. Add new menu item with a caption as &Floater and the Checked property set to True or checked
  11. Associate the COMMAND message of the Floater menu item with the CMainFrame class and initiate its message
  12. Implement its event as follows:
    void CMainFrame::OnViewFloater() 
    	// TODO: Add your command handler code here
    	CMenu *pMenu;
    	// Get a pointer to the menu
    	pMenu = GetMenu();
    	// Find out the state of the Floater menu item
    	// Is it checked?
    	if( pMenu->GetMenuState(ID_VIEW_FLOATER, MF_CHECKED) )
    		// If it is checked, hide the dialog box
    		// and uncheck the Floater menu item
    		pMenu->CheckMenuItem(ID_VIEW_FLOATER, MF_UNCHECKED);
    		pMenu->CheckMenuItem(ID_VIEW_FLOATER, MF_CHECKED);
  13. Test the application
  14. After using it, close it and return to your programming environment.

Home Copyright 2003-2005 FunctionX, Inc. FunctionX