1.按钮点击文字变化 同时对话框大小变化
void CTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str;
if (GetDlgItemText(IDC_BUTTON1,str),str=="收缩<<")
{
SetDlgItemText(IDC_BUTTON1,"扩展>>");
}
else
{
SetDlgItemText(IDC_BUTTON1,"收缩<<");
}
static CRect rectLarge;
static CRect rectSmall;
if (rectLarge.IsRectNull())
{
CRect rectSeparator;
GetWindowRect(&rectLarge);
GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);
rectSmall.left=rectLarge.left;
rectSmall.top=rectLarge.top;
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);
}
}
获取按钮文字 若为“收缩<<”则变为"扩展>>"
static CRect rectLarge;
static CRect rectSmall;
定义两个静态变量类,静态变量在第一次获得取值后,以后再次单击此按钮时,并不需要再去设置这两个变量的值,因此,将这两个变量定义为静态变量。
*IsRectNull() 判断矩阵是否为空。
GetWindowRect(&rectLarge)获得整个对话框的原始尺寸
GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);获得图像控件的大小尺寸赋值给rectSeparator
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);
}
如果当前按钮文本显示为“收缩”
则调用SetWindowPos设置为rectSmall的宽高, 最后一个参数为风格的定义
SWP_NOMOVE|SWP_NOZORDER
1:维持窗口当前位置,这将忽略x,y参数
2:维持当前的z次序,这将忽略pWndInsertAfter参数
windows系统管理三个独立的z次序——一个用于顶层窗口、一个用于兄弟窗口,还有一个是用于最顶层窗口。最顶层窗口覆盖所有其他非顶层窗口,而不管它是不是活动窗口或是前台窗口。