1. 程式人生 > >將CFormView嵌入到CDockablePane中

將CFormView嵌入到CDockablePane中

第一步:將兩個空白的CDockablePane嵌入到MainFrm中:

1,利用類嚮導,新增兩個基類為CDockablePane的類CDock1,CDock2;

2,在MainFrm.h中新增Dock1.h和Dock2.h的標頭檔案;並派生兩個子類

   protected:

                  CDock1 m_dock1;
                  CDock2 m_dock2;
                  CDockablePane* m_pTabbedBar;

3,在MainFrm的OnCreate()裡面新增:(最好接著EnableAutoHidePanes(CBRS_ALIGN_ANY);這句後面新增)

if(!m_dock1.Create("Dock1",this,CRect(0,0,300,300),true,IDD_FRMVIEW,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI))
 {
  TRACE0("Failed to create dock1 window\n");
  return FALSE;
 }
 if(!m_dock2.Create("Dock2",this,CRect(0,0,300,300),true,IDD_FRMVIEW1,WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI))
 {
  TRACE0("Failed to create dock2 window\n");
  return FALSE;
 }
 BOOL bHiColorIcons=TRUE;                           //下面這幾句用於新增標籤的圖示,IDR_SHJKTYPE 為圖示ID
 HICON hdock1=(HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons ? IDR_SHJKTYPE : IDR_SHJKTYPE), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0);
 m_dock1.SetIcon(hdock1, FALSE);
 HICON hdock2=(HICON) ::LoadImage(::AfxGetResourceHandle(), MAKEINTRESOURCE(bHiColorIcons ? IDR_SHJKTYPE : IDR_SHJKTYPE), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0);
 m_dock2.SetIcon(hdock2, FALSE);
 m_dock1.EnableDocking(CBRS_ALIGN_ANY);  //設定可停靠,ANY還可替換成NOALIGN,LEFT,RIGHT,TOP,BOTTOM等
 m_dock2.EnableDocking(CBRS_ALIGN_ANY);
 DockPane(&m_dock1);
 DockPane(&m_dock2);
 m_pTabbedBar = NULL;//將兩個dock合成一個視窗,二者通過標籤切換
 m_dock1.AttachToTabWnd(&m_dock2, DM_SHOW, FALSE, &m_pTabbedBar);

此時,除錯,發現生成兩個CDockablePane,但由於內部沒有東西,所以出現花屏,留影的現象,正常。

 第二步,寫CFormView類

1,利用類嚮導,新增兩個基類為CFormView的類CFrmView,CFrmView1;

2,為CFrmView和CFrmView1分別過載Create函式和OnMouseActivate函式,前者是為了把Create函式過載為public,一行程式碼都不用寫,後者是為了防止CDockablePane處於懸浮狀態時程式崩潰(不過載必然崩潰!)

int CFrmView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
 // TODO: 在此新增訊息處理程式程式碼和/或呼叫預設值
 int nResult = 0;

 CFrameWnd* pParentFrame = GetParentFrame();

 if( pParentFrame == pDesktopWnd )
 {
  // When this is docked
  nResult = CFormView::OnMouseActivate(pDesktopWnd, nHitTest, message);
 }
 else
 {
  // When this is not docked

  BOOL isMiniFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CMiniFrameWnd ) );
  BOOL isPaneFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CPaneFrameWnd ) );
  BOOL isMultiPaneFrameWnd = pDesktopWnd->IsKindOf( RUNTIME_CLASS( CMultiPaneFrameWnd ) );

  // pDesktopWnd is the frame window for CDockablePane

  nResult = CWnd::OnMouseActivate( pDesktopWnd, nHitTest, message );
 //return CFormView::OnMouseActivate(pDesktopWnd, nHitTest, message);
 }
 return nResult;
}

第三步,向CDockablePane裝東西

1,在Dock1.h和Dock2.h中新增protected型別的指標:

CFrmView  *m_frmview; (Dock1.h)
CFrmView1 *m_frmview; (Dock2.h)

(記得分別加FrmView.h和FrmView1.h的檔案包含)

2,接下來以Dock1.cpp為例說,Dock2.cpp一樣,但是注意指標別指重了....

在建構函式中新增

m_frmview = (CFrmView*)(RUNTIME_CLASS(CFrmView)->CreateObject());

過載OnCreate函式和OnSize函式,前者是為初始化,後者是為設定frmview在dock中的顯示大小,一般填充滿

int CDock1::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CDockablePane::OnCreate(lpCreateStruct) == -1)
  return -1;

 // TODO:  在此新增您專用的建立程式碼
 RECT rect;
    GetClientRect(&rect);
    m_frmview->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rect, this, 0, NULL);
 return 0;
}
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"><pre name="code" class="cpp">void CDock1::OnSize(UINT nType, int cx, int cy)
{
 CDockablePane::OnSize(nType, cx, cy);

 // TODO: 在此處新增訊息處理程式程式碼
 if (GetSafeHwnd() == NULL)
 {
  return;
 }
 if(m_frmview->GetSafeHwnd()!=NULL)
 {
  CRect rect;
  GetClientRect(rect);
  m_frmview->SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOACTIVATE | SWP_NOZORDER);
 }
}
轉自:http://www.cnblogs.com/qwcbeyond/archive/2013/03/16/2963436.html