Add the toolbar status bar to the MFC Frame

Source: Internet
Author: User

Menu Bar toolbar Status Bar
Menu Bar
CMenu encapsulates the menu handle and related menu APIs

1.1 add resources

1.2 Add a menu in the CMainFrame: OnCreate function; CMenu: Attach -- Attach the menu handle to the menu object; CMenu: Detach -- Detach the menu handle from the menu object

1.3 command-related ON_COMMAND

1.4 The status ON_UPDATE_COMMAND_UI CCmdUI class of the menu item

Toolbar
Tool bar related class CToolBarCtrl -- the parent class is CWnd, which encapsulates information about the Toolbar Control and APIs. CToolBar -- parent class CControlBar, which encapsulates the relationship between ToolBar and CFrameWnd.

2.1 Create a toolbar CToolBar: Create/Ex

2.2 load Toolbar

2.3 The CToolBar: EnableDocking of the toolbar allows you to dock. The CFrameWnd: EnableDocking setting window allows you to dock. The CFrameWnd: DockControlBar sets the position of the dock.

2.4 command processing

2.5 display and hide of the toolbar CFrameWnd: ShowControlBar

2.6 The style CBRS_TOOLTIPS is added to the information prompt; the prompt information is set.

Status Bar
Status Bar-related class CStatusBarCtrl -- the parent class is CWnd, which encapsulates information about StatusBars and APIs. CStatusBar-the parent class CControlBar, which encapsulates the relationship between the status bar and the window.

3.1 Create status bar CStatusBar: Create/Ex

3.2 set the status bar indicator CStatusBar: SetIndicators

3.3 set or obtain the status bar information CStatusBar: SetPaneText, CStatusBar: GetPaneText

