当前位置:网站首页>5、 MFC view windows and documents
5、 MFC view windows and documents
2022-07-27 03:05:00 【[T]】
One 、 View window
Provide a window for displaying data
1、 View window related classes
CView And its subclasses , The parent class is CWnd class , Encapsulates various operations on the view window , And data interaction with document class
2、 Use of view window
(1) Defines its own view class (CMyView), Derive from CView, And override the pure virtual function of the parent class member void OnDraw(CDC* pDC), Can be used for drawing
(2) The rest of the framework class and application class program code remain unchanged
(3) Handle framework WM_CREATE When the news , Definition CMyView Class object , And call Create Function to create a view window , Of the view window ID by AFX_IDW_PANE_FIRST
3、 Create project (MFCView)
(1) Create project
(2) Programming
#include <afxwin.h>
#include "resource.h"
class CMyView:public CView
{
DECLARE_MESSAGE_MAP()
public:
virtual void OnDraw(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnNew();
};
BEGIN_MESSAGE_MAP(CMyView, CView)
//ON_WM_PAINT()
ON_COMMAND(ID_NEW, OnNew)
END_MESSAGE_MAP()
// When the parent class processes the drawing message, it will call OnDraw, When this class is handled by drawing messages , be OnDraw Will not be called
void CMyView::OnDraw(CDC* pDC)
{
pDC->TextOut(100, 100, "CMyView::OnDraw");
}
void CMyView::OnPaint()
{
PAINTSTRUCT ps = {0};
HDC hdc = ::BeginPaint(m_hWnd, &ps);
::TextOutA(hdc, 200, 200, "CMyView::OnPaint", strlen("CMyView::OnPaint"));
::EndPaint(m_hWnd, &ps);
}
void CMyView::OnNew()
{
AfxMessageBox(" The view class handles OnNew news ");
}
class CMyFrameWnd:public CFrameWnd
{
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();
afx_msg int OnCreate(LPCREATESTRUCT pcs);
afx_msg void OnNew();
};
BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_COMMAND(ID_NEW, OnNew)
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); //AFX_IDW_PANE_FIRST The generated view window will be tiled in the frame window
m_pViewActive = pView; // Assign the window to the active window
return CFrameWnd::OnCreate(pcs);
}
void CMyFrameWnd::OnPaint()
{
PAINTSTRUCT ps = {0};
HDC hdc = ::BeginPaint(m_hWnd, &ps);
::TextOutA(hdc, 100, 100, " Frame window client area ", strlen(" Frame window client area "));
::EndPaint(m_hWnd, &ps);
}
void CMyFrameWnd::OnNew()
{
AfxMessageBox(" The framework class handles OnNew news ");
}
class CMyWinApp:public CWinApp
{
DECLARE_MESSAGE_MAP()
public:
virtual BOOL InitInstance();
afx_msg void OnNew();
};
BEGIN_MESSAGE_MAP(CMyWinApp, CWinApp)
ON_COMMAND(ID_NEW, OnNew)
END_MESSAGE_MAP()
void CMyWinApp::OnNew()
{
AfxMessageBox(" The framework class handles OnNew news ");
}
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;(3) Command message processing sequence
View class -》 Framework class -》 Application class
(4) Object graph
theApp
theApp->m_pMainWnd = pFrame
pFrame->m_pViewActive = pView
Two 、 Document class
Related classes CDocument, Provides classes for managing data , Encapsulates the management of data ( Data Extraction , Data conversion , Data storage, etc ), And interact with the view class .
1、 Create project
(1) Create project (MFCDoc)
(2) Programming
#include <afxwin.h>
#include <afxext.h>
#include "resource.h"
class CMyDoc:public CDocument
{
};
class CMyView:public CView
{
DECLARE_DYNCREATE(CMyView)
DECLARE_MESSAGE_MAP()
public:
virtual void OnDraw(CDC* cDC);
afx_msg int OnCreate(LPCREATESTRUCT pcs);
};
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_WM_CREATE()
END_MESSAGE_MAP()
void CMyView::OnDraw(CDC* cDC)
{
cDC->TextOut(100, 100, " View window ");
}
int CMyView::OnCreate(LPCREATESTRUCT pcs)
{
return CView::OnCreate(pcs);
}
class CMyFrameWnd:public CFrameWnd
{
DECLARE_MESSAGE_MAP()
afx_msg int OnCreate(LPCREATESTRUCT pcs);
afx_msg void OnPaint();
};
BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT()
END_MESSAGE_MAP()
int CMyFrameWnd::OnCreate(LPCREATESTRUCT pcs)
{
return CFrameWnd::OnCreate(pcs);
}
void CMyFrameWnd::OnPaint()
{
PAINTSTRUCT ps = {0};
HDC hdc = ::BeginPaint(m_hWnd, &ps);
::TextOutA(hdc, 200, 200, "CMyFrameWnd::OnPaint", strlen("CMyFrameWnd::OnPaint"));
::EndPaint(m_hWnd, &ps);
}
class CMyWinApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
BOOL CMyWinApp::InitInstance()
{
CMyFrameWnd* pFrame = new CMyFrameWnd;
CCreateContext cct;
CMyDoc* pDoc = new CMyDoc;
cct.m_pNewViewClass = RUNTIME_CLASS(CMyView);
cct.m_pCurrentDoc = pDoc;
pFrame->LoadFrame(IDR_MENU1, WS_OVERLAPPEDWINDOW, NULL, &cct);
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
CMyWinApp theApp;(3) The creation process
Use the frame class object address (pFrame) call LoadFrame function , Create a frame window
In the processing frame window WM_CREATE When the news ,( Parent class CFrameWnd) Dynamically create view class objects , And create a view window
In the processing view window WM_CREATE When the news ,( Parent class CView) See the relationship between document class and view class objects
2、 Create a process
CMyFrameWnd* pFrame = new CMyFrameWnd;
CMyDoc* pDoc = new CMyDoc;
CCreateContext cct;
cct.m_pCurrentDoc = pDoc;// Document class object address
cct.m_pNewViewClass = RUNTIME_CLASS(CMyView);//&CMyView::classCMyView
pFrame->LoadFrame(... &cct)// Internal function this by pFrame
{
Create(...,&cct)// Internal function this by pFrame
{
CreateEx(...&cct)// Internal function this by pFrame
{
CREATESTRUCT cs;
....
cs.lpCreateParams = &cct;
::CreateWindowEx(...,&cct);// Create main frame window
}
}
}
// Handle frame window WM_CREATE news
CFrameWnd::OnCreate( pcs )// Internal function this by pFrame, Parameters can be obtained ::CreateWindowEx Of 12 Parameters
{
CCreateContext* pContext = (CCreateContext*)pcs->lpCreateParams;// obtain &cct
OnCreateHelper(pcs, pContext)// Internal function this by pFrame,pContext===&cct
{
OnCreateClient(pcs, pContext)// Internal function this by pFrame,pContext===&cct
{
CreateView(pContext..)// Internal function this by pFrame,pContext===&cct
{
CWnd* pView = pContext->m_pNewViewClass->CreateObject();
// Dynamically create view class objects , And return the object address
pView->Create(..,pContext)// Internal function this by pView,pContext===&cct
{
CreateEx(..,pContext)// Internal function this by pView,pContext===&cct
{
CREATESTRUCT cs;
....
cs.lpCreateParams = pContext;//pContext===&cct
::CreateWindowEx(...,pContext);// Create view window
}
}
}
}
}
}
// Handle view window WM_CREATE news
CView::OnCreate( pcs )// Internal function this by pView, Parameters can be obtained ::CreateWindowEx Functional 12 Parameters
{
CCreateContext* pContext = (CCreateContext*)lpcs->lpCreateParams;// obtain &cct
pContext->m_pCurrentDoc->AddView(pView)// Internal function this by pDoc, The parameter is the address of the view class object
{
m_viewList.AddTail(pView);// The document class object uses a linked list member variable , Save the address of the view class object
pView->m_pDocument = this;// The view class object uses a common member variable , Save the address of the document class object
}
}3、 Object relationship
(1) The document class object uses a linked list member variable , Save the address of the view class object
(2) The view class object uses a common member variable , Save the address of the document class object
A document object can correspond to multiple view objects ( View window )
A view class object ( View window ) Only one document class object can be mapped
4、 View segmentation
(1) Related classes
CSplitterWnd Irregular frame window class , Subpackaged the operation of irregular frame window
(2) Use of window segmentation
rewrite CFrameWnd Class member virtual function OnCreateClient
Call... In a virtual function CSplitterWnd::CreateStatic Create irregular frame window
Call... In a virtual function CSplitterWnd::CreateView Create view window
(3) Programming
#include <afxwin.h>
#include <afxext.h>
#include "resource.h"
class CMyDoc:public CDocument
{
};
class CMyView:public CView
{
DECLARE_DYNCREATE(CMyView)
DECLARE_MESSAGE_MAP()
public:
virtual void OnDraw(CDC* cDC);
afx_msg int OnCreate(LPCREATESTRUCT pcs);
};
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_WM_CREATE()
END_MESSAGE_MAP()
void CMyView::OnDraw(CDC* cDC)
{
cDC->TextOut(100, 100, " View window ");
}
int CMyView::OnCreate(LPCREATESTRUCT pcs)
{
return CView::OnCreate(pcs);
}
class CMyFrameWnd:public CFrameWnd
{
DECLARE_MESSAGE_MAP()
afx_msg int OnCreate(LPCREATESTRUCT pcs);
afx_msg void OnPaint();
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
private:
CSplitterWnd m_split; // Irregular frame window
};
BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT()
END_MESSAGE_MAP()
int CMyFrameWnd::OnCreate(LPCREATESTRUCT pcs)
{
return CFrameWnd::OnCreate(pcs);
}
void CMyFrameWnd::OnPaint()
{
PAINTSTRUCT ps = {0};
HDC hdc = ::BeginPaint(m_hWnd, &ps);
::TextOutA(hdc, 200, 200, "CMyFrameWnd::OnPaint", strlen("CMyFrameWnd::OnPaint"));
::EndPaint(m_hWnd, &ps);
}
BOOL CMyFrameWnd::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// Create two view windows
m_split.CreateStatic(this, 1, 2);
m_split.CreateView(0, 0, RUNTIME_CLASS(CMyView), CSize(100, 100), pContext);
m_split.CreateView(0, 1, pContext->m_pNewViewClass, CSize(100, 100), pContext);
return TRUE;
}
class CMyWinApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
BOOL CMyWinApp::InitInstance()
{
CMyFrameWnd* pFrame = new CMyFrameWnd;
CCreateContext cct;
CMyDoc* pDoc = new CMyDoc;
cct.m_pNewViewClass = RUNTIME_CLASS(CMyView);
cct.m_pCurrentDoc = pDoc;
pFrame->LoadFrame(IDR_MENU1, WS_OVERLAPPEDWINDOW, NULL, &cct);
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
CMyWinApp theApp;
边栏推荐
- 红宝书第四版的一个错误?
- Pyqt5 use pyqtgraph to draw dynamic scatter chart
- CS224W fall 课程 ---- 1.1 why Graphs ?
- 素因子分解--C(gcc)--PTA
- 阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
- Arduinouno drive RGB module full color effect example
- day6
- Dynamically set the height of applet swiper
- Inftnews | "traffic + experience" white lining e Digital Fashion Festival leads the new changes of digital fashion
- Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
猜你喜欢

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

基于GoLang实现API短信网关

批量复制宝贝上传提示乱码,如何解决?

Cloud development sleeping alarm clock wechat applet source code

商城小程序项目完整源码(微信小程序)

系统安全测试要怎么做,详细来说说

CAS deployment and successful login jump address

Kubernetes dashboard deployment application and access

哈希表与一致性哈希的原理理解以及应用

基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
随机推荐
Go to export excel form
[redis] five common data types
Goatgui invites you to attend a machine learning seminar
Cs224w fall course - --- 1.1 why graphs?
论构造函数的原型是谁
一个测试类了解BeanUtils.copyProperties
[二分查找中等题] LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
Arduinouno drive RGB module full color effect example
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
ansible系列之:不收集主机信息 gather_facts: False
Turn: YuMinHong: what hinders your growth is yourself
How to do the system security test? Let's talk about it in detail
Greed - 376. Swing sequence
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
[二分查找简单题] LeetCode 35. 搜索插入位置,69. x 的平方根,367. 有效的完全平方数,441. 排列硬币
Shell 分析日志文件命令全面总结
商城小程序项目完整源码(微信小程序)
Is the low commission account opening of Galaxy Securities Fund reliable, reliable and safe
vs2019 中编译和使用 protobuf 库