1. 程式人生 > >【VC】Dialog 視窗任意分割子視窗。

【VC】Dialog 視窗任意分割子視窗。

用 Dialog 對話方塊來實現視窗的任意分割。

在資源中新增  Dialog 選擇  IDD_FORMVIEW 資源。。分別新建FormViewOne,FormViewTwo FormViewThree 類,分別繼承基類 CFormView。

class CMyFormViewOne : public CFormView
{
	DECLARE_DYNCREATE(CMyFormViewOne)

protected:
	CMyFormViewOne();           // 動態建立所使用的受保護的建構函式
	virtual ~CMyFormViewOne();

public:
	enum { IDD = IDD_FORMVIEW };
#ifdef _DEBUG
	virtual void AssertValid() const;
#ifndef _WIN32_WCE
	virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援

	DECLARE_MESSAGE_MAP()
};
public:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);


public:
	CFrameWnd *m_pMyWnd;
	CSplitterWnd m_SplitterWnd;
	CSplitterWnd m_SplitterWnd2;
	|			|		|
	|			|	2	|
	|	  1		|||||||||||||||||
	|			|	3	|
	|			|		|

int CSplitDlgDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	CString strMyClass = AfxRegisterWndClass(CS_VREDRAW |CS_HREDRAW,
		::LoadCursor(NULL, IDC_ARROW),
		(HBRUSH) ::GetStockObject(WHITE_BRUSH),
		::LoadIcon(NULL, IDI_APPLICATION));

	// Create the frame window with "this" as the parent
	m_pMyWnd = new CFrameWnd;
	m_pMyWnd->Create(strMyClass,_T(""), WS_CHILD,
		CRect(0,0,200,200), this);
	m_pMyWnd->ShowWindow(SW_SHOW);	


	if (m_SplitterWnd.CreateStatic(m_pMyWnd,1, 2) == NULL) //1行2列
	{
		return -1;
	}	

	if(m_SplitterWnd2.CreateStatic(&m_SplitterWnd,2,1,WS_CHILD|WS_VISIBLE,m_SplitterWnd.IdFromRowCol(0,1)) == NULL)
	{
		return -1;
	}

	m_SplitterWnd.CreateView(0,0, RUNTIME_CLASS(CMyFormViewOne),
		CSize(100,100), NULL);

	m_SplitterWnd2.CreateView(0,0, RUNTIME_CLASS(CMyFormViewTwo),
		CSize(80,80), NULL);

	m_SplitterWnd2.CreateView(1,0, RUNTIME_CLASS(CMyFormViewThree),
		CSize(80,80), NULL);
 return 0;
}


在 OnInitDialog 函式中

	CRect rect;
	GetWindowRect(&rect);
	ScreenToClient(&rect);

	m_pMyWnd->MoveWindow(&rect);
	m_pMyWnd->ShowWindow(SW_SHOW);

去掉子視窗的滾動條的顯示。。如下程式碼即可

void CMyFormViewOne::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	m_nMapMode = -1;
}


關於固定分割欄的,則需要自定義CSpliterWnd類。。

class CMySplitterWnd : public CSplitterWnd
{
	DECLARE_DYNAMIC(CMySplitterWnd)

public:
	CMySplitterWnd();
	virtual ~CMySplitterWnd();

protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
};

void CMySplitterWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此新增訊息處理程式程式碼和/或呼叫預設值
	//CSplitterWnd::OnLButtonDown(nFlags, point);
}

void CMySplitterWnd::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此新增訊息處理程式程式碼和/或呼叫預設值
	//CSplitterWnd::OnMouseMove(nFlags, point);
}

BOOL CMySplitterWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO: 在此新增訊息處理程式程式碼和/或呼叫預設值
	//return CSplitterWnd::OnSetCursor(pWnd, nHitTest, message);
	return TRUE;
}