MFC扩展对话框(收缩对话框)

MFC扩展对话框(收缩对话框)

MFC扩展对话框(即收缩对话框)

效果图如下,扩展前(上)、扩展后(下)、上下扩展:

MFC收缩对话框
MFC收缩对话框
MFC收缩对话框
MFC收缩对话框

具体步骤如下:

1.在.h文件中添加如下代码:

[cpp]

private:
bool m_shrink; //标记是否收缩
int m_winHeight; //存储对话框的高度
int m_shrinkHeight; //收缩后的对话框高度

[/cpp]

2.在.cpp中添加如下代码:

[cpp]

//构造函数,添加如下:

m_shrink = TRUE; //初始化对话框为收缩的
m_winHeight = 0;
m_shrinkHeight = 0;

//初始化函数OninitDialog()函数中,添加如下:

CRect winRect; //对话框矩形
GetWindowRect(winRect); //获得对话框矩形
m_winHeight = winRect.Height(); //对话框原始高度
CRect btRect; //按钮矩形
GetDlgItem(IDC_BUTTON1)->GetWindowRect(btRect);
m_shrinkHeight = btRect.bottom+20;
winRect.SetRect(winRect.left,winRect.top,winRect.right,m_shrinkHeight);
MoveWindow(winRect,TRUE); //移动对话框 使变换生效*/

//设置按钮,添加映射函数,代码如下:

CRect btRect; //按钮矩形
GetDlgItem(IDC_BUTTON1)->GetWindowRect(btRect);
CRect winRect;
GetWindowRect(&winRect);
if (m_shrink)
{
winRect.SetRect(winRect.left,winRect.top,winRect.right,winRect.top + m_winHeight);
GetDlgItem(IDC_BUTTON1)->SetWindowText(_T(“关闭”));
}
else
{
winRect.SetRect(winRect.left,winRect.top,winRect.right,winRect.top + m_shrinkHeight);
GetDlgItem(IDC_BUTTON1)->SetWindowText(_T(“设置”));
}
MoveWindow(winRect,TRUE);
m_shrink = !m_shrink;

[/cpp]