在进行对话框设计时,根据实际需要有时要设计成可以伸缩的对话框:当按钮按下时, 对话框伸展成如图1所示的样子;再一次按下该按钮,则对话框收缩成如图2所示的样子。 (g93-1.jpg) 图1 (g93-2.jpg) 图2 实现步骤如下: (1) 首先在资源文件中建立对话框控件,然后将一个Picture控件的ID设置为 IDC_DIVIDER,Type设置为Rectangle,Color设置为Black,并将其设定为一线状,将其放 在对话框的中间部位,属性设置为不可见,如图3所示。 (g93-3.jpg) 图3 (2)然后执行程序代码。程序的实现原理是:首先获取对话框的尺寸大小,即RECT( left,right,top,bottom),然后根据IDC_DIVIDER的相对位置确定缩减后的对话框尺寸。 其实在left、right、top、bottom四个参数中,缩减后的对话框与扩展后的对话框在尺寸 上的唯一不同之处是right值。缩减后的对话框其right参数正好由IDC_DIVIDER来确定。对 于缩减后的对话框,在原来对话框中不可见的控件应该被禁止,以禁止加速键和TAB键对控 件的操作。而当对话框扩展后,原来被禁止的对话框控件又要使能。相应的程序代码可参 考下面的EnableVisibleChildren()函数。明白了这个原理,程序的设计就很简单了。相应 的程序代码段如下: void CExpand::ExpandDialog(int nResourceID,BOOL bExpand) { //Expand the dialog to full size if bExpand is TRUE;otherwise contract it with the new bottom to the bottom of the divider control specified in nResourceID static CRect rcLarge; static CRect rcSmall; //First time through,save the dialog's large and small sizes if(rcLarge.IsRectNull()) { CRect rcLand; CWnd *pWndLand=GetDlgItem(nResourceID); ASSERT(pWndLand); GetWindowRect(rcLarge); pWndLand->GetWindowRect(rcLand); rcSmall=rcLarge; rcSmall.right=rcLand.right; } if(bExpand) { //Expand the dialog;resize the dialog to its original size(rcLarge) SetWindowPos(NULL,0,0,rcLarge.Width(), rcLarge.Height(), SWP_NOMOVE|SWP_NOZORDER); EnableVisibleChildren(); } else { //Contract the dialog to the small size SetWindowPos(NULL,0,0,rcSmall.Width(), rcSmall.Height(), SWP_NOMOVE|SWP_NOZORDER); EnableVisibleChildren(); } } void CExpand::EnableVisibleChildren() { //Disable all children not in the current dialog.This prevents tabbing to hidden controls and disable accelerators //Get the first child control CWnd *pWndCtrl=GetWindow(GW_CHILD); CRect rcTest; CRect rcControl; CRect rcShow; GetWindowRect(rcShow); while(pWndCtrl!=NULL) { pWndCtrl->GetWindowRect(rcControl); if(rcTest.IntersectRect(rcShow,rcControl)) pWndCtrl->EnableWindow(TRUE); else pWndCtrl->EnableWindow(FALSE); //Get the next sibling window in this chain pWndCtrl=pWndCtrl->GetWindow(GW_HWNDNEXT); } }
SetWindowPos()
对话框伸展成如图1所示的样子;再一次按下该按钮,则对话框收缩成如图2所示的样子。
(g93-1.jpg)
图1
(g93-2.jpg)
图2
实现步骤如下:
(1) 首先在资源文件中建立对话框控件,然后将一个Picture控件的ID设置为
IDC_DIVIDER,Type设置为Rectangle,Color设置为Black,并将其设定为一线状,将其放
在对话框的中间部位,属性设置为不可见,如图3所示。
(g93-3.jpg)
图3
(2)然后执行程序代码。程序的实现原理是:首先获取对话框的尺寸大小,即RECT(
left,right,top,bottom),然后根据IDC_DIVIDER的相对位置确定缩减后的对话框尺寸。
其实在left、right、top、bottom四个参数中,缩减后的对话框与扩展后的对话框在尺寸
上的唯一不同之处是right值。缩减后的对话框其right参数正好由IDC_DIVIDER来确定。对
于缩减后的对话框,在原来对话框中不可见的控件应该被禁止,以禁止加速键和TAB键对控
件的操作。而当对话框扩展后,原来被禁止的对话框控件又要使能。相应的程序代码可参
考下面的EnableVisibleChildren()函数。明白了这个原理,程序的设计就很简单了。相应
的程序代码段如下:
void CExpand::ExpandDialog(int nResourceID,BOOL bExpand)
{
//Expand the dialog to full size if bExpand is TRUE;otherwise contract it with the new bottom to the bottom of the divider control specified in nResourceID
static CRect rcLarge;
static CRect rcSmall;
//First time through,save the dialog's large and small sizes
if(rcLarge.IsRectNull())
{
CRect rcLand;
CWnd *pWndLand=GetDlgItem(nResourceID);
ASSERT(pWndLand);
GetWindowRect(rcLarge);
pWndLand->GetWindowRect(rcLand);
rcSmall=rcLarge;
rcSmall.right=rcLand.right;
}
if(bExpand)
{
//Expand the dialog;resize the dialog to its original size(rcLarge)
SetWindowPos(NULL,0,0,rcLarge.Width(),
rcLarge.Height(),
SWP_NOMOVE|SWP_NOZORDER);
EnableVisibleChildren();
}
else
{
//Contract the dialog to the small size
SetWindowPos(NULL,0,0,rcSmall.Width(),
rcSmall.Height(),
SWP_NOMOVE|SWP_NOZORDER);
EnableVisibleChildren();
}
}
void CExpand::EnableVisibleChildren()
{
//Disable all children not in the current dialog.This prevents tabbing to hidden controls and disable accelerators
//Get the first child control
CWnd *pWndCtrl=GetWindow(GW_CHILD);
CRect rcTest;
CRect rcControl;
CRect rcShow;
GetWindowRect(rcShow);
while(pWndCtrl!=NULL)
{
pWndCtrl->GetWindowRect(rcControl);
if(rcTest.IntersectRect(rcShow,rcControl))
pWndCtrl->EnableWindow(TRUE);
else
pWndCtrl->EnableWindow(FALSE);
//Get the next sibling window in this chain
pWndCtrl=pWndCtrl->GetWindow(GW_HWNDNEXT);
}
}
2.MoveWindow:移动窗口,改变窗口大小。
if(GetDlgItemText(IDC_BUTTON2,str),str=="收缩<<")
{
SetDlgItemText(IDC_BUTTON2,"扩展>>");
}
else
{
SetDlgItemText(IDC_BUTTON2,"收缩<<");
} 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);
}孙鑫视频里的代码