拆分窗口类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; // 左右分隔
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);
注意倒数第三、四行。这里是要拆分的两个窗口对应的视图类,为了方便演示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());
}
为分割窗添加一些自己需要的功能
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()); }
为分割窗添加一些自己需要的功能
相关文章推荐
- MooBox 基于Mootools的对话框插件
- C#实现在前端网页弹出警告对话框(alert)的方法
- Visual C++中MFC消息的分类
- C#使用Word中的内置对话框实例
- MFC中Radio Button的用法详解
- MFC对话框中添加状态栏的方法
- MFC创建右键弹出菜单的方法
- MFC中动态创建控件以及事件响应实现方法
- C++ 关于MFC多线程编程的注意事项
- MFC程序对文件的处理方法
- MFC自定义消息的实现方法
- MFC之ComboBox控件用法实例教程
- 用JavaScript实现对话框的教程
- Asp.net 弹出对话框基类(输出alet警告框)
- boxy基于jquery的弹出层对话框插件扩展应用 弹出层选择器
- 自制轻量级仿jQuery.boxy对话框插件代码
- 基于jQuery的弹出警告对话框美化插件(警告,确认和提示)
- MFC绘制不规则窗体的方法
- JS 模态对话框和非模态对话框操作技巧汇总
- 设计师如何设计正确的对话框按钮位置