相关类:
CToolBarCtrl - 父类是 CWnd 封装了工具栏控件相关操作相关类:
CStatusBar - 父类是 CControlBar,封装了状态栏的创建和各种操作提供一个显示数据的窗口,与用户进行交互
相关类:CView 父类为 CWnd在CView中,如果有消息调用OnPaint(),则不调用 OnDraw(),建议只写OnDraw。
工具栏,状态栏使用:
#include
#include
cpp文件代码:
// 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;
}
视图窗口使用代码:
// 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;
}