CView - MSDN6翻译交流

BUG和建议请提出,谢谢!

CView

The CView class provides the basic functionality for user-defined view classes. A view is attached to a document and acts as an intermediary between the document and the user: the view renders an image of the document on the screen or printer and interprets user input as operations upon the document.

CView 类为用户(自)定义的视图类提供(最)基本的功能。视图附属于一个文档,扮演着文档和用户之间一个中介。视图(用来)呈现文档在屏幕或是打印机上(所显示)的图象,并将用户的输入(解释为)对文档的操作。

A view is a child of a frame window. More than one view can share a frame window, as in the case of a splitter window. The relationship between a view class, a frame window class, and a document class is established by a CDocTemplate object. When the user opens a new window or splits an existing one, the framework constructs a new view and attaches it to the document.

视图是框架窗口的(子窗口)。多于一个的视图可以共用一个框架窗口,(就像在窗口被分割时那样)。视图类、框架窗口类和文档类三者之间的关系是由一个CDocTemplate 物件来制定的。当用户打开一个新的窗口或是分割一个现有的窗口时,(MFC)框架将构造一个新的视图并使其附属到此文档。

A view can be attached to only one document, but a document can have multiple views attached to it at once — for example, if the document is displayed in a splitter window or in multiple child windows in a multiple document interface (MDI) application. Your application can support different types of views for a given document type; for example, a word-processing program might provide both a complete text view of a document and an outline view that shows only the section headings. These different types of views can be placed in separate frame windows or in separate panes of a single frame window if you use a splitter window.

一个视图只能附属于单个文档,但一个文档能够同时拥有多个隶属于它视图——例如,( 如果 )某个文档被显示在分割的窗口中,或是多文档接口(MDI)应用的多个子窗口中。你的应用(也)能够为给定的文档类型支持不同类型的视图;例如,一个文字处理程序可以为一个文档同时提供一个完整的文本视图和一个只显示(节标题)的大纲视图。这些不同类型的视图能够被安置在独立的框架窗口之中,或是单个框架窗口的独立的窗格之中(如果你使用分割窗口的话)。

A view may be responsible for handling several different types of input, such as keyboard input, mouse input or input via drag-and-drop, as well as commands from menus, toolbars, or scroll bars. A view receives commands forwarded by its frame window. If the view does not handle a given command, it forwards the command to its associated document. Like all command targets, a view handles messages via a message map.

视图可以负责处理几种不同类型的输入,像是键盘输入、鼠标输入或是通过拖放的输入,还有来自菜单、工具栏或和滚动条的命令。视图接受由其框架窗口传入命令。如果此视图没有处理给定的命令,它将把此命令传给其关联的文档。和所有的命令目标一样,视图通过消息映射来处理消息。

The view is responsible for displaying and modifying the document’s data but not for storing it. The document provides the view with the necessary details about its data. You can let the view access the document’s data members directly, or you can provide member functions in the document class for the view class to call.

视图负责了文档数据的显示和修改但不(负责其)存储。文档为视图提供其数据的必要详情。你能够让视图直接访问文档的数据成员,或者你可以在文档类中提供成员函数以供视图类调用。

When a document’s data changes, the view responsible for the changes typically calls the CDocument::UpdateAllViews function for the document, which notifies all the other views by calling the OnUpdate member function for each. The default implementation of OnUpdate invalidates the view’s entire client area. You can override it to invalidate only those regions of the client area that map to the modified portions of the document.

当文档的数据改变时,(负责着变化的)视图(代表性地)调用此文档的CDocument::UpdateAllViews函数,此函数通过调用所有其他的视图的OnUpdate成员函数来通知它们。OnUpdate 默认的执行(将)使视图的全部客户区(都)无效。你可以覆盖(此函数)以仅使那些对应着文档改动部分的客户区域无效。

To use CView, derive a class from it and implement the OnDraw member function to perform screen display. You can also use OnDraw to perform printing and print preview. The framework handles the print loop for printing and previewing your document.

要使用CView,请从它继承一个类并实现OnDraw 成员函数以执行屏幕显示。你也能用OnDraw 来执行打印和打印预览。(MFC)框架(会)处理你的文档的打印及预览的(打印循环)。

A view handles scroll-bar messages with the CWnd::OnHScroll and CWnd::OnVScroll member functions. You can implement scroll-bar message handling in these functions, or you can use the CView derived class CScrollView to handle scrolling for you.

视图使用CWnd::OnHScrollCWnd::OnVScroll成员函数来处理滚动条消息。你能够在这些函数中实现对滚动条消息的处理,你也能使用CView 的派生类CScrollView来为你处理滚动(消息)。

