实现单文档程序中分割窗口

经过多方寻找资料和反复试验,终于实现了在单文档程序中分割窗口,并且可以对视图进行随意切换,以下是比较详尽的步骤:

  1,向导第四步,选高级,“使用分割栏”挑勾,原始VIEW类为CMyView,派生自CVIEW类   2,要想加入从其他VIEW类派生的类,如CFormView等,应该在StdAfx.h中加入#include <afxcview.h>   3,加入新类CTView,派生自CTreeView,编辑初始化CTView::OnInitialUpdate() 代码如下        void CTView::OnInitialUpdate() {  CTreeView::OnInitialUpdate();

 // TODO: Add your specialized code here and/or call the base class  CTreeCtrl &treeCtrl=GetTreeCtrl();  DWORD dwStyle=::GetWindowLong(treeCtrl.m_hWnd,GWL_STYLE);  dwStyle|=TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT;  ::SetWindowLong(treeCtrl.m_hWnd,GWL_STYLE,dwStyle);  HTREEITEM hRoot,hCurPos;  TV_INSERTSTRUCT tInsert;  tInsert.hParent=TVI_ROOT;  tInsert.hInsertAfter=TVI_LAST;  tInsert.item.mask=TVIF_TEXT|TVIF_PARAM;     tInsert.item.pszText="分类";  tInsert.item.lParam=0;  hRoot=treeCtrl.InsertItem(&tInsert);  char *plant[4]={"编程","小说","科学","人文"};  char *cell[4][5]={         {"VC++","Delphi","BCB","",""},//主系统运行日志   {"武侠","侦探","言情","恐怖","悬疑"},   {"天文","地理","自然","",""},   {"社会科学","","","",""}  };  int i,j;  for(i=0;i<4;i++)  {   tInsert.hParent=hRoot;   tInsert.item.pszText=plant[i];   hCurPos=treeCtrl.InsertItem(&tInsert);   for(j=0;j<5;j++)   {    tInsert.hParent=hCurPos;    if(cell[i][j]!="")    {         tInsert.item.pszText=cell[i][j];      treeCtrl.InsertItem(&tInsert);}   }   //treeCtrl.Expand(hCurPos,TVE_EXPAND);

 }  treeCtrl.Expand(hRoot,TVE_EXPAND);    }

  4,加入CFormView派生类,先加入一个对话框资源,在对话框属性中,指定STYLES->STYLE:child BORDER:NONE,不选Title Bar 在More Styles中不选可见。然后在新建的CFormView的派生类CFView中指定对话框为刚建的对话框

  5,因为每个CView派生类都已经继承了GetDocument()函数,因此只要在调用时直接调用无需再在其中声明GetDocument()函数了,调用后再进行类型强制转换应该就可以了。比方,在cmyview.h中注释掉 // Attributes // CMyDoc* GetDocument(); 在cmyview.cpp中注释掉 //CMyDoc* CMyView::GetDocument() // non-debug version is inline //{  //ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));  //return (CMyDoc*)m_pDocument; //}

