当前位置: 首页 > news >正文

MFC第二十九天 CView类的分支(以及其派生类的功能)、MFC六大关键技术

文章目录

  • CView类的分支
    • CEditView
    • CHtmlView
      • MainFrm.h CMainFrame 类的接口
      • CMainView .h
    • CListCtrl与CListView的创建原理
  • CTreeView
    • CTreeCtrl类简介
    • CTreeCtrl类的原理以及常用功能
  • MFC六大关键技术
  • 视图和带分割栏的框架开发与消息路由
    • CLeftView.cpp
    • CRightView.h
    • CRightView.cpp
    • CWelcome .h
    • CWelcome.cpp
    • division.h
    • division.cpp
    • MainFrm.h
    • MainFrm.cpp
  • 附录:为什么用户点击一下视图,即可让菜单有效?

CView类的分支

CEditView

对CMainView使用了CEditView类的派生,对其默认字体进行改变。

#include "CMainView.h"BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{CCreateContext cc;cc.m_pNewViewClass = RUNTIME_CLASS(CMainView);CreateView(&cc);return CFrameWnd::OnCreateClient(lpcs, pContext);
}
// CMainView 视图
class CMainView : public CEditView
{DECLARE_DYNCREATE(CMainView)protected:CMainView();           // 动态创建所使用的受保护的构造函数virtual ~CMainView();protected:DECLARE_MESSAGE_MAP()
public:virtual void OnInitialUpdate();afx_msg void OnEnChange();
};
void CMainView::OnInitialUpdate()
{CEditView::OnInitialUpdate();auto hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);SetFont(CFont::FromHandle(hFont), FALSE); //CFont::FromHandle(hFont) 将句柄转为指针 临时外壳}

在这里插入图片描述
在这里插入图片描述

CHtmlView

需要添加#include <afxhtml.h>头文件

MainFrm.h CMainFrame 类的接口

#pragma once
class CMainView;
class CMainFrame : public CFrameWnd
{CMainView* m_pMainView;
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{CCreateContext cc;cc.m_pNewViewClass = RUNTIME_CLASS(CMainView);m_pMainView =(CMainView*)CreateView(&cc);return  m_pMainView!=NULL;
}
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{// 让视图第一次尝试该命令if (m_pMainView && m_pMainView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))return TRUE;// 否则,执行默认处理return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

CMainView .h

class CMainView : public CHtmlView
{DECLARE_DYNCREATE(CMainView)
}
#include "pch.h"
#include "Browse.h"
#include "CMainView.h"void CMainView::OnInitialUpdate()
{CHtmlView::OnInitialUpdate();Navigate2(_T("www.baidu.com")); //导航到目标地址上}

在这里插入图片描述

CListCtrl与CListView的创建原理

a)CListCtrl的内部创建原理是通过CWnd::Create(sClassName,…)来实现的。
b)CStatic,CEdit,CButton的内部创建原理无一不是这个原理,即使是拖入对话框的控件底层也是这样实现的。
(通过.rc读取风格和位置等要素,再调用CWnd类的Create函数)
c)CListView和CTreeView整个类都几乎没有代码,其实就是一个变种的CListCtrl或者CTreeCtrl。
d)所以你会看到直接强转:

INLINE CListCtrl& CListView::GetListCtrl() const{ return *(CListCtrl*)this; }

CListView和CTreeView的原理

// CListView
_AFXCVIEW_INLINE CListView::CListView() : CCtrlView(WC_LISTVIEW,AFX_WS_DEFAULT_VIEW){ }_AFXCVIEW_INLINE CListCtrl& CListView::GetListCtrl() const{ return *(CListCtrl*)this; }
_AFXCVIEW_INLINE CTreeView::CTreeView() : CCtrlView(WC_TREEVIEW,AFX_WS_DEFAULT_VIEW){ }_AFXCVIEW_INLINE CTreeCtrl& CTreeView::GetTreeCtrl() const{ return *(CTreeCtrl*)this; }

CTreeView

CTreeCtrl类简介