Besides CScrollView, the Microsoft Foundation Class Library provides nine other classes derived from CView:

除了CScrollView之外,微软基础类库(还)提供9个其它的继承自CView的类:

  • CCtrlView, a view that allows usage of document - view architecture with tree, list, and rich edit controls.
  • CCtrlView,(允许将文档——视图体系按树、列表和富编辑控件的用法使用的视图)。

  • CDaoRecordView, a view that displays database records in dialog-box controls.
  • CDaoRecordView,在对话框控件中显示数据库记录的视图。

  • CEditView, a view that provides a simple multiline text editor. You can use a CEditView object as a control in a dialog box as well as a view on a document.
  • CEditView,提供简单的多行文本编辑器的视图。你可以使用一个CEditView 物件作为对话框中的控件,(就像在文档上使用视图那样)。

  • CFormView, a scrollable view that contains dialog-box controls and is based on a dialog template resource.
  • CFormView,一个可滚动的视图,包含对话框控件,是基于对话框模板资源的。

  • CListView, a view that allows usage of document - view architecture with list controls.
  • CListView,(允许将文档——视图体系按列表控件的用法使用的视图)。

  • CRecordView, a view that displays database records in dialog-box controls.
  • CRecordView,在对话框控件中显示数据库记录的视图。

  • CRichEditView, a view that allows usage of document - view architecture with rich edit controls.
  • CRichEditView,(允许将文档——视图体系按富编辑控件的用法使用的视图)。

  • CScrollView, a view that automatically provides scrolling support.
  • CScrollView,自动提供滚动支持的视图。

  • CTreeView, a view that allows usage of document - view architecture with tree controls.
  • CTreeView,(允许将文档——视图体系按树控件的用法使用的视图)。

The CView class also has a derived implementation class named CPreviewView, which is used by the framework to perform print previewing. This class provides support for the features unique to the print-preview window, such as a toolbar, single- or double-page preview, and zooming, that is, enlarging the previewed image. You don’t need to call or override any of CPreviewView’s member functions unless you want to implement your own interface for print preview  (for example, if you want to support editing in print preview mode). For more information on using CView, see Document/View Architecture Topics and Printing in Visual C++ Programmer’s Guide. In addition, see Technical Note 30 for more details on customizing print preview.

#include <afxwin.h>

Class MembersBase ClassHierarchy Chart

Samples    MFC Sample HELLO |  MFC Sample MDIBIND |  MFC Sample MDIDOCVW

See Also   CWnd, CFrameWnd, CSplitterWnd, CDC, CDocTemplate, CDocument

