- Start Microsoft Visual C++ and create a new project named Floating1
- Create the project as a Single Document
- To add a dialog box, display the Insert Resource dialog box. Click
Dialog and click New
- On the dialog, delete the TODO line, the OK and the Cancel buttons
- 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)
- Add a class for the dialog box. Name it CFloaterDlg and make sure it
is based on CDialog
- 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
ShowWindow(FALSE);
CDialog::OnClose();
}
|
- 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
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)
// Attributes
public:
CFloaterDlg *Floater;
// Operations
public:
. . .
};
|
- Next, use the OnCreate() event of the CMainFrame class to formally
create the floating window:
CMainFrame::CMainFrame()
{
Floater = new CFloaterDlg;
}
CMainFrame::~CMainFrame()
{
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
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
Floater->Create(IDD_FLOATER_DLG, this);
return 0;
}
|
- 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
- Associate the COMMAND message of the Floater menu item with the
CMainFrame class and initiate its message
- 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
Floater->ShowWindow(SW_HIDE);
// and uncheck the Floater menu item
pMenu->CheckMenuItem(ID_VIEW_FLOATER, MF_UNCHECKED);
}
else
{
Floater->ShowWindow(SW_SHOWNORMAL);
pMenu->CheckMenuItem(ID_VIEW_FLOATER, MF_CHECKED);
}
}
|
- Test the application
- After using it, close it and return to your programming environment.
|