您的位置:首页 > 其它

拆分窗口类CSplitterWnd在对话框中的应用及拆分子窗口间的通信

2015-08-10 09:11 363 查看
分类:
MFC
  当你在GOOGLE或者百度键入"如何在对话框中使用CSplitterWnd“时,搜索出来的帖子大多千篇一律,鲜有真正是基于对话框的CSplitterWnd应用,大多是基于单文档框架的说明。至于具有参考价值的一篇博文,请参见Codeguru

本文将告诉你如何在对话框中使用CSplitterWnd将主窗口拆分成两个子窗口,并且只与视图类有关,不涉及文档类。重载OnSize(), 使得窗口的大小可随主窗口的变化而改变。

步骤如下:

1,在主窗口类(某对话框)头文件中分别声明一个框架类以及一个CSplitterWnd拆分窗口类的指针类型成员变量

[cpp]
view plaincopyprint?

private:  
    CFrameWnd*      m_pFrameSplit;    // 分隔窗口  
    CSplitterWnd    m_wndSpliter;     // 左右分隔  

private:
CFrameWnd*      m_pFrameSplit;    // 分隔窗口
CSplitterWnd    m_wndSpliter;     // 左右分隔


2,在主窗口类实现文件中的OnInitDialog()方法或OnCreate()事件中进行初始化工作,注册框架类及初始CSplitterWnd

[cpp]
view plaincopyprint?

// 取得主窗口区域   
CRect rc;  
GetDlgItem(IDC_CHILDWND)->GetWindowRect(rc);  //IDC_CHILDWND是一个PictureBox的ID,表示要拆分的区域  
ScreenToClient(&rc);  
  
// 注册框架类   
CString sClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,  
    ::LoadCursor(NULL, IDC_ARROW), (HBRUSH)::GetStockObject(WHITE_BRUSH),  
    ::LoadIcon(NULL, IDI_APPLICATION));  
  
// 创建分隔窗口框架   
m_pFrameSplit = new CFrameWnd;  
m_pFrameSplit->Create(sClass, _T(""), WS_CHILD, CRect(0,0,0,0), this);  
m_pFrameSplit->MoveWindow(rc);  
m_pFrameSplit->ShowWindow(SW_SHOW);  
  
// 创建分割窗口   
m_wndSpliter.CreateStatic(m_pFrameSplit, 1, 2);  
m_wndSpliter.CreateView(0, 0, RUNTIME_CLASS(CEditView), CSize((int)(rc.Width() * 0.265), 0), NULL);   // 左窗口所占比例26.5%  
m_wndSpliter.CreateView(0, 1, RUNTIME_CLASS(CEditView), CSize(0,0), NULL);  
m_wndSpliter.MoveWindow(0, 0, rc.Width(), rc.Height());  
m_wndSpliter.ShowWindow(SW_SHOW);  

// 取得主窗口区域
CRect rc;
GetDlgItem(IDC_CHILDWND)->GetWindowRect(rc);  //IDC_CHILDWND是一个PictureBox的ID,表示要拆分的区域
ScreenToClient(&rc);

// 注册框架类
CString sClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW), (HBRUSH)::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));

// 创建分隔窗口框架
m_pFrameSplit = new CFrameWnd;
m_pFrameSplit->Create(sClass, _T(""), WS_CHILD, CRect(0,0,0,0), this);
m_pFrameSplit->MoveWindow(rc);
m_pFrameSplit->ShowWindow(SW_SHOW);

// 创建分割窗口
m_wndSpliter.CreateStatic(m_pFrameSplit, 1, 2);
m_wndSpliter.CreateView(0, 0, RUNTIME_CLASS(CEditView), CSize((int)(rc.Width() * 0.265), 0), NULL);   // 左窗口所占比例26.5%
m_wndSpliter.CreateView(0, 1, RUNTIME_CLASS(CEditView), CSize(0,0), NULL);
m_wndSpliter.MoveWindow(0, 0, rc.Width(), rc.Height());
m_wndSpliter.ShowWindow(SW_SHOW);


注意倒数第三、四行。这里是要拆分的两个窗口对应的视图类,为了方便演示CSplitterWnd类拆分的效果,可以先直接用CEditView代替。

运行结果,主窗口的PictureBox区域被拆分成两个EditBox,中间有可移动的分隔栏用以调节大小。

3.重载OnSize(),使得窗口及控件自适应大小。传控件指针或ID两种方式均可。

[cpp]
view plaincopyprint?

void CMainWndDlg::OnSize(UINT nType, int cx, int cy)  
{  
    CDialog::OnSize(nType, cx, cy);  
  
    static int nLastCx = 0;  
    static int nLastCy = 0;  
    int nWidth  = cx - nLastCx;  
    int nHeight = cy - nLastCy;  
    AdjustDlgItem(m_pFrameSplit, 0, 0, nWidth, nHeight);  
    nLastCx = cx;  
    nLastCy = cy;  
}  
  
// 移动控件 以实现窗口自适应   
void CMainWndDlg::AdjustDlgItem(UINT nId, int nLeft, int nTop,int nRight, int nBottom)  
{  
    AdjustDlgItem(GetDlgItem(nId), nLeft, nTop, nRight, nBottom);  
}  
  
// 移动控件 以实现窗口自适应   
void CMainWndDlg::AdjustDlgItem(CWnd* pItem, int nLeft, int nTop,int nRight, int nBottom)  
{  
    if(NULL == pItem)  
        return;  
    if(!IsWindow(pItem->GetSafeHwnd()))  
        return;  
  
    // 取得控件区域   
    CRect rcWnd;  
    pItem->GetWindowRect(&rcWnd);  
    ScreenToClient(&rcWnd);  
  
    // 重新计算区域   
    rcWnd.top += nTop;  
    rcWnd.bottom += nBottom;  
    rcWnd.left += nLeft;  
    rcWnd.right += nRight;  
  
    // 移动控件   
    pItem->MoveWindow(rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height());  
}  

void CMainWndDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

static int nLastCx = 0;
static int nLastCy = 0;
int nWidth  = cx - nLastCx;
int nHeight = cy - nLastCy;
AdjustDlgItem(m_pFrameSplit, 0, 0, nWidth, nHeight);
nLastCx = cx;
nLastCy = cy;
}

// 移动控件 以实现窗口自适应
void CMainWndDlg::AdjustDlgItem(UINT nId, int nLeft, int nTop,int nRight, int nBottom)
{
AdjustDlgItem(GetDlgItem(nId), nLeft, nTop, nRight, nBottom);
}

// 移动控件 以实现窗口自适应
void CMainWndDlg::AdjustDlgItem(CWnd* pItem, int nLeft, int nTop,int nRight, int nBottom)
{
if(NULL == pItem)
return;
if(!IsWindow(pItem->GetSafeHwnd()))
return;

// 取得控件区域
CRect rcWnd;
pItem->GetWindowRect(&rcWnd);
ScreenToClient(&rcWnd);

// 重新计算区域
rcWnd.top += nTop;
rcWnd.bottom += nBottom;
rcWnd.left += nLeft;
rcWnd.right += nRight;

// 移动控件
pItem->MoveWindow(rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height());
}


为分割窗添加一些自己需要的功能
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  对话框 mfc