视图窗口
提供了一个用于显示数据的窗口
区别与框架窗口:
框架窗口:容纳子窗口,充当容器
视图窗口为框架窗口的子窗口
相关类
CView及其子类, CWnd为父类,封装了关于视图窗口的各种操作,以及和文档类的数据交互
使用
- 定义一个自己的视图类,派生自CView(抽象类,不重写无法生成对象),重写符类成员纯虚函数OnDraw(可用于画图)
- 其余框架类和应用程序类代码不变
- 在处理框架窗口的WM_CREATE消息时,定义CMyView类对象,并调用Create函数创建视图窗口
视图窗口的ID为 AFX_IDW_PANE_FIRST:视图窗口平铺框架窗口,只要大于ID即可平铺
#include<afxwin.h>
class CMyView :public CView {
DECLARE_MESSAGE_MAP()
public:
void OnDraw(CDC* pDC);
afx_msg void OnPaint();
};
void CMyView::OnDraw(CDC* pDC) {
pDC->TextOut(100, 100, "CMyView::OnDraw");
}//视图类不处理绘图消息,则父类处理绘图消息;建议直接用OnDraw画
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CMyView::OnPaint() {
PAINTSTRUCT pcs = { 0 };
HDC hdc = ::BeginPaint(this->m_hWnd, &pcs);
::TextOut(hdc, 200, 200,"CMyView::OnPaint",strlen("CMyView::OnPaint"));
::EndPaint(this->m_hWnd, &pcs);
}
class CMyFrameWnd :public CFrameWnd {
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();//创建框架窗口的宏需要的函数
afx_msg int OnCreate(LPCREATESTRUCT pcs);
};
BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_CREATE()
END_MESSAGE_MAP()
int CMyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
CMyView* pView = new CMyView;
pView->Create(NULL, "MFCView", WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(0, 0, 200, 200), this,
AFX_IDW_PANE_FIRST);
return CFrameWnd::OnCreate(pcs);
}
void CMyFrameWnd::OnPaint() {
PAINTSTRUCT ps = { 0 };
HDC hdc = ::BeginPaint(this->m_hWnd, &ps);
::TextOut(hdc, 100, 100, "我是框架窗口客户区", strlen("我是框架窗口客户区"));
::EndPaint(this->m_hWnd, &ps);
}
class CMyWinApp :public CWinApp {
public:
virtual BOOL InitInstance();
};
BOOL CMyWinApp::InitInstance() {
CMyFrameWnd* pFrame = new CMyFrameWnd;
pFrame->Create(NULL, "MFCView");
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return true;
}
CMyWinApp theApp;
命令消息处理顺序
视图类->框架类->应用程序类
`#include<afxwin.h>
#include "resource.h"
class CMyView :public CView {
DECLARE_MESSAGE_MAP()
public:
void OnDraw(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnNew();
};
void CMyView::OnNew() {
AfxMessageBox("视图类处理了WM_COMMAND消息");//需要点击视图窗口,因为活动视图窗口只能有一个
//或增加代码m_pViewActive=pView;
}
void CMyView::OnDraw(CDC* pDC) {
pDC->TextOut(100, 100, "CMyView::OnDraw");
}//视图类不处理绘图消息,则父类处理绘图消息;建议直接用OnDraw画
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_NEW,OnNew)
// ON_WM_PAINT()
END_MESSAGE_MAP()
void CMyView::OnPaint() {
PAINTSTRUCT pcs = { 0 };
HDC hdc = ::BeginPaint(this->m_hWnd, &pcs);
::TextOut(hdc, 200, 200,"CMyView::OnPaint",strlen("CMyView::OnPaint"));
::EndPaint(this->m_hWnd, &pcs);
}
class CMyFrameWnd :public CFrameWnd {
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();//创建框架窗口的宏需要的函数
afx_msg int OnCreate(LPCREATESTRUCT pcs);
afx_msg void OnNew();
};
void CMyFrameWnd::OnNew() {
AfxMessageBox("框架类处理了WM_COMMAND消息");
}
BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_PAINT()
ON_COMMAND(ID_NEW,OnNew)
ON_WM_CREATE()
END_MESSAGE_MAP()
int CMyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
CMyView* pView = new CMyView;
pView->Create(NULL, "MFCView", WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(0, 0, 200, 200), this,
AFX_IDW_PANE_FIRST);
m_pViewActive = pView;//增加后无需点击窗口即可弹出新建窗口
return CFrameWnd::OnCreate(pcs);
}
void CMyFrameWnd::OnPaint() {
PAINTSTRUCT ps = { 0 };
HDC hdc = ::BeginPaint(this->m_hWnd, &ps);
::TextOut(hdc, 100, 100, "我是框架窗口客户区", strlen("我是框架窗口客户区"));
::EndPaint(this->m_hWnd, &ps);
}
class CMyWinApp :public CWinApp {
DECLARE_MESSAGE_MAP()
public:
virtual BOOL InitInstance();
afx_msg void OnNew();
};
void CMyWinApp::OnNew() {
AfxMessageBox("应用程序类处理了WM_COMMAND消息");
}
BEGIN_MESSAGE_MAP(CMyWinApp,CWinApp)
ON_COMMAND(ID_NEW, OnNew)
END_MESSAGE_MAP()
BOOL CMyWinApp::InitInstance() {
CMyFrameWnd* pFrame = new CMyFrameWnd;
pFrame->Create(NULL, "MFCView", WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault,
NULL,(CHAR*)IDR_MENU1);
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return true;
}
CMyWinApp theApp;