并在OnDraw中试用如下代码 void CMyView::OnDraw(CDC* pDC) {  CMyDoc* pDoc =(CMyDoc*)CMyView:: GetDocument();  ASSERT_VALID(pDoc);  // TODO: add draw code for native data here }

  7,为了使新加的view派生类能够被方便的调用,可以将这些view派生类的构造函数由protected改为public

  8,加入一个新类CMySplitter,派生自CMDIChildWnd类,然后将代码中所有的CMDIChildWnd改成CSplitterWnd 在MainFrm.h中加入#include "MySplitter.h",并将CSplitterWnd m_wndSplitter;改成 CMySplitter m_wndSplitter;

  9,给CMySplitter类加入一个publice型成员函数    BOOL ReplaceView(int row, int col,CRuntimeClass * pViewClass,SIZE size);        编辑代码如下    

BOOL CMySplitter::ReplaceView(int row, int col, CRuntimeClass *pViewClass, SIZE size) { CCreateContext context;   BOOL bSetActive;              if ((GetPane(row,col)->IsKindOf(pViewClass))==TRUE)        return FALSE;               // Get pointer to CDocument object so that it can be used in the creation    // process of the new view    CDocument * pDoc= ((CView *)GetPane(row,col))->GetDocument();    CView * pActiveView=GetParentFrame()->GetActiveView();    if (pActiveView==NULL || pActiveView==GetPane(row,col))       bSetActive=TRUE;    else       bSetActive=FALSE;

    // set flag so that document will not be deleted when view is destroyed  pDoc->m_bAutoDelete=FALSE;        // Delete existing view    ((CView *) GetPane(row,col))->DestroyWindow();     // set flag back to default     pDoc->m_bAutoDelete=TRUE;       // Create new view                            context.m_pNewViewClass=pViewClass;    context.m_pCurrentDoc=pDoc;    context.m_pNewDocTemplate=NULL;    context.m_pLastView=NULL;    context.m_pCurrentFrame=NULL;       CreateView(row,col,pViewClass,size, &context);       CView * pNewView= (CView *)GetPane(row,col);       if (bSetActive==TRUE)       GetParentFrame()->SetActiveView(pNewView);       RecalcLayout();    GetPane(row,col)->SendMessage(WM_PAINT);       return TRUE; }

    10,在MainFrm.cpp前边加上     #include "AppDoc.h"     #include "TView.h"     #include "MyView.h"     #include "FView.h" 在MainFrm.cpp 中编辑虚拟函数BOOL CMainFrame::OnCreateClient如下      BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,  CCreateContext* pContext) {

  if (!m_wndSplitter.CreateStatic(this,1,2))  {   TRACE(_T("failed to create the splitter"));   return FALSE;  }

 if (!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CTView),CSize(100,100),pContext))  {   TRACE(_T("Failed to create view in first pane"));   return FALSE;  }

 if (!m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CMyView),CSize(100,100),pContext))  {   TRACE(_T("failed to create view in second pane"));   return FALSE;  }

   return TRUE; }   

  11,加入两个菜单项ID_VIEW_VIEW和ID_VIEW_FVIEW分别对应两个视图,在MainFrm.h中加入成员变量    BOOL ShowView1; 在CMainFrame::OnCreate中return 0;前边加上一行ShowView1=true;       编辑两个菜单项代码如下       void CMainFrame::OnViewView() {  // TODO: Add your command handler code here  CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();  pMainFrame->m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(CSplView),CSize(100,100));     ShowView1=true; }

void CMainFrame::OnViewFview() {  // TODO: Add your command handler code here  CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd();  pMainFrame->m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(CFView),CSize(100,100));     ShowView1=false;  }

void CMainFrame::OnUpdateViewView(CCmdUI* pCmdUI) {  // TODO: Add your command update UI handler code here    pCmdUI->SetCheck(ShowView1);  }

void CMainFrame::OnUpdateViewFview(CCmdUI* pCmdUI) {  // TODO: Add your command update UI handler code here  pCmdUI->SetCheck(!ShowView1);  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我将进行实际操作和解释。 首先,我们需要下载 demo 所需的图像和代码。可以从以下链接下载: - 图像:https://www.mathworks.com/help/images/examples/circuitboard.png - 代码:https://www.mathworks.com/help/images/examples/watersheddemo.m 将这些文件保存在您的计算机上,然后打开 MATLAB。 在 MATLAB 命令窗口,切换到代码所在的目录,并键入以下命令以运行 demo: ``` run watersheddemo ``` 这将打开一个名为“Watershed Segmentation”的 GUI 界面,如下图所示: ![GUI](https://www.mathworks.com/help/images/examples/watersheddemo_01.png) 该界面包括以下部分: 1. **原始图像**:显示待分割的原始图像。 2. **标记图像**:显示用户提供的标记图像,其前景和背景区域用不同的颜色标记。 3. **分割图像**:显示分水岭算法得到的分割结果。 4. **控制面板**:包含可供用户调整的参数和选项。 在 GUI 界面,我们可以使用鼠标左键击标记图像来创建标记。左键击会将标记放置在前景区域,右键击则将标记放置在背景区域。在放置标记之后,我们可以使用“Calculate Segmentation”按钮运行分水岭算法,该算法会根据标记将图像分割为前景和背景。 下面我们来分析一下 demo 使用的各种函数: 1. `imread`:读取图像文件。 2. `imshow`:显示图像。 3. `imresize`:调整图像大小。 4. `im2double`:将图像转换为双精度浮点数格式。 5. `rgb2gray`:将 RGB 图像转换为灰度图像。 6. `imbinarize`:将灰度图像二值化。 7. `imopen`:执行形态学开运算。 8. `watershed`:执行分水岭算法。 9. `regionprops`:计算分割结果的区域属性。 10. `imshowpair`:同时显示两幅图像。 11. `imoverlay`:将颜色叠加到图像上。 通过对这些函数的理解,我们可以更好地理解 demo 程序逻辑,并且可以更加灵活地运用这些函数进行图像处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值