代码如下:
void CTestDlg::OnBtnEx()
{
// TODO: Add your control notification handler code here
//改变按钮的caption
CString str;
if(GetDlgItemText(IDC_BTN_EX,str),str=="收缩<<")
{
SetDlgItemText(IDC_BTN_EX,"扩展>>");
}
else
{
SetDlgItemText(IDC_BTN_EX,"收缩<<");
}
//对对话框进行收缩
//静态变量默认初始化为0,所以默认rectLarge(0,0,0,0)、rectSmall(0,0,0,0)
static CRect rectLarge; //原始矩形区域的位置
static CRect rectSmall; //切割之后矩形区域的位置
if(rectLarge.IsRectNull()) //判断原始矩形的四个坐标是否都为零
{
CRect rectSeparator;
GetWindowRect(&rectLarge);
GetDlgItem(IDC_SEPARATOR)->GetWindowRect(rectSeparator);
rectSmall.top=rectLarge.top;
rectSmall.left=rectLarge.left;
rectSmall.right=rectLarge.right;
rectSmall.bottom=rectSeparator.bottom;
}
if("收缩<<"==str)
{
SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),
SWP_NOMOVE|SWP_NOZORDER);
}
else
{
SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),
SWP_NOMOVE|SWP_NOZORDER);
}
}
Remarks:
CRect::IsRectNull判断矩形的top, left, bottom, and righ是否都为零
CRect::IsRectEmpty判断矩形的宽和高是否至少有一个为零或负值,说白了就是判断矩形面积是否为零
CWnd::GetWindowRect获得窗体矩形的位置,即把左上角和右下角的坐标返回给一个CRect object
CWnd::SetWindowPos 调用这个函数去改变与窗体位置一系列有关的属性
详情查看MSDN