class CTreeCtrl : public CWnd
{// DECLARE_DYNAMIC virtual OK - CWnd has DECLARE_DYNAMICDECLARE_DYNAMIC(CTreeCtrl)// Constructors
public:CTreeCtrl();// Generic creatorBOOL Create(_In_ DWORD dwStyle, _In_ const RECT& rect, _In_ CWnd* pParentWnd, _In_ UINT nID);// Generic creator allowing extended style bitsBOOL CreateEx(_In_ DWORD dwExStyle, _In_ DWORD dwStyle, _In_ const RECT& rect,_In_ CWnd* pParentWnd, _In_ UINT nID);// Attributes// Retrieves the bounding rectangle for the specified item.BOOL GetItemRect(_In_ HTREEITEM hItem, _Out_ LPRECT lpRect, _In_ BOOL bTextOnly) const;// Gets the count of items in the control.UINT GetCount() const;// Returns the level-to-level indentation (in pixels).UINT GetIndent() const;// Sets the level-to-level indentation (in pixels).void SetIndent(_In_ UINT nIndent);// Retrieves the image list associated with the control.CImageList* GetImageList(_In_ int nImageList) const;// Sets the image list associated with this control.CImageList* SetImageList(_In_opt_ CImageList* pImageList, _In_ int nImageList);// Retrieves the next item having the given relationship with the// specified item. 遍历函数HTREEITEM GetNextItem(_In_ HTREEITEM hItem, _In_ UINT nCode) const;// Retrieves the next child item after the specified item.HTREEITEM GetChildItem(_In_ HTREEITEM hItem) const;
//	获取长子// Retrieves the next sibling item after the specified item.HTREEITEM GetNextSiblingItem(_In_ HTREEITEM hItem) const;// Retrieves the previous sibling item of the specified item.HTREEITEM GetPrevSiblingItem(_In_ HTREEITEM hItem) const;// Retrieves the parent of the specified item.HTREEITEM GetParentItem(_In_ HTREEITEM hItem) const;// Retrieves the topmost visible item in the control.HTREEITEM GetFirstVisibleItem() const;// Retrieves the next visible item after the specified item.HTREEITEM GetNextVisibleItem(_In_ HTREEITEM hItem) const;// Retrieves the previous visible item before the specified item.HTREEITEM GetPrevVisibleItem(_In_ HTREEITEM hItem) const;// Retrieves the last expanded item in the tree. This does not retrieve the last item visible in the tree-view window.HTREEITEM GetLastVisibleItem() const;// Retrieves the higlighted item, NULL if none.HTREEITEM GetSelectedItem() const;获取选中的项// Retrieves the currently drop-highlighted item, NULL if none.HTREEITEM GetDropHilightItem() const;// Retrieves the root item of the control, NULL if none.HTREEITEM GetRootItem() const;// Retrieves information about a particular item in the control.BOOL GetItem(_Out_ TVITEM* pItem) const;// Retrieves the text associated with the given item.CString GetItemText(_In_ HTREEITEM hItem) const;// Retrieves the images associated with the given item.BOOL GetItemImage(_In_ HTREEITEM hItem, _Out_ int& nImage,_Out_ int& nSelectedImage) const;// Retrieves the state of the given item.UINT GetItemState(_In_ HTREEITEM hItem, _In_ UINT nStateMask) const;// Retrieves the user-supplied data associated with the given item.DWORD_PTR GetItemData(_In_ HTREEITEM hItem) const;// Sets the state of the an item.BOOL SetItem(_In_ TVITEM* pItem);BOOL SetItem(_In_ HTREEITEM hItem, _In_ UINT nMask, _In_opt_z_ LPCTSTR lpszItem, _In_ int nImage,_In_ int nSelectedImage, _In_ UINT nState, _In_ UINT nStateMask, _In_ LPARAM lParam);BOOL SetItemEx(_In_ HTREEITEM hItem, _In_ UINT nMask, _In_opt_z_ LPCTSTR lpszItem, _In_ int nImage,_In_ int nSelectedImage, _In_ UINT nState, _In_ UINT nStateMask, _In_ LPARAM lParam,_In_ UINT uStateEx, _In_opt_ HWND hWnd, _In_ int iExpandedImage);// Sets the text of the specified item.BOOL SetItemText(_In_ HTREEITEM hItem, _In_z_ LPCTSTR lpszItem);// Sets the image on the specified item.BOOL SetItemImage(_In_ HTREEITEM hItem, _In_ int nImage, _In_ int nSelectedImage);// Sets the state of the specified item.BOOL SetItemState(_In_ HTREEITEM hItem, _In_ UINT nState, _In_ UINT nStateMask);// Sets the user data on the specified item.BOOL SetItemData(_In_ HTREEITEM hItem, _In_ DWORD_PTR dwData);// Determines if the specified item has children.BOOL ItemHasChildren(_In_ HTREEITEM hItem) const;// Gets the count of items presently visible in the control.UINT GetVisibleCount() const;// Retrieves the tool tip control associated with this control.CToolTipCtrl* GetToolTips() const;// Sets the tool tip control to be used by this control.CToolTipCtrl* SetToolTips(_In_ CToolTipCtrl* pWndTip);// Retrieves the background colour used throughout the control.COLORREF GetBkColor() const;// Sets the background color to be used throughout the control.COLORREF SetBkColor(_In_ COLORREF clr);// Retrieves the height of items in the control.SHORT GetItemHeight() const;// Sets the height of items in the control.SHORT SetItemHeight(_In_ SHORT cyHeight);// Retrieves the text color used for all items in the control.COLORREF GetTextColor() const;// Sets the text color used for all items in the control.COLORREF SetTextColor(_In_ COLORREF clr);// Gets the checked state of the specified item in the control.// (Only useful on a control with the TVS_CHECKBOXES style.)BOOL GetCheck(_In_ HTREEITEM hItem) const;// Sets the checked state of the specified item in the control.// (Only useful on a control with the TVS_CHECKBOXES style.)BOOL SetCheck(_In_ HTREEITEM hItem, _In_ BOOL fCheck = TRUE);COLORREF GetLineColor() const;COLORREF SetLineColor(_In_ COLORREF clrNew = CLR_DEFAULT);// Operations// Inserts a new item to the control.HTREEITEM InsertItem(_In_ LPTVINSERTSTRUCT lpInsertStruct);HTREEITEM InsertItem(_In_ UINT nMask, _In_z_ LPCTSTR lpszItem, _In_ int nImage,_In_ int nSelectedImage, _In_ UINT nState, _In_ UINT nStateMask, _In_ LPARAM lParam,_In_ HTREEITEM hParent, _In_ HTREEITEM hInsertAfter);HTREEITEM InsertItem(_In_z_ LPCTSTR lpszItem, _In_ HTREEITEM hParent = TVI_ROOT,_In_ HTREEITEM hInsertAfter = TVI_LAST);HTREEITEM InsertItem(_In_z_ LPCTSTR lpszItem, _In_ int nImage, _In_ int nSelectedImage,_In_ HTREEITEM hParent = TVI_ROOT, _In_ HTREEITEM hInsertAfter = TVI_LAST);// Removes the specified item from the control.BOOL DeleteItem(_In_ HTREEITEM hItem);// Removes all items from the control.BOOL DeleteAllItems();// Expands the children of the specified item.BOOL Expand(_In_ HTREEITEM hItem, _In_ UINT nCode);// Selects the specified item. 选中BOOL Select(_In_ HTREEITEM hItem, _In_ UINT nCode);// Selects the specified item.BOOL SelectItem(_In_opt_ HTREEITEM hItem);// Selects an item to be the drop target in the control.BOOL SelectDropTarget(_In_opt_ HTREEITEM hItem);// Draws the specified item as the drop target for the control.BOOL SelectSetFirstVisible(_In_ HTREEITEM hItem);// Begins editing the label of the specified item.CEdit* EditLabel(_In_ HTREEITEM hItem);
TVN_ENDEDIT 消息时获取编辑控件,把用户输入的文字取出来// Retrieves the edit control used to perform in-place editing.CEdit* GetEditControl() const;
};

