1. 程式人生 > >不用splitter控制元件 簡單實現對mfc對話方塊的分割的方法

不用splitter控制元件 簡單實現對mfc對話方塊的分割的方法

不用splitter控制元件  簡單實現對mfc對話方塊的分割的方法

直接貼上原始碼主要部分吧

這個是基於對話方塊的工程 進行對話方塊的分割實現

只是相應了三個訊息函式,看一下就會明白的

我空間資源裡邊有現成的工程程式碼可以下載執行

.cpp 檔案

// spliteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "splite.h"
#include "spliteDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSpliteDlg dialog

CSpliteDlg::CSpliteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSpliteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSpliteDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_SplitCursor = LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CURSOR_SPLIT));
}

void CSpliteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSpliteDlg)
	DDX_Control(pDX, IDC_LIST, m_edit);
	DDX_Control(pDX, IDC_TREE, m_tree);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSpliteDlg, CDialog)
	//{{AFX_MSG_MAP(CSpliteDlg)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSpliteDlg message handlers
#define SIZEBAR          2// Space BTW Tree and Edit
BOOL CSpliteDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CRect rect ;
	GetClientRect( &rect );
	// TODO: Add extra initialization here
	CRect treepos ;
	m_tree.GetClientRect( treepos );
	m_tree.MoveWindow(0, 0, treepos.Width(), rect.Height() );

	m_edit.MoveWindow(treepos.Width() +SIZEBAR , 0, rect.Width()-(treepos.Width() +SIZEBAR), rect.Height() );
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CSpliteDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CRect treepos ;
	m_tree.GetWindowRect( &treepos );
	ScreenToClient( &treepos );
	CRect editpos ;
	m_edit.GetWindowRect( &editpos );
	ScreenToClient( &editpos );

	CRect rc;
	rc.left = treepos.right;
	rc.right = editpos.left;
	rc.top = treepos.top;
	rc.bottom = treepos.bottom;

	CRect rect;
	GetClientRect( &rect );

	if ( rect.PtInRect(point) )
	{
		SetCursor(m_SplitCursor);  //設定滑鼠游標形狀
		if ( nFlags & MK_LBUTTON )
		{
			ResizeWindows(point.x, rect.right-rect.left);
		}
	}
	else
	{
		if(m_bButtonDown) 
		{
			m_bButtonDown = FALSE;
			ReleaseCapture();
		}
	}
	CDialog::OnMouseMove(nFlags, point);
}


void CSpliteDlg::ResizeWindows( int CxBarAt, int len )
{
	CRect treepos ;
	m_tree.GetClientRect( &treepos );
	CRect rect ;
	GetClientRect( &rect );
    if(CxBarAt <= 1) 
		CxBarAt = 1;
	if(CxBarAt >= len - 1)
		CxBarAt = len - 1;
	// 移動tree視窗
    m_tree.MoveWindow( 0,0, CxBarAt-SIZEBAR, rect.Height() );
	// 移動edit視窗 
    m_edit.MoveWindow(CxBarAt, 0, rect.right-CxBarAt, rect.Height());

}

void CSpliteDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_bButtonDown = TRUE;
	CRect treepos ;
	m_tree.GetWindowRect( &treepos );
	ScreenToClient( &treepos );
	CRect editpos ;
	m_edit.GetWindowRect( &editpos );
	ScreenToClient( &editpos );
	
	CRect rect;
	rect.left = treepos.right;
	rect.right = editpos.left;
	rect.top = treepos.top;
	rect.bottom = treepos.bottom;
	
	//	CRect rect;
	//	GetClientRect( &rect );
	
	if ( rect.PtInRect(point) )
	{
		SetCursor(m_SplitCursor);
	}

    SetCapture();
	CDialog::OnLButtonDown(nFlags, point);
}

void CSpliteDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_bButtonDown=FALSE;
	ReleaseCapture();
	CDialog::OnLButtonUp(nFlags, point);
}

void CSpliteDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if((IsWindow(m_tree.m_hWnd)) && (IsWindow(m_edit.m_hWnd)))
	{// get sizes; width of tree never changed 
		CRect rcTree;
		m_tree.GetClientRect(&rcTree);
		CRect rcEdit;
		m_edit.GetClientRect(&rcEdit);
		// 移動tree控制元件
		rcTree.bottom=cy;
		m_tree.MoveWindow(&rcTree,TRUE);
		// 移動edit控制元件
		rcEdit.left=rcTree.right+SIZEBAR;
		rcEdit.right=cx;
		rcEdit.top=0;
		rcEdit.bottom=rcTree.bottom;
		m_edit.MoveWindow(&rcEdit,TRUE);
	}
}


 .h 標頭檔案

// spliteDlg.h : header file
//

#if !defined(AFX_SPLITEDLG_H__5C7DA2C0_E37E_426A_A7D3_E1DC7DFD2766__INCLUDED_)
#define AFX_SPLITEDLG_H__5C7DA2C0_E37E_426A_A7D3_E1DC7DFD2766__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CSpliteDlg dialog

class CSpliteDlg : public CDialog
{
// Construction
public:
	CSpliteDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CSpliteDlg)
	enum { IDD = IDD_SPLITE_DIALOG };
	CListCtrl	m_edit;
	CTreeCtrl	m_tree;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSpliteDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CSpliteDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
private:
	void ResizeWindows(int CxBarAt, int len );
private:
	HCURSOR     m_SplitCursor;
	BOOL        m_bButtonDown;
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_SPLITEDLG_H__5C7DA2C0_E37E_426A_A7D3_E1DC7DFD2766__INCLUDED_)