分类: C/C++
2008-08-07 17:40:35
CSplitterWindow 垂直分隔 CHorSplitterWindow 水平分隔
因为目前还不是很清楚 CSplitterWindow 扩展是怎么做,所以本代码是用很笨的方法,利用CSplitterWindow 的 SetSinglePaneMode 接口来实现的。
bool SetSinglePaneMode(int nPane = SPLIT_PANE_NONE)
调用SetSinglePaneMode()函数可以改变分隔窗口的模式使单窗格模式还是双窗格模式,在单窗格模式下,只有一个窗格使可见的并且隐藏了分隔条,这和MFC的动态分隔窗口相似(只是没有那个小钳子形状的手柄,它用于重新分隔分隔窗口)。对于nPane参数可用的值是SPLIT_PANE_LEFT,SPLIT_PANE_RIGHT,SPLIT_PANE_TOP,SPLIT_PANE_BOTTOM,和SPLIT_PANE_NONE,前四个指示显示那个窗格(例如,使用SPLIT_PANE_LEFT参数将显示左边的窗格,隐藏右边的窗格),使用SPLIT_PANE_NONE表示两个窗格都显示。
主要原理如下:
1、把窗口分隔成左右两部分(一个TreeView和一个水平分隔条);
GetClientRect( &rcDefault ); m_hWndClient = m_VertSplitter.Create( m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN); m_VertSplitter.m_bFullDrag = false; // create the left menu treeview m_VertSplitter.GetClientRect( &rcDefault ); m_LeftMenuTree.Create( m_VertSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | TVS_HASBUTTONS | WS_EX_CLIENTEDGE, 0,(HMENU)IDC_LEFT_MENUTREE); InitLeftTreeView(&m_LeftMenuTree); //create the horizontal splitter m_HorzSplitter.Create( m_VertSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ); m_HorzSplitter.m_bFullDrag = false; m_VertSplitter.SetSplitterPanes( m_LeftMenuTree, m_HorzSplitter );
2、把右边水平分隔成上下两部分(两个垂直分隔条);
//create the main listview ,must have LVS_REPORT m_HorzSplitter.GetClientRect( &rcDefault ); m_TopVertSplitter.Create( m_HorzSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ); m_BottomVertSplitter.Create( m_HorzSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ); m_HorzSplitter.SetSplitterPanes( m_TopVertSplitter, m_BottomVertSplitter ); 3.把上面的垂直分隔条分成两个ListView视图; m_TopVertSplitter.GetClientRect( &rcDefault ); m_MainListView.Create(m_TopVertSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | LVS_REPORT, LVS_EX_FULLROWSELECT, (HMENU)IDC_MAIN_LISTVIEW); InitTopListView(&m_MainListView,SPLIT_PANE_LEFT); m_TopListView.Create(m_TopVertSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | LVS_REPORT, LVS_EX_FULLROWSELECT, (HMENU)IDC_TOP_LISTVIEW); InitTopListView(&m_TopListView,SPLIT_PANE_RIGHT); m_TopVertSplitter.SetSplitterPanes( m_MainListView, m_TopListView ); m_TopVertSplitter.SetSinglePaneMode(SPLIT_PANE_LEFT);
3、把下面的垂直分隔条分成两个水平分隔条;
m_BottomVertSplitter.GetClientRect( &rcDefault ); m_LeftHorzSplitter.Create( m_BottomVertSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ); m_HorzSplitter.m_bFullDrag = false; m_RightHorzSplitter.Create( m_BottomVertSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ); m_RightHorzSplitter.m_bFullDrag = false; m_BottomVertSplitter.SetSplitterPanes( m_LeftHorzSplitter, m_RightHorzSplitter );
4、把下面左边的水平分隔条分成两个TreeView视图;
m_LeftHorzSplitter.GetClientRect( &rcDefault ); m_RightMenuTree.Create( m_LeftHorzSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | TVS_HASBUTTONS | WS_EX_CLIENTEDGE, 0,(HMENU)IDC_RIGHT_TOPMENUTREE); InitRightTreeView(&m_RightMenuTree,SPLIT_PANE_TOP); m_BottomMenuTree.Create( m_LeftHorzSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | TVS_HASBUTTONS | WS_EX_CLIENTEDGE, 0,(HMENU)IDC_RIGHT_BOTTOMMENUTREE); InitRightTreeView(&m_BottomMenuTree,SPLIT_PANE_BOTTOM); m_LeftHorzSplitter.SetSplitterPanes( m_RightMenuTree, m_BottomMenuTree ); m_LeftHorzSplitter.SetSinglePaneMode(SPLIT_PANE_TOP);
5、把下面右边的水平分隔条分成两个ListView视图;
m_RightHorzSplitter.GetClientRect( &rcDefault ); m_InfoListView.Create( m_RightHorzSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | LVS_REPORT, LVS_EX_FULLROWSELECT, (HMENU)IDC_INFO_LISTVIEW); InitBottomListView(&m_InfoListView,SPLIT_PANE_TOP); m_BottomListView.Create( m_RightHorzSplitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | LVS_REPORT, LVS_EX_FULLROWSELECT, (HMENU)IDC_BOTTOM_LISTVIEW); InitBottomListView(&m_BottomListView,SPLIT_PANE_BOTTOM); m_RightHorzSplitter.SetSplitterPanes( m_InfoListView, m_BottomListView ); m_RightHorzSplitter.SetSinglePaneMode(SPLIT_PANE_TOP);
6、设置分隔条的位置
//create m_InfoListView m_HorzSplitter.SetSplitterPos( 200 ); m_VertSplitter.SetSplitterPos( 150 ); m_BottomVertSplitter.SetSplitterPos( 100 );
最近工作比较忙,没有对代码做太多优化和整理,只是简单实现了,如果你有不明白的地方,还是自己去思考和修改吧,不要找我询问了,我可能也不会回答。
如果你认为有更好的实现方法,并且把代码修改了,请也发给我一份吧(如果你愿意)^_^!谢谢阅读
WTL做界面确实有很多好处,但是主要不方便的地方是和MFC不能混合使用,所以使用时应认真选择。