28上位机VC MFC实现视图窗口的分割效果
28上位机VC MFC实现视图窗口的分割效果
功能展示
在文档视图结构的程序中,有们有时需要将视图进行分割,从而使视图显示不同的信息,使我们开发的上位机程序具有更多的功能和更简化的操作,我们当前就实现了这一功能,效果如图
要点提示 通过CSplitterWnd类可以实现视图分割效果,当改变分割窗口的大小 时,窗口的客户区将自动重新绘制, CSplitterWnd有两个重要函数: CreateStatic()和CreateView();
CreateStatic()语法BOOL CreateStatic( CWnd* pParentWnd, int nRows, int nCols, DWORD dwStyle = WS_CHILD | WS_VISIBLE, UINT nID = AFX_IDW_PANE_FIRST ); pParentWnd: 分割窗口父窗口;nRows, nCols:分割窗口的行数与列数不得超过16个; dwStyle分割窗口样式; nID分割窗口资源ID;
CreateView()语法virtual BOOL CreateView( int row, int col, CRuntimeClass* pViewClass, SIZE sizeInit, CCreateContext* pContext ); Row, col子视窗所在行与列; pViewClass新视窗的CRuntimeClass对象; sizeInit视窗初始大小; CCreateContext 指向CCreateContext的结构指针;
在我们创建了自己的分割窗口后,把文档默认的视窗更换便可;
实现功能
1.新建基于单文档的应用程序 2.根据自己的需要创建几个RUNTIME_CLASS类型的类。我们以当前例程为例,建立左边的CGKBC8TreeView树状视窗类,和右边的CGKBC8ListView列表视窗类;
3.在CGKBC8TreeView类的OnCreate()中初始化 - <font size="4">int CGKBC8TreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CTreeView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- // TODO: Add your specialized creation code here
- m_ImageList.Create(16,16,ILC_COLOR32|ILC_MASK,0,0);
- CTreeCtrl *pTreeCtl=&GetTreeCtrl();
- pTreeCtl->SetImageList(&m_ImageList,TVSIL_NORMAL);
- pTreeCtl->ModifyStyle(0L,TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS);
- for(int i=0;i<11;i++)
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1+i));
- HTREEITEM item;
- pTreeCtl->DeleteAllItems();
- item=pTreeCtl->InsertItem("基本信息",0,0,TVI_ROOT);
- pTreeCtl->InsertItem("课程名称",1,1,item);
- pTreeCtl->InsertItem("课程时长",2,2,item);
- pTreeCtl->InsertItem("课程讲师",3,3,item);
- </font>
复制代码m_ImageList 为我们手动添加的CImageList成员变量,IDI_ICON为一些手动添加的图标ID;
4.在CGKBC8ListView类的OnCreate()中初始化 - <font size="4">int CGKBC8ListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- // TODO: Add your specialized creation code here
- CString strdata[5][4]=
- {
- "0","欧姆龙PLC编程","1200分钟","工老师",
- "1","上位机编程入门","800分钟","控老师",
- "2","三菱触摸屏编程","500分钟","编老师",
- "3","西门子S7-200编程","1900分钟","程老师",
- "4","台达PLC编程教程","100分钟","吧老师"
- };
- CListCtrl *pListCtl=&GetListCtrl();
- pListCtl->DeleteAllItems();
- pListCtl->ModifyStyle(0L,LVS_REPORT);
- pListCtl->SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
- pListCtl->InsertColumn(0,"索引",LVCFMT_LEFT,100);
- pListCtl->InsertColumn(1,"课程名称",LVCFMT_LEFT,150);
- pListCtl->InsertColumn(2,"课时时长",LVCFMT_LEFT,150);
- pListCtl->InsertColumn(3,"课程讲师",LVCFMT_LEFT,150);
- </font>
复制代码5.在准备好了自建立的两个类后,我们把APP的InitInstance()中 pDocTemplate的视窗类进行屏蔽,改成NULL;,然后在框架类CMainFrame() OnCreateClient()创建我们自定义的分割窗口 - <font size="4">BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
- {
- // TODO: Add your specialized code here and/or call the base class
- m_SplitWnd.CreateStatic(this,1,2);
- m_SplitWnd.CreateView(0,1,RUNTIME_CLASS(CGKBC8ListView),CSize(150,100),pContext);
- m_SplitWnd.CreateView(0,0,RUNTIME_CLASS(CGKBC8TreeView),CSize(200,100),pContext);
- return CFrameWnd::OnCreateClient(lpcs, pContext);
- </font>
复制代码我们事先已手动添加了框架类的成员变量CSplitterWnd m_SplitWnd;
下面我们演示整个功能实现过程
源码及视频下载 (仅在电脑可见)
|