树形控件的属性

#define TVS_HASBUTTONS          0x0001
#define TVS_HASLINES       有虚线     0x0002
#define TVS_LINESATROOT         0x0004			#define TVS_EDITLABELS          0x0008
#define TVS_DISABLEDRAGDROP     0x0010
#define TVS_SHOWSELALWAYS       0x0020
#define TVS_RTLREADING          0x0040
#define TVS_NOTOOLTIPS          0x0080
#define TVS_CHECKBOXES          0x0100
#define TVS_TRACKSELECT         0x0200
#define TVS_SINGLEEXPAND        0x0400
#define TVS_INFOTIP             0x0800
#define TVS_FULLROWSELECT       0x1000
#define TVS_NOSCROLL            0x2000
#define TVS_NONEVENHEIGHT       0x4000
#define TVS_NOHSCROLL           0x8000  // TVS_NOSCROLL overrides this
#if (NTDDI_VERSION >= NTDDI_WINXP)
#define TVS_EX_NOSINGLECOLLAPSE     0x0001
#endif
#if (NTDDI_VERSION >= NTDDI_VISTA)
#define TVS_EX_MULTISELECT          0x0002
#define TVS_EX_DOUBLEBUFFER         0x0004
#define TVS_EX_NOINDENTSTATE        0x0008
#define TVS_EX_RICHTOOLTIP          0x0010
#define TVS_EX_AUTOHSCROLL          0x0020
#define TVS_EX_FADEINOUTEXPANDOS    0x0040
#define TVS_EX_PARTIALCHECKBOXES    0x0080
#define TVS_EX_EXCLUSIONCHECKBOXES  0x0100
#define TVS_EX_DIMMEDCHECKBOXES     0x0200
#define TVS_EX_DRAWIMAGEASYNC       0x0400
#endif

CTreeCtrl类的原理以及常用功能

CTreeCtrl类的选中消息分析:
a)结构体:

typedef struct tagTVITEMCHANGE {NMHDR hdr;UINT uChanged;HTREEITEM hItem;UINT uStateNew;UINT uStateOld;LPARAM lParam;
} NMTVITEMCHANGE;

b)State状态分析:

TVIS_SELECTED(0x0002):项被选中。
TVIS_CUT(0x0008):项被剪切。
TVIS_DROPHILITED(0x0004):项高亮显示,用于拖放操作。
TVIS_EXPANDED(0x0020):项展开。
TVIS_BOLD(0x0001):项以粗体显示。
TVIS_DISABLED(0x0040):项禁用(灰色显示)。
TVIS_EXPANDEDONCE(0x0200):项已经展开过一次。
TVIS_EXPANDPARTIAL(0x0400):项部分展开。
TVIS_OVERLAYMASK(0x0F00):覆盖层蒙版。

MFC六大关键技术

1、MFC Initialization —— MFC程序的初始化过程
参见:CWinApp::InitInstance的虚函数,MFC内部接管WinMain平台启动初始化之后再调用InitInstance。
开发者需要创建CWinApp的派生类,并且在全局区定义派生类的全局对象,最后在派生类礼重写InitInstance虚函数。

2、Message Mapping —— 消息映射
参见:任何窗口派生类的头文件中 :DECLARE_MESSAGE_MAP
源文件中:
BEGIN_MESSAGE_MAP(CMainDlg, CDialogEx)
消息映射的数组{数组的每个元素都是一个结构体(消息-ID-函数地址)}
END_MESSAGE_MAP()