Instance
[Cpp]
# Include "stdafx. h"
# Include "Resource. h"
UINT g_nIndicators [] =
{
0,
IDS_TIME,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL
};
Class CMainFrame: public CFrameWnd
{
Public:
DECLARE_MESSAGE_MAP ()
Afx_msg int OnCreate (maid); // menu bar, toolbar, status bar creation and Loading
// Menu bar Message Processing Function
Afx_msg void OnExit ();
Afx_msg void OnOpen ();
Afx_msg void OnUpdateUIOpen (CCmdUI * pCmdUI );
// Toolbar Message Processing Function
Afx_msg void OnViewStandard ();
Afx_msg void OnUpdateUIStandard (CCmdUI * pCmdUI );
// Status bar Message Processing Function
Afx_msg void OnTimer (UINT nIDEvent );
Afx_msg void OnMouseMove (UINT nFlags, CPoint point );
Private:
CToolBar mWndToolBar;
CStatusBar mWndStatusBar;
};
BEGIN_MESSAGE_MAP (CMainFrame, CFrameWnd)
ON_WM_CREATE ()
ON_COMMAND (ID_OPEN, OnOpen)
ON_COMMAND (ID_EXIT, OnExit)
ON_UPDATE_COMMAND_UI (ID_OPEN, OnUpdateUIOpen)
ON_COMMAND (ID_VIEW_STANDARD, OnViewStandard)
ON_UPDATE_COMMAND_UI (ID_VIEW_STANDARD, OnUpdateUIStandard)
ON_WM_TIMER ()
ON_WM_MOUSEMOVE ()
END_MESSAGE_MAP ()
Int CMainFrame: OnCreate (maid)
{
// Add menu
CMenu menu;
Menu. LoadMenuW (IDR_MAINFRAME); // load menu Resources
SetMenu (& menu); // set the menu to the window
Menu. Detach (); // separates the menu handle from the menu object
 
// Add a toolbar
MWndToolBar. CreateEx (this, TBSTATE_CHECKED, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_ANY | CBRS_TOOLTIPS );
MWndToolBar. LoadToolBar (IDR_MAINFRAME); // load the toolbar
// Dock the toolbar
MWndToolBar. EnableDocking (CBRS_ALIGN_ANY); // 1. Set the toolbar to allow access
EnableDocking (CBRS_ALIGN_ANY); // you can set the frame window to be docked.
DockControlBar (& mWndToolBar, AFX_IDW_DOCKBAR_TOP); // dock to the toolbar
MWndToolBar. SetWindowTextW (L "Standard toolbar"); // you can specify the title of the toolbar.
 
// Create a status bar
MWndStatusBar. CreateEx (this );
MWndStatusBar. SetIndicators (g_nIndicators, sizeof (g_nIndicators)/sizeof (UINT); // set the indicator
SetTimer (, NULL); // start the timer
Return 0;
}
Void CMainFrame: OnOpen ()
{
MessageBox (L "OnOpen ");
}
Void CMainFrame: OnExit ()
{
MessageBox (L "OnExit ");
// PostQuitMessage (0 );
}
Void CMainFrame: OnUpdateUIOpen (CCmdUI * pCmdUI)
{
PCmdUI-> SetCheck ();
PCmdUI-> SetRadio ();
PCmdUI-> SetText (L "Open ");
}
Void CMainFrame: OnViewStandard ()
{
If (mWndToolBar. IsWindowVisible ())
{
ShowControlBar (& mWndToolBar, FALSE, FALSE );
}
Else
{
ShowControlBar (& mWndToolBar, TRUE, FALSE );
}
}
Void CMainFrame: OnUpdateUIStandard (CCmdUI * pCmdUI)
{
PCmdUI-> SetCheck (mWndToolBar. IsVisible ());
}
Void CMainFrame: OnTimer (UINT nIDEvent)
{
CTime t = CTime: GetCurrentTime ();
CString strTime = t. Format ("% Y-% m-% d % H: % M: % S ");
MWndStatusBar. SetPaneText (1, strTime );
}
Void CMainFrame: OnMouseMove (UINT nFlags, CPoint point)
{
CString strPt;
StrPt. Format (L "coordinates: X = % d, Y = % d", point. x, point. y );
MWndStatusBar. SetPaneText (0, strPt );
}
Class MFCMenu: public CWinApp
{
Public:
Virtual BOOL InitInstance ();
};
BOOL MFCMenu: InitInstance ()
{
CMainFrame * pFrame = new CMainFrame;
PFrame-> Create (NULL, L "MFCMenue ");
// PFrame-> Create (NULL, L "MFCToolbar", WS_OVERLAPPEDWINDOW, CFrameWnd: rectDefault, NULL, MAKEINTRESOURCE (IDR_MAINFRAME ));
M_pMainWnd = pFrame;
PFrame-> ShowWindow (SW_SHOW );
PFrame-> UpdateWindow ();
Return TRUE;
}
MFCMenu theApp;

# Include "stdafx. h"
# Include "Resource. h"
UINT g_nIndicators [] =
{
0,
IDS_TIME,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL
};
Class CMainFrame: public CFrameWnd
{
Public:
DECLARE_MESSAGE_MAP ()
Afx_msg int OnCreate (maid); // menu bar, toolbar, status bar creation and Loading
// Menu bar Message Processing Function
Afx_msg void OnExit ();
Afx_msg void OnOpen ();
Afx_msg void OnUpdateUIOpen (CCmdUI * pCmdUI );
// Toolbar Message Processing Function
Afx_msg void OnViewStandard ();
Afx_msg void OnUpdateUIStandard (CCmdUI * pCmdUI );
// Status bar Message Processing Function
Afx_msg void OnTimer (UINT nIDEvent );
Afx_msg void OnMouseMove (UINT nFlags, CPoint point );
Private:
CToolBar mWndToolBar;
CStatusBar mWndStatusBar;
};
BEGIN_MESSAGE_MAP (CMainFrame, CFrameWnd)
ON_WM_CREATE ()
ON_COMMAND (ID_OPEN, OnOpen)
ON_COMMAND (ID_EXIT, OnExit)
ON_UPDATE_COMMAND_UI (ID_OPEN, OnUpdateUIOpen)
ON_COMMAND (ID_VIEW_STANDARD, OnViewStandard)
ON_UPDATE_COMMAND_UI (ID_VIEW_STANDARD, OnUpdateUIStandard)
ON_WM_TIMER ()
ON_WM_MOUSEMOVE ()
END_MESSAGE_MAP ()
Int CMainFrame: OnCreate (maid)
{
// Add menu
CMenu menu;
Menu. LoadMenuW (IDR_MAINFRAME); // load menu Resources
SetMenu (& menu); // set the menu to the window
Menu. Detach (); // separates the menu handle from the menu object

// Add a toolbar
MWndToolBar. CreateEx (this, TBSTATE_CHECKED, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_ANY | CBRS_TOOLTIPS );
MWndToolBar. LoadToolBar (IDR_MAINFRAME); // load the toolbar
// Dock the toolbar
MWndToolBar. EnableDocking (CBRS_ALIGN_ANY); // 1. Set the toolbar to allow access
EnableDocking (CBRS_ALIGN_ANY); // you can set the frame window to be docked.
DockControlBar (& mWndToolBar, AFX_IDW_DOCKBAR_TOP); // dock to the toolbar
MWndToolBar. SetWindowTextW (L "Standard toolbar"); // you can specify the title of the toolbar.

// Create a status bar
MWndStatusBar. CreateEx (this );
MWndStatusBar. SetIndicators (g_nIndicators, sizeof (g_nIndicators)/sizeof (UINT); // set the indicator
SetTimer (, NULL); // start the timer
Return 0;
}
Void CMainFrame: OnOpen ()
{
MessageBox (L "OnOpen ");
}
Void CMainFrame: OnExit ()
{
MessageBox (L "OnExit ");
// PostQuitMessage (0 );
}
Void CMainFrame: OnUpdateUIOpen (CCmdUI * pCmdUI)
{
PCmdUI-> SetCheck ();
PCmdUI-> SetRadio ();
PCmdUI-> SetText (L "Open ");
}
Void CMainFrame: OnViewStandard ()
{
If (mWndToolBar. IsWindowVisible ())
{
ShowControlBar (& mWndToolBar, FALSE, FALSE );
}
Else
{
ShowControlBar (& mWndToolBar, TRUE, FALSE );
}
}
Void CMainFrame: OnUpdateUIStandard (CCmdUI * pCmdUI)
{
PCmdUI-> SetCheck (mWndToolBar. IsVisible ());
}
Void CMainFrame: OnTimer (UINT nIDEvent)
{
CTime t = CTime: GetCurrentTime ();
CString strTime = t. Format ("% Y-% m-% d % H: % M: % S ");
MWndStatusBar. SetPaneText (1, strTime );
}
Void CMainFrame: OnMouseMove (UINT nFlags, CPoint point)
{
CString strPt;
StrPt. Format (L "coordinates: X = % d, Y = % d", point. x, point. y );
MWndStatusBar. SetPaneText (0, strPt );
}
Class MFCMenu: public CWinApp
{
Public:
Virtual BOOL InitInstance ();
};
BOOL MFCMenu: InitInstance ()
{
CMainFrame * pFrame = new CMainFrame;
PFrame-> Create (NULL, L "MFCMenue ");
// PFrame-> Create (NULL, L "MFCToolbar", WS_OVERLAPPEDWINDOW, CFrameWnd: rectDefault, NULL, MAKEINTRESOURCE (IDR_MAINFRAME ));
M_pMainWnd = pFrame;
PFrame-> ShowWindow (SW_SHOW );
PFrame-> UpdateWindow ();
Return TRUE;
}
MFCMenu theApp;

Resources in the menu bar

[Cpp]
//////////////////////////////////////// /////////////////////////////////////
// Bitmap
IDR_MAINFRAME BITMAP "res \ toolbar1.bmp"
 
//////////////////////////////////////// /////////////////////////////////////
// Toolbar
 
IDR_MAINFRAME TOOLBAR 16, 15
BEGIN
BUTTON ID_OPEN
BUTTON ID_EXIT
SEPARATOR
BUTTON ID_EDIT_COPY
BUTTON ID_EDIT_PASTE
SEPARATOR
BUTTON ID_APP_ABOUT
END
//////////////////////////////////////// /////////////////////////////////////
// Menu
IDR_MAINFRAME MENU
BEGIN
POPUP "file (& F )"
BEGIN
MENUITEM "open (& O)", ID_OPEN
MENUITEM "exit (& X)", ID_EXIT
END
POPUP "View (& V )"
BEGIN
POPUP "toolbar (& T )"
BEGIN
MENUITEM "Standard (& S)", ID_VIEW_STANDARD
END
END
END
//////////////////////////////////////// /////////////////////////////////////
STRINGTABLE
BEGIN
IDS_TIME ""
END

//////////////////////////////////////// /////////////////////////////////////
// Bitmap
IDR_MAINFRAME BITMAP "res \ toolbar1.bmp"

//////////////////////////////////////// /////////////////////////////////////
// Toolbar

IDR_MAINFRAME TOOLBAR 16, 15
BEGIN
BUTTON ID_OPEN
BUTTON ID_EXIT
SEPARATOR
BUTTON ID_EDIT_COPY
BUTTON ID_EDIT_PASTE
SEPARATOR
BUTTON ID_APP_ABOUT
END
//////////////////////////////////////// /////////////////////////////////////
// Menu
IDR_MAINFRAME MENU
BEGIN
POPUP "file (& F )"
BEGIN
MENUITEM "open (& O)", ID_OPEN
MENUITEM "exit (& X)", ID_EXIT
END
POPUP "View (& V )"
BEGIN
POPUP "toolbar (& T )"
BEGIN
MENUITEM "Standard (& S)", ID_VIEW_STANDARD
END
END
END
//////////////////////////////////////// /////////////////////////////////////
STRINGTABLE
BEGIN
IDS_TIME ""
END running result:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.