C++MFC编程笔记day03 MFC工具栏、状态栏、视图窗口

MFC工具栏

相关类:

CToolBarCtrl - 父类是 CWnd  封装了工具栏控件相关操作
CToolBar - 父类是CControlBar  封装了工具栏和框架窗口之间的关系
工具栏使用:
//把工具栏对象定义为 CMyFrameWnd成员:
CToolBar toolbar;
//在窗口创建时,加载工具栏资源
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.EnableDocking(CBRS_ALIGN_ANY);//设置工具栏准备停靠的位置:
this->EnableDocking(CBRS_ALIGN_ANY);//框架窗口允许停靠的位置
this->DockControlBar(&toolbar,AFX_IDW_DOCKBAR_BOTTOM);//框架窗口设置初始停靠的位置
return CFrameWnd::OnCreate(lpc);
}
//工具栏风格:
TBSTYLE_FLAT:平滑风格
CBRS_GRIPPER:有推动把手
CBRS_SIZE_DYNAMIC:动态改变形状
CBRS_TOOLTIPS:能提示文字
CBRS_FLYBY:在状态栏显示提示




//映射消息,使用ID绑定COMMAND事件
ON_COMMAND(IDM_new,OnNew)
TOOLBAR的提示格式示例:新建\n新建,
\n前面的文字用于状态栏显示,后面的用于tooltip提示
//控制工具栏显示与隐藏:ShowControlBar(),
是否为显示状态:IsWindowVisible()
void CMyFrameWnd::ShowToolBar()
{
this->ShowControlBar(&toolbar,!toolbar.IsWindowVisible(),FALSE);
menu.CheckMenuItem(IDM_toolbar,MF_BYCOMMAND|toolbar.IsWindowVisible()?MF_CHECKED:MF_UNCHECKED);
}


MFC状态栏

相关类:

CStatusBar - 父类是 CControlBar,封装了状态栏的创建和各种操作
状态栏的使用:
创建状态栏:
CStatusBar statusbar;
statusbar.CreateEx(this,TBSTYLE_FLAT);


设置指示器:
在字符串表中加字符串:IDS_TIME,IDS_POS
全局数组:
UINT g_hIndicator[]={
0,IDS_TIME,IDS_POS
};//ID为0的为默认指示器
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);//定时器
ON_WM_TIMER()
 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);//设置文字到指示器
 }
//鼠标移动实时显示鼠标位置
ON_WM_MOUSEMOVE()
void CMyFrameWnd::OnMouseMove(UINT id, CPoint pt)
{
CString str;
str.Format("x=%d,y=%d",pt.x,pt.y);
statusbar.SetPaneText(2,str);
}

MFC 视图窗口

提供一个显示数据的窗口,与用户进行交互

  相关类:CView  父类为 CWnd
写CMyView类派生自CView
在框架的WM_CREATE处理函数中创建CMyView对象
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;//设置为活跃视图
//重写纯虚函数
void CMyView::OnDraw(CDC* pDC)
{
pDC->TextOut(100,100,"CMyView::OnDraw");
}

在CView中,如果有消息调用OnPaint(),则不调用 OnDraw(),建议只写OnDraw。


工具栏,状态栏使用:

#include <afxwin.h>
#include <AFXEXT.H>

C++MFC编程笔记day03 MFC工具栏、状态栏、视图窗口

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;
}



C++MFC编程笔记day03 MFC工具栏、状态栏、视图窗口,布布扣,bubuko.com

C++MFC编程笔记day03 MFC工具栏、状态栏、视图窗口

上一篇:Combinations leetcode java


下一篇:Visual C++文件后缀名释义