⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expandingdialog.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// ExpandingDialog.cpp : implementation file
            
////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <windowsx.h>
#include "ExpandingDialog.h"

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

////////////////////////////////////////////////////////////////////////
// CExpandingDialog dialog

CExpandingDialog::CExpandingDialog(UINT nIDTemplate,CWnd* pParent,
				 int nIDFrame,int nIDButton,LPCTSTR strExpand,
				 LPCTSTR strContract,int bAllowContract) 
				 : CDialog(nIDTemplate, pParent)
				 
{
	//{{AFX_DATA_INIT(CExpandingDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_strExpand = strExpand;
	m_strContract = strContract;
	m_nIDFrame = nIDFrame;
	m_nIDButton = nIDButton;
	m_bExpanded = TRUE;
	m_bAllowContract = bAllowContract;
	m_pSize = NULL;
}

CExpandingDialog::~CExpandingDialog()
{
	delete m_pSize;
}

void CExpandingDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExpandingDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CExpandingDialog, CDialog)
	//{{AFX_MSG_MAP(CExpandingDialog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////
// CExpandingDialog message handlers
void CExpandingDialog::ExpandBox(BOOL fExpand)
{	
	if (fExpand == m_bExpanded) return;

	CRect rcWnd, rcDefaultBox, rcChild, rcIntersection;
	CWnd * wndChild=NULL;
	CWnd * wndDefaultBox=NULL;
	 
	CWnd * pCtrl = GetDlgItem(m_nIDButton);
	if (pCtrl==NULL) return;

	wndDefaultBox = GetDlgItem(m_nIDFrame);
	if (wndDefaultBox==NULL) return;
	
	wndDefaultBox->GetWindowRect(&rcDefaultBox);

	// 是否使覆盖框之外的对话框部分可见
	wndChild = GetTopWindow();
	for ( ; wndChild != NULL; wndChild = wndChild->GetWindow(GW_HWNDNEXT))
	{
		// 得到屏幕坐标
		wndChild->GetWindowRect(&rcChild);
		if (!rcIntersection.IntersectRect(&rcChild,&rcDefaultBox))
			wndChild->EnableWindow(fExpand);		
	}

	if (!fExpand)//收缩
	{
		_ASSERT(m_bExpanded);
		GetWindowRect(&rcWnd);
		if (m_pSize == NULL)
		{
			m_pSize = new CSize;
			m_pSize->cx = rcWnd.right - rcWnd.left;
			m_pSize->cy = rcWnd.bottom - rcWnd.top;			
			wndDefaultBox->ShowWindow(SW_HIDE);
		}
		SetWindowPos(NULL,0,0,
			rcDefaultBox.right - rcWnd.left, 
			rcDefaultBox.bottom - rcWnd.top,
			SWP_NOZORDER|SWP_NOMOVE);
		pCtrl->SetWindowText(m_strExpand);

		//标识此时对话框处于收缩状态
		m_bExpanded = FALSE;
	}
	else //展开
	{
		_ASSERT(!m_bExpanded);
		_ASSERT(m_pSize != NULL);
		SetWindowPos(NULL,0,0,m_pSize->cx,m_pSize->cy,SWP_NOZORDER|SWP_NOMOVE);
		//确保整个对话框都可见
		SendMessage(DM_REPOSITION,0,0);

		if (m_bAllowContract)
			pCtrl->SetWindowText(m_strContract);
		else
			pCtrl->EnableWindow(FALSE);				
		m_bExpanded = TRUE;
	}
}
 
void CExpandingDialog::OnClickAdvanced() 
{
	Expand(!m_bExpanded);
}

//关键函数:处理展开和收缩
BOOL CExpandingDialog::Expand(BOOL bExpand)
{
	BOOL bShouldExpand;
	if (bExpand == m_bExpanded) return TRUE;

	//模拟展开过程
	bShouldExpand = OnDialogExpanding(m_bExpanded);
	
	//正式展开
	if (bShouldExpand)
	{
		ExpandBox(bExpand);

		//把焦点设置到欲展开的控件
		CWnd * pCtrl;
		pCtrl = GetDlgItem(m_nIDButton);
		if (pCtrl != NULL)
		{
			GetNextDlgTabItem(pCtrl,0)->SetFocus();
		}
		//Expand!
		OnDialogExpanded(m_bExpanded);
	}
	return(m_bExpanded == bExpand);
}


BOOL CExpandingDialog::OnDialogExpanding(BOOL /*bExpanded*/) 
{
	return (TRUE);
}

void CExpandingDialog::OnDialogExpanded(BOOL /*bExpanded*/) 
{
	return;
}

BOOL CExpandingDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	//初始化使对话框处于收缩状态
	ExpandBox(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CExpandingDialog::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	HWND hwndCtl;
	int id;
	UINT codeNotify;
	//得到WM_COMMAND消息各组成部分
	id = GET_WM_COMMAND_ID(wParam,lParam);
	hwndCtl = GET_WM_COMMAND_HWND(wParam,lParam);
	codeNotify = GET_WM_COMMAND_CMD(wParam,lParam);
	//处理消息
	if ((id == m_nIDButton)&&(codeNotify==BN_CLICKED))
		OnClickAdvanced();

	return CDialog::OnCommand(wParam, lParam);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -