使用vc6.0。mfc单文档程序。
效果:初始时,有上下两个窗口,但只有下面的窗口显示;点击分割条,上窗口显示;再点击分割条,上窗口隐藏。
1)建立CSplitterWnd的派生类CMySptWnd。
class CMySptWnd : public CSplitterWnd
并定一个成员变量bool m_bIs;,
初始化为false
2)分割成上下两个窗口。
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) { if(m_wndSplitter.CreateStatic(this,2,1)==NULL) // CMySptWnd m_wndSplitter; return FALSE; m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CView11View),CSize(0,0), pContext); m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CView11View),CSize(100,100),pContext); return TRUE; }
3)定义函数:(主要就是它)
void CMySptWnd::SetSpl(CPoint pt) { enum HitTestValue { noHit = 0, vSplitterBox = 1, hSplitterBox = 2, bothSplitterBox = 3, // just for keyboard vSplitterBar1 = 101, vSplitterBar15 = 115, hSplitterBar1 = 201, hSplitterBar15 = 215, splitterIntersection1 = 301, splitterIntersection225 = 525 }; if (m_bTracking) { // move tracker to current cursor position pt.Offset(m_ptTrackOffset); // pt is the upper right of hit detect // limit the point to the valid split range if (pt.y < m_rectLimit.top) pt.y = m_rectLimit.top; else if (pt.y > m_rectLimit.bottom) pt.y = m_rectLimit.bottom; if (pt.x < m_rectLimit.left) pt.x = m_rectLimit.left; else if (pt.x > m_rectLimit.right) pt.x = m_rectLimit.right; if (m_htTrack == vSplitterBox || m_htTrack >= vSplitterBar1 && m_htTrack <= vSplitterBar15) { if (m_rectTracker.top != pt.y) { OnInvertTracker(m_rectTracker); m_rectTracker.OffsetRect(0, pt.y - m_rectTracker.top); OnInvertTracker(m_rectTracker); } } else if (m_htTrack == hSplitterBox || m_htTrack >= hSplitterBar1 && m_htTrack <= hSplitterBar15) { if (m_rectTracker.left != pt.x) { OnInvertTracker(m_rectTracker); m_rectTracker.OffsetRect(pt.x - m_rectTracker.left, 0); OnInvertTracker(m_rectTracker); } } else if (m_htTrack == bothSplitterBox || (m_htTrack >= splitterIntersection1 & m_htTrack <= splitterIntersection225)) { if (m_rectTracker.top != pt.y) { OnInvertTracker(m_rectTracker); m_rectTracker.OffsetRect(0, pt.y - m_rectTracker.top); OnInvertTracker(m_rectTracker); } if (m_rectTracker2.left != pt.x) { OnInvertTracker(m_rectTracker2); m_rectTracker2.OffsetRect(pt.x - m_rectTracker2.left, 0); OnInvertTracker(m_rectTracker2); } } } else { // simply hit-test and set appropriate cursor int ht = HitTest(pt); SetSplitCursor(ht); } }
这段代码是主要是从WINSPLIT.CPP文件的CSplitterWnd::OnMouseMove函数中拷贝来的。WINSPLIT.CPP文件在安装目录的/VC98/MFC/SRC/中。
4)重载OnLButtonUp函数
void CMySptWnd::OnLButtonUp(UINT nFlags, CPoint point) { CRect rc; GetWindowRect(&rc); if(point.x>rc.left && point.x<rc.right)//判断鼠标是否点在分割条上 { if(m_bIs==false) { SetSpl(CPoint(0,(rc.bottom-rc.top)/2)); m_bIs = !m_bIs; } else { SetSpl(CPoint(0,0)); m_bIs = !m_bIs; } } CSplitterWnd::OnLButtonUp(nFlags, point); }
代码的复用性比较差。如有问题请指正。