相關類:數組
CToolBarCtrl - 父類是 CWnd 封裝了工具欄控件相關操做相關類:app
CStatusBar - 父類是 CControlBar,封裝了狀態欄的建立和各類操做提供一個顯示數據的窗體,與用戶進行交互框架
相關類:CView 父類爲 CWnd在CView中,假設有消息調用OnPaint()。則不調用 OnDraw(),建議僅僅寫OnDraw。函數
工具欄,狀態欄使用:工具
#include <afxwin.h>
#include <AFXEXT.H>
ui
cpp文件代碼:this
// MFCtoolbar.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" UINT g_hIndicator[]={ 0,IDS_TIME,IDS_POS }; class CMyFrameWnd:public CFrameWnd { DECLARE_MESSAGE_MAP() public: CToolBar toolbar; CMenu menu; CStatusBar statusbar; afx_msg int OnCreate(LPCREATESTRUCT lpc); afx_msg void OnNew(); afx_msg void ShowToolBar(); afx_msg void OnTimer(UINT uid); afx_msg void OnMouseMove(UINT id, CPoint pt); }; void CMyFrameWnd::OnMouseMove(UINT id, CPoint pt) { CString str; str.Format("x=%d,y=%d",pt.x,pt.y); statusbar.SetPaneText(2,str); } void CMyFrameWnd::OnTimer(UINT uid) { SYSTEMTIME st={0}; ::GetLocalTime(&st); CString str; str.Format("%d-%d-%d %d:%d:%d",st.wYear,st.wMonth,st.wDay, st.wHour,st.wMinute,st.wSecond); statusbar.SetPaneText(1,str,TRUE); } BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd) ON_WM_CREATE() ON_COMMAND(IDM_new,OnNew) ON_COMMAND(IDM_toolbar,ShowToolBar) ON_WM_TIMER() ON_WM_MOUSEMOVE() //ON_COMMAND_RANGE(IDM_new,ID_blue,OnNew) END_MESSAGE_MAP() void CMyFrameWnd::OnNew() { AfxMessageBox("CMyFrameWnd::OnNew"); } void CMyFrameWnd::ShowToolBar() { this->ShowControlBar(&toolbar,!toolbar.IsWindowVisible(),FALSE); menu.CheckMenuItem(IDM_toolbar,MF_BYCOMMAND|toolbar.IsWindowVisible()?MF_CHECKED:MF_UNCHECKED); } int CMyFrameWnd::OnCreate(LPCREATESTRUCT lpc) { toolbar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_ALIGN_TOP |CBRS_GRIPPER|CBRS_SIZE_DYNAMIC|CBRS_TOOLTIPS|CBRS_FLYBY); toolbar.LoadToolBar(IDR_TOOLBAR1); toolbar.SetWindowText("工具欄"); toolbar.EnableDocking(CBRS_ALIGN_ANY); this->EnableDocking(CBRS_ALIGN_ANY); this->DockControlBar(&toolbar,AFX_IDW_DOCKBAR_TOP); menu.LoadMenu(IDR_MENU1); SetMenu(&menu); statusbar.CreateEx(this,TBSTYLE_FLAT); statusbar.SetIndicators(g_hIndicator,sizeof(g_hIndicator)/sizeof(UINT)); statusbar.SetPaneInfo(1,IDS_TIME,SBPS_NORMAL,200); statusbar.SetPaneInfo(2,IDS_POS,SBPS_NORMAL,100); ::SetTimer(this->m_hWnd,1,1000,NULL); return CFrameWnd::OnCreate(lpc); } class CMyWinApp:public CWinApp { public: virtual BOOL InitInstance(); }; CMyWinApp theApp; BOOL CMyWinApp::InitInstance() { CMyFrameWnd *pFrame=new CMyFrameWnd; pFrame->Create(NULL,"TOOLBAR"/*,WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault,NULL,MAKEINTRESOURCE(IDR_MENU1)*/); m_pMainWnd=pFrame; pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); return TRUE; }
視圖窗體使用代碼:spa
// MFCview.cpp : Defines the entry point for the application. // #include "stdafx.h" class CMyView:public CView { public: virtual void OnDraw(CDC* pDC); }; void CMyView::OnDraw(CDC* pDC) { pDC->TextOut(100,100,"CMyView::OnDraw"); } class CMyFrameWnd:public CFrameWnd { DECLARE_MESSAGE_MAP() public: afx_msg void OnPaint(); afx_msg int OnCreate(LPCREATESTRUCT lpc); }; BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd) ON_WM_PAINT() ON_WM_CREATE() END_MESSAGE_MAP() int CMyFrameWnd::OnCreate(LPCREATESTRUCT lpc) { CMyView * pView=new CMyView; pView->Create(NULL,"myview",WS_CHILD|WS_VISIBLE,CRect(0,0,200,200),this, AFX_IDW_PANE_FIRST); m_pViewActive=pView;//設置爲活躍視圖 //AFX_IDW_PANE_FIRST 爲第一個視圖窗體ID return CFrameWnd::OnCreate(lpc); } void CMyFrameWnd::OnPaint() { PAINTSTRUCT ps={0}; HDC hdc=::BeginPaint(m_hWnd,&ps); ::TextOut(hdc,100,100,"hello",5); ::EndPaint(m_hWnd,&ps); } class CMyWinApp:public CWinApp { public: virtual BOOL InitInstance(); }; CMyWinApp theApp; BOOL CMyWinApp::InitInstance() { CMyFrameWnd *pFrame=new CMyFrameWnd; pFrame->Create(NULL,"MFCview"); m_pMainWnd=pFrame; pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); return TRUE; }