MFC - 04 单文档视图、多文档视图、MFC绘图
单文档视图架构程序
只能管理一个文档,只有一个文档类对象。
参与架构的类: CFrameWnd / CView / CDocument / CWinApp(除了这个类以外,均支持动态创建机制)
需要用到的类:CSingleDocTemplate 文档模板类(父类为 CDocTemplate 文档模板类)/ CDocManager 文档管理类
#include <afxwin.h>
#include <afxext.h>
#include "resource.h"
class CMyDoc : public CDocument
{
DECLARE_DYNCREATE(CMyDoc)
};
IMPLEMENT_DYNCREATE(CMyDoc, CDocument)
class CMyView : public CView
{
DECLARE_DYNCREATE(CMyView)
public:
virtual void OnDraw(CDC* cDC);
};
IMPLEMENT_DYNCREATE(CMyView, CView)
void CMyView::OnDraw(CDC* cDC)
{
cDC->TextOut(100, 100, "CMyView::OnDraw");
}
class CMyFrameWnd : public CFrameWnd
{
DECLARE_DYNCREATE(CMyFrameWnd)
};
IMPLEMENT_DYNCREATE(CMyFrameWnd, CFrameWnd)
class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
BOOL CMyWinApp::InitInstance()
{
CSingleDocTemplate* pTemplate = new CSingleDocTemplate(
IDR_MENU1,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CMyFrameWnd),
RUNTIME_CLASS(CMyView));
AddDocTemplate(pTemplate);
OnFileNew();
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMyWinApp theApp;
并添加菜单资源和字符串表资源:
多文档视图架构
可以管理多个文档(可以有多个文档类对象)
参与架构的类: CMDIFrameWnd / CMDIChildWnd / CWinApp / CView / CDocument
需要用到的类:CMultiDocTemplate(多文档模板类,继承自 CDocTemplate 文档模板类) CDocManager 文档管理类
#include <afxwin.h>
#include "resource.h"
class CMyDoc : public CDocument
{
DECLARE_DYNCREATE(CMyDoc)
};
IMPLEMENT_DYNCREATE(CMyDoc, CDocument)
class CMyView : public CView
{
DECLARE_DYNCREATE(CMyView)
public:
virtual void OnDraw(CDC* pDC);
};
IMPLEMENT_DYNCREATE(CMyView, CView)
void CMyView::OnDraw(CDC* pDC)
{
pDC->TextOut(100, 100, "CMyView::OnDraw");
}
class CMyChild : public CMDIChildWnd
{
DECLARE_DYNCREATE(CMyChild)
};
IMPLEMENT_DYNCREATE(CMyChild, CMDIChildWnd)
// 不支持动态创建
class CMyFrameWnd : public CMDIFrameWnd
{
};
// 不支持动态创建
class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
BOOL CMyWinApp::InitInstance()
{
CMyFrameWnd* pFrame = new CMyFrameWnd;
pFrame->LoadFrame(IDR_MENU1);
this->m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
CMultiDocTemplate* pTemplate = new CMultiDocTemplate(
IDR_MENU2, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMyChild), RUNTIME_CLASS(CMyView));
this->AddDocTemplate(pTemplate);
// 调用三次创建三个子窗口对象,每个都关联一个文档类对象
this->OnFileNew();// 创建文档类对象,创建子框架类对象、窗口,创建视图类对象、窗口,文档类对象和视图类窗口关联
this->OnFileNew();
this->OnFileNew();
return TRUE;
}
CMyWinApp theApp;
MFC 绘图
WIN32:需要绘图设备 HDC
MFC:与HDC绑定的类对象
CDC类(绘图设备类),封装了各种绘图相关的函数,以及两个非常重要的成员变量 m_hDC
和 m_hArribDC
二者一样,保存的都是绘图句柄
子类:
CPaintDC
,封装了 WM_PAINT 消息中绘图的绘图设备
class CMyView : public CView { DECLARE_DYNCREATE(CMyView) DECLARE_MESSAGE_MAP() public: virtual void OnDraw(CDC* cDC); void OnPaint(); }; IMPLEMENT_DYNCREATE(CMyView, CView) BEGIN_MESSAGE_MAP(CMyView, CView) ON_WM_PAINT() END_MESSAGE_MAP() void CMyView::OnDraw(CDC* cDC) { } void CMyView::OnPaint() { CPaintDC dc(this); // 封装了 BeginPaint EndPaint 等操作 dc.Rectangle(200, 200, 300, 300); }
CClientDC
,封装了在客户区绘图的绘图设备
void CMyView::OnClient() { // 在客户区画图的工具类 CClientDC CClientDC dc(this); dc.Ellipse(300, 300, 500, 500); }
绘图相关类:封装了各种绘图对象操作,以及一个非常重要的成员变量 m_hObject(绘图对象句柄)
- CPen
class CMyView : public CView { DECLARE_DYNCREATE(CMyView) DECLARE_MESSAGE_MAP() public: virtual void OnDraw(CDC* cDC); }; IMPLEMENT_DYNCREATE(CMyView, CView) void CMyView::OnDraw(CDC* dc) { dc->TextOut(100, 100, "CMyView::OnDraw"); CPen pen(PS_SOLID, 2, RGB(255, 0, 0)); CPen* oldPen = dc->SelectObject(&pen); dc->Rectangle(200, 200, 250, 250); dc->SelectObject(oldPen); pen.DeleteObject(); }
- CBrush
class CMyView : public CView { DECLARE_DYNCREATE(CMyView) public: virtual void OnDraw(CDC* cDC); }; IMPLEMENT_DYNCREATE(CMyView, CView) void CMyView::OnDraw(CDC* dc) { CBrush brush(RGB(0, 255, 255)); CBrush* oldBrush = dc->SelectObject(&brush); dc->Rectangle(100, 100, 300, 300); dc->SelectObject(oldBrush); brush.DeleteObject(); }
- CFont
- CBitmap
class CMyView : public CView { DECLARE_DYNCREATE(CMyView) public: virtual void OnDraw(CDC* cDC); }; IMPLEMENT_DYNCREATE(CMyView, CView) void CMyView::OnDraw(CDC* dc) { // 创建一个和当前DC匹配的内存DC CDC memdc; memdc.CreateCompatibleDC(dc); //将位图的数据送给内存DC CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP1); CBitmap* oldbmp = memdc.SelectObject(&bmp); //成像 dc->BitBlt(100, 100, 48, 48, &memdc, 0, 0, SRCCOPY); //将位图数据换回来 memdc.SelectObject(oldbmp); //销毁位图 bmp.DeleteObject(); //销毁内存DC memdc.DeleteDC(); }