<think> 我们分析用户的问题:在MFC框架中,结构为MainFrame -> ChildFrame -> CView -> CSplitterWnd -> CWnd,如何从MainFrame给CWnd正确传值。 根据之前的讨论,直接跨层级访问对象会破坏MFC文档/视图架构。正确的做法是通过层层传递,确保每一层只与直接下一层交互。 解决方案步骤: 1. MainFrame获取当前活动的ChildFrame(通过MDIGetActive)。 2. MainFrame调用ChildFrame的一个自定义方法,将数据传递给ChildFrame。 3. ChildFrame中,因为其持有一个CView(可能是分割窗口的父视图),所以需要进一步传递到CView。 4. CView再传递到分割窗口CSplitterWnd。 5. 最后,CSplitterWnd将数据传递到目标CWnd(窗格中的视图或控件)。 注意:实际中,CSplitterWnd通常包含多个窗格(每个窗格是一个视图),所以需要明确目标窗格。 为了减少耦合,我们可以采用以下方法: - 在ChildFrame中定义一个方法,用于接收来自MainFrame的数据,并将其转发给它的视图(CView)。 -CView中,再定义一个方法,将数据转发给分割窗口(CSplitterWnd)。 - 在CSplitterWnd中,再定义一个方法,将数据转发给指定的窗格(CWnd)。 但是,这样层层传递可能过于繁琐。另一种推荐的方法是使用MFC的消息机制(SendMessage/PostMessage)进行跨层级通信,这样各个类之间可以解耦。 下面给出两种实现方式: ### 方式1:通过方法调用层层传递(适合直接控制) #### 步骤1:在ChildFrame中定义方法 ```cpp // ChildFrame.h class CChildFrame : public CMDIChildWnd { public: void PassDataToView(const CString& strData); }; // ChildFrame.cpp void CChildFrame::PassDataToView(const CString& strData) { CView* pView = GetActiveView(); if (pView) { // 调用视图的方法 pView->SendDataToSplitter(strData); // 需要视图类有这个方法 } } ``` #### 步骤2:在CView中定义方法,传给分割窗口 ```cpp // MyView.h class CMyView : public CView { public: void SendDataToSplitter(const CString& strData); }; // MyView.cpp void CMyView::SendDataToSplitter(const CString& strData) { // 假设视图类持有一个分割窗口的指针(例如,在创建分割窗口时保存) if (m_pSplitter) { m_pSplitter->PassDataToPane(strData); // 需要分割窗口类有这个方法 } } ``` #### 步骤3:在CSplitterWnd中定义方法,传给目标窗格 ```cpp // MySplitter.h class CMySplitter : public CSplitterWnd { public: void PassDataToPane(const CString& strData); }; // MySplitter.cpp void CMySplitter::PassDataToPane(const CString& strData) { // 获取目标窗格(假设第0行第1列) CWnd* pWnd = GetPane(0, 1); if (pWnd) { // 这里假设目标窗格是一个自定义视图,有SetString方法 ((CMyPaneView*)pWnd)->SetString(strData); } } ``` #### 步骤4:在MainFrame中调用ChildFrame的方法 ```cpp void CMainFrame::OnPassData() { CMDIChildWnd* pChild = MDIGetActive(); if (pChild) { CChildFrame* pMyChild = DYNAMIC_DOWNCAST(CChildFrame, pChild); if (pMyChild) { pMyChild->PassDataToView(_T("Data from MainFrame")); } } } ``` ### 方式2:使用消息传递(解耦推荐) #### 步骤1:定义自定义消息 ```cpp #define WM_USER_PASS_DATA (WM_USER + 100) ``` #### 步骤2:在目标CWnd(窗格视图)中处理消息 ```cpp // 在窗格视图类的头文件中声明消息处理函数 afx_msg LRESULT OnPassData(WPARAM wParam, LPARAM lParam); // 在实现文件中添加映射 BEGIN_MESSAGE_MAP(CMyPaneView, CWnd) ON_MESSAGE(WM_USER_PASS_DATA, OnPassData) END_MESSAGE_MAP() // 实现消息处理 LRESULT CMyPaneView::OnPassData(WPARAM wParam, LPARAM lParam) { CString* pStr = (CString*)wParam; // 使用数据,注意:这里需要处理数据的生命周期,也可以使用其他方式传递数据 SetMyString(*pStr); return 0; } ``` #### 步骤3:从MainFrame发送消息 假设已知目标窗格的句柄(hwnd),但通常我们不知道。因此,需要先获取目标窗格的句柄。 如何获取?可以通过层层获取,但也可以提前在某个地方(如创建时)将目标窗格的句柄注册到MainFrame,或者通过消息广播。 但是,如果采用消息传递,我们也可以将消息发送给ChildFrame,然后ChildFrame转发给它的视图,再转发给分割窗口,最后到目标窗格。这样同样需要层层传递消息。 更直接的方式(如果知道目标窗格的路径): ```cpp void CMainFrame::OnPassData() { CMDIChildWnd* pChild = MDIGetActive(); if (pChild) { // 获取子框架的视图 CView* pView = pChild->GetActiveView(); if (pView) { // 假设我们知道分割窗口是视图的子窗口(例如,视图创建了分割窗口作为子窗口) CSplitterWnd* pSplitter = (CSplitterWnd*)pView->GetWindow(GW_CHILD); if (pSplitter && pSplitter->IsKindOf(RUNTIME_CLASS(CSplitterWnd))) { CWnd* pTargetWnd = pSplitter->GetPane(0, 1); if (pTargetWnd) { // 发送消息给目标窗格 CString strData = _T("Hello from MainFrame"); // 注意:这里直接发送字符串可能有问题,因为字符串是局部变量,在接收端可能已经被销毁。 // 解决方案:使用全局字符串、动态分配字符串(注意释放)或通过其他方式传递字符串(如共享内存、成员变量等) pTargetWnd->SendMessage(WM_USER_PASS_DATA, (WPARAM)&strData, 0); } } } } } ``` 注意:上面的消息传递中,直接传递局部字符串的地址是不安全的,因为当接收消息时,可能该字符串已经超出作用域。因此,需要确保接收方在字符串有效期内处理消息(如同步SendMessage),或者通过动态分配字符串,并在接收方处理后删除。但这样会增加复杂性。 ### 推荐做法 根据MVC原则,建议采用方式1的层层方法调用,或者在ChildFrame中提供对目标窗格的访问接口(封装),这样MainFrame只需调用ChildFrame的接口,由ChildFrame负责内部传递。 ### 结论 从MainFrame给CWnd传值,应通过ChildFrame中转,并最终由分割窗口传递给目标窗格。推荐在ChildFrame中封装一个方法,供MainFrame调用,内部实现数据传递到目标窗格。 §§相关问题§§ 1. 在MFC中,如何使用消息传递机制实现跨多个窗口层级的数据传输? 2. 使用SendMessage传递数据时,如何避免因数据生命周期导致的内存问题? 3. 在MFC文档/视图架构中,MainFrame如何安全地获取和操作深层嵌套的视图对象? 4. 如何设计MFC应用程序的通信机制以减少层级间的耦合? 5. 使用CSplitterWnd创建的分割窗口中,如何动态获取特定窗格的指针? </think> ### 正确实现方案 在 MFC 框架中,MainFrame → ChildFrame → CView → CSplitterWnd → CWnd 的层级结构中,从 MainFrame 向最底层的 CWnd 传值需要遵循 MFC 文档/视图架构的分层原则。以下是安全可靠的实现方法: #### 推荐方案:通过消息机制传递(解耦最佳实践) ```cpp // 1. 定义自定义消息 (MainFrame.h) #define WM_USER_PASS_VALUE (WM_USER + 100) // 2. 在目标 CWnd 类中处理消息 (MyPaneWnd.h) class CMyPaneWnd : public CWnd { DECLARE_MESSAGE_MAP() afx_msg LRESULT OnPassValue(WPARAM wParam, LPARAM lParam); }; // (MyPaneWnd.cpp) BEGIN_MESSAGE_MAP(CMyPaneWnd, CWnd) ON_MESSAGE(WM_USER_PASS_VALUE, OnPassValue) END_MESSAGE_MAP() LRESULT CMyPaneWnd::OnPassValue(WPARAM wParam, LPARAM lParam) { CString* pStr = reinterpret_cast<CString*>(wParam); if (pStr) { SetWindowText(*pStr); // 实际使用值 delete pStr; // 释放动态内存 } return 0; } // 3. 在 ChildFrame 中封装访问方法 (ChildFrame.cpp) CWnd* CChildFrame::GetTargetPane() { if (CView* pView = GetActiveView()) { if (CSplitterWnd* pSplitter = dynamic_cast<CSplitterWnd*>(pView)) { return pSplitter->GetPane(0, 1); // 目标窗格 } } return nullptr; } // 4. MainFrame 中安全传值 (MainFrame.cpp) void CMainFrame::SendValueToPane(const CString& strValue) { if (CMDIChildWnd* pChild = MDIGetActive()) { if (CChildFrame* pChildFrame = dynamic_cast<CChildFrame*>(pChild)) { if (CWnd* pTarget = pChildFrame->GetTargetPane()) { // 动态分配确保线程安全 CString* pDynamicStr = new CString(strValue); pTarget->SendMessage( WM_USER_PASS_VALUE, reinterpret_cast<WPARAM>(pDynamicStr) ); } } } } ``` #### 备选方案:通过子框架中转(直接访问) ```cpp // 在 ChildFrame 中添加访问方法 (ChildFrame.cpp) void CChildFrame::SetPaneValue(const CString& strValue) { if (CSplitterWnd* pSplitter = dynamic_cast<CSplitterWnd*>(GetActiveView())) { if (CMyPaneWnd* pPane = dynamic_cast<CMyPaneWnd*>(pSplitter->GetPane(0, 1))) { pPane->SetValueDirectly(strValue); // 窗格自有方法 } } } // MainFrame 中调用 (MainFrame.cpp) void CMainFrame::OnPassValue() { if (CMDIChildWnd* pChild = MDIGetActive()) { if (CChildFrame* pFrame = dynamic_cast<CChildFrame*>(pChild)) { pFrame->SetPaneValue(_T("Data from MainFrame")); } } } ``` ### 关键原则 1. **分层访问**:MainFrame 只应与直接子级 ChildFrame 交互 2. **动态类型检查**:必须使用 `dynamic_cast` 或 `DYNAMIC_DOWNCAST` 3. **资源管理**: - 消息传递时使用 `new/delete` 管理字符串内存 - 使用 `SendMessage` 确保同步执行 4. **封装性**: - 窗格操作细节封装在 ChildFrame 中 - MainFrame 不应知晓 CSplitterWnd/CWnd 的存在 > **性能提示**:高频操作建议使用共享内存或文件映射,避免消息传递开销
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值