3、Message Routing —— 消息传递(路由)
主要在框架与视图架构里,把框架收到的菜单和工具栏消息分发到各个视图类。

BOOL CMainFrame::OnCmdMsg(UINT nID...)if (m_pView && m_pView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))return TRUE;

4、RTTI(Runtime Type Identification)—— 运行时类型识别
核心的作用是任何MFC的类(CObject派生)都能够获取到类型信息。
而且能知道你的派生类是谁,甚至获取到整个派生树分枝的名字。

auto pInfo = obj.GetRuntimeClass();
while (pInfo)
{AfxMessageBox(CString(_T("你是:")) + pInfo->m_lpszClassName);pInfo = pInfo->m_pBaseClass;//pNext;
}

参见:RUNTIME_CLASS宏以及每个MFC类的信息管理:
DECLARE_DYNAMIC(CMainFrame)
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
RTTI运行时识别类型,靠的就是

BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const;
CRuntimeClass* pClassThis = GetRuntimeClass();

5、Dynamic Creation —— 动态创建
比动态类型识别多了一个函数:
原理参见:DECLARE_DYNCREATE(CApp)

IMPLEMENT_DYNCREATE(CApp, CWinApp)
class CApp::public CWinApp
{
public:
static CObject* WINAPI CApp::CreateObject() 	{ return new CApp; } 
}

6、Persistence ——永久保存(串行化、序列化)
利用CArchive类将对象数据保存到磁盘上,把数据按照顺序连续(operator<<)存入到磁盘,加载时按照存入的顺序连续提取数据(operator>>);
优点是比结构体在存储文字时节省存储空间,存储文字时先存编码和长度,加载时再根据长度读取文字,并根据来源编码格式转换到目标格式。
MFC这种连续存储的机制称之为序列化存储(Serialize)。

视图和带分割栏的框架开发与消息路由

CLeftView.h

class CLeftView : public CTreeView{DECLARE_DYNCREATE(CLeftView) //动态创建CTreeCtrl& m_tree {	GetTreeCtrl() };CImageList m_iList;int GetDepth(HTREEITEM hItem) const;
protected:CLeftView();           // 动态创建所使用的受保护的构造函数virtual ~CLeftView();void InitCtrl();
protected:DECLARE_MESSAGE_MAP()
};

CLeftView.cpp

#include "pch.h"
#include "division.h"
#include "CLeftView.h"
#include "CRightView.h"
#include "CWelcome.h"
IMPLEMENT_DYNCREATE(CLeftView, CTreeView)
CLeftView::CLeftView()
{theApp.m_pLeftView = this;
}
CLeftView::~CLeftView()
{}
BEGIN_MESSAGE_MAP(CLeftView, CTreeView)ON_NOTIFY_REFLECT(TVN_SELCHANGED, &CLeftView::OnTvnSelchanged)ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, &CLeftView::OnTvnItemexpanded)ON_NOTIFY_REFLECT(TVN_ITEMCHANGED, &CLeftView::OnTvnItemChanged)ON_COMMAND(ID_COMP_ADD, &CLeftView::OnCompAdd)ON_COMMAND(ID_COMP_DEL, &CLeftView::OnCompDel)
END_MESSAGE_MAP()
// CLeftView 诊断
#ifdef _DEBUG
void CLeftView::AssertValid() const{CTreeView::AssertValid();
}
// CLeftView 消息处理程序
void CLeftView::OnInitialUpdate()
{CTreeView::OnInitialUpdate();if (m_iList.GetSafeHandle())return;InitCtrl();ModifyStyle(0, TVS_HASLINES| TVS_HASBUTTONS | TVS_LINESATROOT | TVS_SHOWSELALWAYS);auto hItem = m_tree.InsertItem(_T("异人之下"),0,1);auto h = m_tree.InsertItem(_T("那都通"),2,3, hItem);m_tree.InsertItem(_T("张楚岚"),5,4,h);m_tree.InsertItem(_T("冯宝宝"),5,4,h);m_tree.InsertItem(_T("徐三"),  5,4,h);m_tree.InsertItem(_T("徐四"),  5,4,h);m_tree.Expand(h, TVE_EXPAND);auto h2 = m_tree.InsertItem(_T("武当山"),2,3 ,hItem);m_tree.InsertItem(_T("王也"),  5,4, h2);m_tree.InsertItem(_T("老天师"),5,4, h2);m_tree.InsertItem(_T("诸葛青"),5,4, h2);m_tree.InsertItem(_T("诸葛玄"),5,4, h2);m_tree.Expand(h2, TVE_EXPAND);m_tree.Expand(hItem, TVE_EXPAND); //要先插入再展开//m_tree.Select(h2, TVGN_CARET); //设置当前选择 并且带有样式m_tree.SelectItem(h); //设置当前选择 没有样式
}
void CLeftView::InitCtrl(){m_iList.Create(16, 16, ILC_COLOR32, 8, 8);int i = -1;while (++i < 6){m_iList.Add(theApp.LoadIcon(IDI_ICON1 + i));}m_tree.SetImageList(&m_iList, TVSIL_NORMAL);
}
int CLeftView::GetDepth(HTREEITEM hItem) const  //用于获取当前选中的行在 多深
{if (!hItem)return -1;int i = 0;while (hItem = m_tree.GetParentItem(hItem)){++i;}return i;
}
void CLeftView::OnTvnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);// TODO: 在此添加控件通知处理程序代码*pResult = 0;
}
void CLeftView::OnTvnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult) //展开或者折叠
{LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);*pResult = 0;
}
void CLeftView::OnTvnItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
{NMTVITEMCHANGE* p  = reinterpret_cast<NMTVITEMCHANGE*>(pNMHDR);auto hItem = p->hItem;if (TVIS_SELECTED & p->uStateNew){auto& split = theApp.m_split;auto pRightView = theApp.m_split.GetPane(0, 1);	 if (pRightView)split.DeleteView(0,1);switch (GetDepth(hItem)){case 0:case 1:split.CreateView(0, 1, RUNTIME_CLASS(CWelcome), { 0,0 }, NULL); break;case 2:{split.CreateView(0, 1, RUNTIME_CLASS(CRightView), { 0,0 }, NULL); CRightView* pRightView =(CRightView*) split.GetPane(0, 1);pRightView->Refresh();}}split.RecalcLayout();}*pResult = 0;
}

CRightView.h

class CRightView : public CListView{DECLARE_DYNCREATE(CRightView)//动态创建CListCtrl& m_list = GetListCtrl(); //说白了任何一个控件的创建都是由CWnd::Create(sCLassName)
protected:CRightView();           // 动态创建所使用的受保护的构造函数virtual ~CRightView();
public:void Refresh();
protected:DECLARE_MESSAGE_MAP()
public:afx_msg void OnStaffAdd();afx_msg void OnStaffDel();virtual void OnInitialUpdate();virtual BOOL PreCreateWindow(CREATESTRUCT& cs);	};

CRightView.cpp

#include "pch.h"
#include "division.h"
#include "CRightView.h"
IMPLEMENT_DYNCREATE(CRightView, CListView)
CRightView::CRightView(){theApp.m_pRightView = this;
}
CRightView::~CRightView()
{theApp.m_pRightView = nullptr;
}
BEGIN_MESSAGE_MAP(CRightView, CListView)ON_COMMAND(ID_STAFF_ADD, &CRightView::OnStaffAdd)ON_COMMAND(ID_STAFF_DEL, &CRightView::OnStaffDel)
END_MESSAGE_MAP()
void CRightView::Refresh()
{
}
void CRightView::AssertValid() const
{CListView::AssertValid();
}
void CRightView::OnStaffAdd()
{}
void CRightView::OnStaffDel()
{}
void CRightView::OnInitialUpdate(){CListView::OnInitialUpdate();if (m_list.GetHeaderCtrl()->GetItemCount() > 0) //防止标题插入两边return;m_list.InsertColumn(0, _T("工号"), 0, 150);m_list.InsertColumn(1, _T("姓名"), 0, 150);m_list.InsertColumn(2, _T("性别"), 0, 150);m_list.InsertColumn(3, _T("入职日期"), 0, 150);m_list.InsertColumn(4, _T("外语"), 0, 150);m_list.InsertColumn(5, _T("工资"), 0, 150);
}
BOOL CRightView::PreCreateWindow(CREATESTRUCT& cs)
{  cs.style |= LVS_REPORT;return CListView::PreCreateWindow(cs);
}

CWelcome .h

class CWelcome : public CFormView{DECLARE_DYNCREATE(CWelcome)
protected:CWelcome();           // 动态创建所使用的受保护的构造函数virtual ~CWelcome();protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持DECLARE_MESSAGE_MAP()
};

CWelcome.cpp

#include "pch.h"
#include "division.h"
#include "CWelcome.h"
IMPLEMENT_DYNCREATE(CWelcome, CFormView)CWelcome::CWelcome(): CFormView(IDD_FORMVIEW)
{theApp.m_pRightView = this;
}
CWelcome::~CWelcome()
{theApp.m_pRightView = NULL;
}
BEGIN_MESSAGE_MAP(CWelcome, CFormView)
END_MESSAGE_MAP()

division.h

#include "resource.h"       // 主符号
#include "CLeftView.h"
// CApp:有关此类的实现,请参阅 division.cpp
class CLeftView;
class CRightView;
class CApp : public CWinApp
{
public:CApp() noexcept;CLeftView* m_pLeftView{};CView* m_pRightView{};CSplitterWnd m_split;
};
extern CApp theApp;

division.cpp

#include "pch.h"
#include "framework.h"
#include "afxwinappex.h"
#include "afxdialogex.h"
#include "division.h"
#include "MainFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CApp
BEGIN_MESSAGE_MAP(CApp, CWinApp)ON_COMMAND(ID_APP_ABOUT, &CApp::OnAppAbout)
END_MESSAGE_MAP()
BOOL CApp::InitInstance(){			// CApp 初始化CFrameWnd* pFrame = new CMainFrame;if (!pFrame)	return FALSE;m_pMainWnd = pFrame; 	// 创建并加载框架及其资源pFrame->LoadFrame(IDR_MAINFRAME,WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, nullptr,nullptr);if (m_pRightView->IsKindOf(RUNTIME_CLASS(CCtrlView)))AfxMessageBox(_T("m_pRightView对象是CCtrlView派生的"));elseAfxMessageBox(_T("m_pRightView对象不是CCtrlView派生的"));if (m_pRightView->IsKindOf(RUNTIME_CLASS(CFormView)))AfxMessageBox(_T("m_pRightView对象是CFormView派生的"));elseAfxMessageBox(_T("m_pRightView对象不是CFormView派生的"));pFrame->ShowWindow(SW_SHOW);	// 唯一的一个窗口已初始化,因此显示它并对其进行更新pFrame->UpdateWindow();				return TRUE;				}
// 唯一的 CApp 对象
CApp theApp;// 用于运行对话框的应用程序命令
void CApp::OnAppAbout()
{CAboutDlg aboutDlg;aboutDlg.DoModal();
}

MainFrm.h

class CMainFrame : public CFrameWnd{	 
public:CMainFrame() noexcept;
protected: DECLARE_DYNAMIC(CMainFrame)  //动态识别
public:				// 重写virtual BOOL PreCreateWindow(CREATESTRUCT& cs);virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
public:  // 实现virtual ~CMainFrame();
protected:afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);afx_msg void OnSetFocus(CWnd *pOldWnd);DECLARE_MESSAGE_MAP()virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);		};

MainFrm.cpp

#include "pch.h"
#include "framework.h"
#include "division.h"
#include "CLeftView.h"
#include "CRightView.h"
#include "MainFrm.h"// CMainFrame 构造/析构
CMainFrame::CMainFrame() noexcept
{// TODO: 在此添加成员初始化代码
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CFrameWnd::OnCreate(lpCreateStruct) == -1)return -1;if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))	{TRACE0("未能创建工具栏\n");return -1;      // 未能创建	}if (!m_wndStatusBar.Create(this))	{TRACE0("未能创建状态栏\n");return -1;      // 未能创建	}m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);// TODO: 如果不需要可停靠工具栏,则删除这三行EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);		return 0;	}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{if( !CFrameWnd::PreCreateWindow(cs) )return FALSE;// TODO: 在此处通过修改//  CREATESTRUCT cs 来修改窗口类或样式cs.dwExStyle &= ~WS_EX_CLIENTEDGE;cs.lpszClass = AfxRegisterWndClass(0);return TRUE;
}
// CMainFrame 消息处理程序void CMainFrame::OnSetFocus(CWnd* /*pOldWnd*/)
{// 将焦点前移到视图窗口if (theApp.m_pLeftView)theApp.m_pLeftView->SetFocus();
}
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{// 让视图第一次尝试该命令if (theApp.m_pLeftView &&theApp.m_pLeftView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))return TRUE;if (theApp.m_pRightView &&theApp.m_pRightView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))return TRUE;// 否则,执行默认处理return CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext){auto& split = theApp.m_split;split.CreateStatic(this, 1, 2);split.CreateView(0, 1, RUNTIME_CLASS(CRightView), { 0,0 }, NULL); //righrview 要在leftview之前  因为在leftview中调用rightview读取数据时,rightview还没有创建,拿不到数据 报它为空split.CreateView(0, 0, RUNTIME_CLASS(CLeftView), { 150,0 }, NULL);//theApp.m_pLeftView = (CLeftView*)split.GetPane(0, 0);  //做这个的目的为了是 点击左边的时候找右边方便 点击右边的时候找左边方便
//theApp.m_pRightView = (CRightView*)split.GetPane(0, 1);//给这个指针赋值也可通过构造函数赋值//CCreateContext cc;//cc.m_pNewViewClass = RUNTIME_CLASS(CLeftView);//theApp.m_pLeftView =(CLeftView*) CreateView(&cc);return TRUE;
}

在这里插入图片描述

附录:为什么用户点击一下视图,即可让菜单有效?

CView* pView = GetActiveView();

BOOL CFrameWnd::OnCmdMsg(UINT nID, int nCode, void* pExtra,AFX_CMDHANDLERINFO* pHandlerInfo){CPushRoutingFrame push(this);// pump through current view FIRSTCView* pView = GetActiveView();if (pView != NULL && pView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))return TRUE;// then pump through frameif (CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))return TRUE;// last but not least, pump through appCWinApp* pApp = AfxGetApp();if (pApp != NULL && pApp->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))return TRUE;return FALSE;
}

相关文章:

MFC第二十九天 CView类的分支(以及其派生类的功能)、MFC六大关键技术

文章目录 CView类的分支CEditViewCHtmlViewMainFrm.h CMainFrame 类的接口CMainView .h CListCtrl与CListView的创建原理 CTreeViewCTreeCtrl类简介CTreeCtrl类的原理以及常用功能 MFC六大关键技术视图和带分割栏的框架开发与消息路由CLeftView.cppCRightView.hCRightView.cppC…...

SpringBoot复习:(37)自定义ErrorController

