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狀態欄

相關類:app

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>
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;
}
相關文章
相關標籤/搜索