如何有效的使用对话框之二
译者:徐景周(原作:Nishant S )
当我们想让窗口初始时不显示时,通常会用ShowWindow(SW_HIDE) ,但实际上还是在启动是可以看到窗口一闪而过的痕迹。所以,可以使用下面的方法来实现它:
l 先在构造涵数中设置布乐变量 visible值为false.
l 重载 WM_WINDOWPOSCHANGING,并添加下面代码:
l 然后设布乐visible变量值true,并在要显示窗口时,再用ShowWindow(SW_SHOW)既可。
visible = true;ShowWindow(SW_SHOW);对话框的全屏显示可以在OnInitDialog()中用 SetWindowPos 和 HWND_TOPMOST 来实现对话框的重新大小。
在2K/XP下我们可以用 AttachThreadInput 和SetForegroundWindow来有效的获取焦点。
可以直接在 OnInitDialog()中用SetWindowPos来实现。
还是可以用SetWindowPos或MoveWindow来实现它。
void CTest_deleteDlg::OnMakeSmall() { SetWindowPos(NULL,0,0,200,200,SWP_NOZORDER|SWP_NOMOVE); }或:
//伸、缩在IDC_DYCREDITS和IDC_COPYRIGHT两STATIC控件间,做为分隔线
BOOL CAbout::OnInitDialog()
{
CDialog::OnInitDialog();
//"关于"对话框中对话框可收缩效果
CRect Rect1,Rect2; //对话框收缩时大小
GetDlgItem(IDC_DYCREDITS)->GetWindowRect(Rect1);
GetDlgItem(IDC_COPYRIGHT)->GetWindowRect(Rect2);
m_nReducedHeight = Rect1.Height()+(Rect1.top -Rect2.bottom)/2; //收缩后窗体高度
dlgRect.bottom -= (Rect1.Height()+(Rect1.top -Rect2.bottom)/2);
MoveWindow(&dlgRect); //如果要显示对话框起始动态效果的话,不能使用该句
m_bVertical=false; //默认收缩对话框
}
// ---------------------------------------------------------
// 名称: OnMore
// 功能: 是否荣誉显示
// 变量: 无
// 返回: 无
// 编写: 徐景周,2002.4.8
// ---------------------------------------------------------
void CAbout::OnMore()
{
m_bVertical = !m_bVertical;
if(m_bVertical == FALSE) //不显示
{
SetDlgItemText(ID_MORE,_T("更多>>"));
SizeWindow(m_nReducedHeight,true);
// m_DyCredits.EndScrolling(); //停止滚动
}
else //显示
{
SetDlgItemText(ID_MORE,_T("<<隐藏"));
SizeWindow(m_nReducedHeight,false);
m_DyCredits.StartScrolling(); //开始滚动
}
UpdateWindow();
}
// ---------------------------------------------------------
// 名称: SizeWindow
// 功能: 伸展或收缩对话框
// 变量: ReduceHeight-收缩高度,bExtend-是否伸展
// 返回: 无
// 编写: 徐景周,2002.4.8
// ---------------------------------------------------------
void CAbout::SizeWindow(int ReduceHeight, bool bExtend)
{
CRect rc;
GetWindowRect(&rc);
if(bExtend)
{
for (int i= 0; i < ReduceHeight; i++)
{
rc.bottom--;
MoveWindow(&rc);
}
}
else
{
for (int i= 0; i < ReduceHeight; i++)
{
rc.bottom++;
MoveWindow(&rc);
}
}
}
当对话框被拖离屏幕时,可用下面代码使其回到屏幕中。
SendMessage(DM_REPOSITION);注:它必须是顶端窗口且不是chind窗口。
在OnCreate()或OnInitDialog() 改变其显示风格既可。
或用:
ModifyStyle (NULL, WS_MAXIMIZEBOX);
可以在OnSetCursor中实现.
可以在InitInstance()中实现。
先从CwinApp继承类中建立一个不显示的顶级窗口.
在 OnInitDialog中修改显示风格 WS_EX_APPWINDOW.
在 OnInitDialog 修改显示风格,加入上、下文HLP帮助显示.
重载OnHelpInfo(...),用显示相关帮助信息
联系方式:
作者EMAIL:
未来工作室(Future Studio)