所有接口统一返回的数据格式 package cn.edu.tju.domain;public class MyResponse {private int code;private String message;private String exception;private String stack;public int getCode() {return code;}public void setCode(int code) {this.code code;}public S…...

Linux学习之防火墙概述

防火墙分类&#xff1a; 软件防火墙&#xff1a;常用于数据包的过滤&#xff0c;比如限制某些ip或者端口&#xff0c;进行某些数据的转发或者传送 硬件防火墙&#xff1a;防御地域攻击 软件防火墙的分类&#xff1a; 包过滤防火墙&#xff1a;控制比较宽泛&#xff0c;防御效果…...

JS_围绕圆形滑动

需求&#xff1a;滑动手势最大不能超过一个半径为50的圆形&#xff0c;超出围绕圆形边线滑动 这里只提供一个思路&#xff0c;下面代码可以运行&#xff0c;但是要使用需要改成自己的参数 <div style"width: 100%;height: 100vh;display: flex;justify-content: cente…...

Ubuntu上安装RabbitMQ

在Ubuntu上安装RabbitMQ并设置管理员用户为"admin"&#xff0c;密码为"123456"&#xff0c;并开启开机自启 更新系统软件包列表。在终端中执行以下命令&#xff1a; sudo apt update安装RabbitMQ服务器软件包。运行以下命令&#xff1a; sudo apt insta…...

统计学和机器学习之间的联系和区别

一、说明 老实说&#xff0c;我厌倦了几乎每天都在社交媒体和我的大学里听到这场辩论。通常&#xff0c;这伴随着一些模糊的陈述来解释这个问题。双方都为此感到内疚。我希望在本文结束时&#xff0c;您将对这些有些模糊的术语有更明智的立场。 二、论点 与普遍的看法相反&…...

linux中profile.d和profile的区别

profile.d在profile中加载 profile文件 PATH"/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/scripts:/soc/bin:/soc/scripts" LD_LIBRARY_PATH"/usr/local/lib:/usr/lib:/opt/lib:/soc/lib" export SSL_LDPATH/usr/local/lib/ export ZLIB_LDPATH/usr/lo…...

MobaXterm sftp 不能拖拽文件夹了?

原因是我把mobaxterm设置成Windows管理员权限运行了,结果就不能拖动文件。把管理员权限去掉就恢复正常了。 原因是我把mobaxterm设置成Windows管理员权限运行了,结果就不能拖动文件。把管理员权限去掉就恢复正常了。 原因是我把mobaxterm设置成Windows管理员权限运行了,结果就不…...

【ArcGIS Pro二次开发】(59):Editing(编辑)模块

ArcGIS Pro SDK中的"Editing"&#xff08;编辑&#xff09;模块提供了一系列API和工具&#xff0c;允许开发人员在ArcGIS Pro中实现各种地图数据编辑操作&#xff0c;从简单的要素绘制到复杂的数据集编辑。 下面列举一些常用编辑工具的实现方法。 1、获取所选要素的…...

WebSocket与消息推送

B/S结构的软件项目中有时客户端需要实时的获得服务器消息&#xff0c;但默认HTTP协议只支持请求响应模式&#xff0c;这样做可以简化Web服务器&#xff0c;减少服务器的负担&#xff0c;加快响应速度&#xff0c;因为服务器不需要与客户端长时间建立一个通信链接&#xff0c;但…...

5.1 web浏览安全

数据参考&#xff1a;CISP官方 目录 Web应用基础浏览器所面临的安全威胁养成良好的Web浏览安全意识如何安全使用浏览器 一、Web应用基础 1、Web应用的基本概念 Web ( World wide Web) 也称为万维网 脱离单机Web应用在互联网上占据了及其重要的地位Web应用的发展&#xf…...

(六)Unity开发Vision Pro——词汇表

1.PolySpatial Core (com.unity.polyspatial)&#xff1a;基础 PolySpatial 包&#xff0c;初始化和所有设置从这里开始。它执行更改跟踪和处理、序列化/反序列化&#xff0c;并包括 ShaderGraph 到 MaterialX 转换器 2.PolySpatial XR (com.unity.polyspatial.xr)&#xff1a…...

算法随笔:图论问题之割点割边

割点 定义 割点的定义&#xff1a;如果一个点被删除之后会导致整个图不再是一个连通图&#xff0c;那么这个顶点就是这个图的割点。举例&#xff1a; 上图中的点2就是一个割点&#xff0c;如果它被删除&#xff0c;则整个图被分为两个连通分量&#xff0c;不再是一个连通图。…...

【虚幻引擎】UE5数字人的创建

安装插件 在插件里面找到MetaHuman&#xff0c;设置激活&#xff0c;然后重启引擎 找到bridge&#xff0c;并开启&#xff0c;这个需要我们制作完成的metahuman需要在这个插件里下载&#xff0c;unreal5自动安装 创建metahuman 首先添加一个metahuman本体&#xff0c;如果你的插…...

算法:深度优先遍历

文章目录 什么是深搜典型题目积累 本篇主要积累的是深度优先遍历算法 什么是深搜 深度优先搜索英文缩写为 DFS 即Depth First Search 其过程是对每一个可能的分支路径深入到不能再深入为止&#xff0c;而且每个节点只能访问一次 简单来说就是: 一路走到头&#xff0c;不撞墙…...

Stable Diffusion + Deform制作指南

1.安装sd以及deform插件,更新后记得重启 需要安装ffmpeg https://ffmpeg.org/download.html 选择对应版本然后安装 如果是windows需要解压后将ffmpeg的bin目录配置在电脑的环境变量里面。 2.准备一张初始开始图片 3.填写参数,这里面参数要注意,宽高一定是32的倍数。如果填写…...

ssm+vue网上花店设计源码和论文

ssmvue网上花店设计源码和论文017 开发工具&#xff1a;idea 数据库mysql5.7 数据库链接工具&#xff1a;navcat,小海豚等 技术&#xff1a;ssm 摘 要 网络技术和计算机技术发展至今&#xff0c;已经拥有了深厚的理论基础&#xff0c;并在现实中进行了充分运用&#xf…...

【leetcode】第一章数组

704. 二分查找 边界值需注意left代表左边界下标值&#xff0c;right代表右边界的下标值当数组只有一个元素时&#xff0c;此时如果找到该元素应该返回下标0&#xff0c;因此条件为left<right当mid的元素值大于target时&#xff0c;此时说明我们想找的target在右边&#xff…...

01|Java中常见错误或不清楚

补充&#xff1a;length vs length() vs size() 1 java中的length属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了length这个属性. 2 java中的length()方法是针对字符串String说的,如果想看这个字符串的长度则用到length()这个方法. 3.java中的siz…...

递归的用法和例子

b站视频&#xff1a;https://www.bilibili.com/video/BV1iS4y1e7MJ/?spm_id_from333.999.0.0&vd_source1717654b9cbbc6a773c2092070686a95 # 递归的定义&#xff1a;其实就是自己调用自己&#xff0c;一般用函数的形式来进行 """ 特点&#xff1a; 1、一定…...

Leetcode 3576. Transform Array to All Equal Elements

Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接&#xff1a;3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到&#xf…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

MySQL 隔离级别:脏读、幻读及不可重复读的原理与示例

一、MySQL 隔离级别 MySQL 提供了四种隔离级别,用于控制事务之间的并发访问以及数据的可见性,不同隔离级别对脏读、幻读、不可重复读这几种并发数据问题有着不同的处理方式,具体如下: 隔离级别脏读不可重复读幻读性能特点及锁机制读未提交(READ UNCOMMITTED)允许出现允许…...

通过Wrangler CLI在worker中创建数据库和表

官方使用文档&#xff1a;Getting started Cloudflare D1 docs 创建数据库 在命令行中执行完成之后&#xff0c;会在本地和远程创建数据库&#xff1a; npx wranglerlatest d1 create prod-d1-tutorial 在cf中就可以看到数据库&#xff1a; 现在&#xff0c;您的Cloudfla…...

IGP(Interior Gateway Protocol,内部网关协议)

IGP&#xff08;Interior Gateway Protocol&#xff0c;内部网关协议&#xff09; 是一种用于在一个自治系统&#xff08;AS&#xff09;内部传递路由信息的路由协议&#xff0c;主要用于在一个组织或机构的内部网络中决定数据包的最佳路径。与用于自治系统之间通信的 EGP&…...

从深圳崛起的“机器之眼”:赴港乐动机器人的万亿赛道赶考路

进入2025年以来&#xff0c;尽管围绕人形机器人、具身智能等机器人赛道的质疑声不断&#xff0c;但全球市场热度依然高涨&#xff0c;入局者持续增加。 以国内市场为例&#xff0c;天眼查专业版数据显示&#xff0c;截至5月底&#xff0c;我国现存在业、存续状态的机器人相关企…...

连锁超市冷库节能解决方案:如何实现超市降本增效

在连锁超市冷库运营中&#xff0c;高能耗、设备损耗快、人工管理低效等问题长期困扰企业。御控冷库节能解决方案通过智能控制化霜、按需化霜、实时监控、故障诊断、自动预警、远程控制开关六大核心技术&#xff0c;实现年省电费15%-60%&#xff0c;且不改动原有装备、安装快捷、…...

如何为服务器生成TLS证书

TLS&#xff08;Transport Layer Security&#xff09;证书是确保网络通信安全的重要手段&#xff0c;它通过加密技术保护传输的数据不被窃听和篡改。在服务器上配置TLS证书&#xff0c;可以使用户通过HTTPS协议安全地访问您的网站。本文将详细介绍如何在服务器上生成一个TLS证…...

unix/linux,sudo,其发展历程详细时间线、由来、历史背景

sudo 的诞生和演化,本身就是一部 Unix/Linux 系统管理哲学变迁的微缩史。来,让我们拨开时间的迷雾,一同探寻 sudo 那波澜壮阔(也颇为实用主义)的发展历程。 历史背景:su的时代与困境 ( 20 世纪 70 年代 - 80 年代初) 在 sudo 出现之前,Unix 系统管理员和需要特权操作的…...

vue3+vite项目中使用.env文件环境变量方法

vue3vite项目中使用.env文件环境变量方法 .env文件作用命名规则常用的配置项示例使用方法注意事项在vite.config.js文件中读取环境变量方法 .env文件作用 .env 文件用于定义环境变量&#xff0c;这些变量可以在项目中通过 import.meta.env 进行访问。Vite 会自动加载这些环境变…...