我用了下面的句子:BOOL CEditDlg::OnInitDialog()
{
CDialog::OnInitDialog(); this->ShowWindow(SW_HIDE);
GetWindowRect(dlgRect);
centerPoint.x=dlgRect.Width()/2;
centerPoint.y=dlgRect.Height()/2;
for (int i=10; i+centerPoint.x <= dlgRect.right; i+=1)
{
testrgn.CreateRectRgn(centerPoint.x-6*i,centerPoint.y-4*i,centerPoint.x+6*i,centerPoint.y+4*i);
SetWindowRgn((HRGN) testrgn,TRUE);
ShowWindow(SW_SHOW);
CenterWindow();
testrgn.DeleteObject();
UpdateWindow();
}
……………………
……………………我的系统是xp,结果对话框出来之后客户区还是原来的xp样式,可是标题栏已经变成windows标准样式了:对话框的边框也没有了,标题栏也不是立体的了,右上交的按钮也变成平的了,,非常难看。不知道why会这样啊??
{
CDialog::OnInitDialog(); this->ShowWindow(SW_HIDE);
GetWindowRect(dlgRect);
centerPoint.x=dlgRect.Width()/2;
centerPoint.y=dlgRect.Height()/2;
for (int i=10; i+centerPoint.x <= dlgRect.right; i+=1)
{
testrgn.CreateRectRgn(centerPoint.x-6*i,centerPoint.y-4*i,centerPoint.x+6*i,centerPoint.y+4*i);
SetWindowRgn((HRGN) testrgn,TRUE);
ShowWindow(SW_SHOW);
CenterWindow();
testrgn.DeleteObject();
UpdateWindow();
}
……………………
……………………我的系统是xp,结果对话框出来之后客户区还是原来的xp样式,可是标题栏已经变成windows标准样式了:对话框的边框也没有了,标题栏也不是立体的了,右上交的按钮也变成平的了,,非常难看。不知道why会这样啊??
通常,我们可以在对话框的WM_INITDIALOG事件的响应函数中加入对话框的一些初始化设置,比如改变窗口的位置及大小。同样,我们也可以在WM_INITDIALOG事件的响应函数中让对话框由小到大地显示。但是,如何改变对话框的大小呢?在编程过程中,我们通常是利用MoveWindow函数来动态调整对话框的大小,但是MoveWindow函数有其局限性:一是它的速度较慢;二是由于众所周知的MFC内存泄漏的原因,如果重复调用MoveWindow函数来改变对话框的大小,MoveWindow不能够将屏幕清“干净”(这点你可以在实际编程中试一下),这显然不是我们所需要的。能不能找到一种其他的方法来快速地改变窗口大小呢?我们知道,用SetWindowRgn函数能够改变窗口的显示区域,那么,在对话框显示之前,让程序将对话框先隐藏起来,然后让对话框的显示区域不断变大,这样就实现了对话框的动态显示。 在继续介绍之前,我们先来介绍一下SetWindowRgn的具体用法,以下是其调用格式: int SetWindowRgn( HRGN hRgn, BOOL bRedraw ); hRgn:指向一区域的句柄,通常该参数是由CRgn对象创建的。 bRedraw:指明在设定窗口显示区域之后系统是否进行窗口重绘操作。 二、应用实例
接下来我们以让程序的“帮助对话框”动态弹出为例,介绍如何通过编程的方式来实现对话框的动态弹出显示。在CAboutDlg类中加入对WM_INITDIALOG的映射,并编辑其响应函数OnInitDialog,该函数的具体内容设置如下: BOOL CAboutDlg::OnInitDialog() { CDialog::OnInitDialog(); //隐藏对话框 ShowWindow(SW_HIDE); CRect dlgRect; GetClientRect(&&dlgRect); CPoint centerPoint; centerPoint.x=dlgRect.Width()/2; centerPoint.y=dlgRect.Height()/2; //得到对话框的中点坐标 CRgn testrgn; this-〉ShowWindow(SW_HIDE); int m=GetSystemMetrics(SM_CYSIZEFRAME); //以下代码实现对话框的动态弹出 for (int i=10;i〈dlgRect.Width()/2+2m;i+=1) { testrgn.CreateRectRgn(centerPoint.x-i,centerPoint.y-i,centerPoint.x+i,centerPoint.y+i); SetWindowRgn((HRGN) testrgn,TRUE); ShowWindow(SW_SHOW); CenterWindow(); testrgn.DeleteObject(); } return TRUE; } 该程序的代码比较简单,编译之后进入程序的帮助选单,你会看见一个由小变大的对话框跃然于屏幕之上。 第二个:::在Windows应用程序中,对话框是应用最广泛也是比较难控制其风格(外表)的一类窗口。相信用过Windows 的朋友在享受其强大功能的同时,一定也为它所提供的具有立体感的界面而感叹吧。通常情况下,对话框的弹出和消隐都是瞬时的,下面将介绍如何实现对话框的动画弹出和消隐,增强程序的美观性。 请按以下步骤实现: 第一步:生成我们的工程(基于对话框)FlashDlg,所有的选项都取默认值,在对话框上随意添加几个控件。 第二步:在对话框的类头文件中定义如下变量,如下:CPoint point;
int nWidth,nHeight;
int dx,dy;
int dx1,dy1; 第三步:在OnInitDialog()中添加如下代码:BOOL CFlashDlgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CRect dlgRect;
GetWindowRect(dlgRect);
CRect desktopRect;
GetDesktopWindow()->GetWindowRect(desktopRect);
MoveWindow(
(desktopRect.Width() - dlgRect.Width()) / 2,
(desktopRect.Height() - dlgRect.Height()) / 2,
0,
0 );
nWidth=dlgRect.Width();
nHeight=dlgRect.Height();
dx=2;
dy=4;
dx1=2;
dy1=2;
SetTimer(1,10 , NULL);
return TRUE;
} 第四步:添加OnTimer函数,添加如下代码:void CFlashDlgDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CRect dlgRect;
GetWindowRect(dlgRect);CRect desktopRect;
GetDesktopWindow()->GetWindowRect(desktopRect);
if(nIDEvent == 1)
{
MoveWindow(
(-dx+desktopRect.Width() - dlgRect.Width()) / 2,
(-dy+desktopRect.Height() - dlgRect.Height()) / 2,
+dx+dlgRect.Width(),
+dy+dlgRect.Height() );if(dlgRect.Width() >=nWidth)
dx=0; // do not over grow
if(dlgRect.Height() >=nHeight)
dy=0; // do not over grow
if((dlgRect.Width() >=nWidth) && (dlgRect.Height() >=nHeight))
KillTimer(1); //Stop the timer
}
if((dlgRect.Width() >=nWidth) && (dlgRect.Height() >=nHeight))
KillTimer(1); //Stop the timerif(nIDEvent == 2)
{
MoveWindow((+dx+desktopRect.Width() - dlgRect.Width()) / 2,
(+dy+desktopRect.Height() - dlgRect.Height()) / 2,
-dx1+dlgRect.Width(),
-dy1+dlgRect.Height() );if(dlgRect.Width() <= 0)
dx1=0; // do not over grow
if(dlgRect.Height() <= 0 )
dy1=0; // do not over grow
if((dlgRect.Width() <= 0 ) && (dlgRect.Height() <=0))
{
KillTimer(2); //Stop the timer
CDialog::OnOK();
}}CDialog::OnTimer(nIDEvent);
} 好了,对话框的动画出现和消隐实现了,运行程序我们会发现对话框平滑的划出,关闭程序我们会发现对话框平滑的消失。可是这两个都有不足,,第一个会引起对话框窗口标题栏风格破坏,第二个不能从对话框中心向四周扩展显示,,
关注better的对话框动态显示例子。。
LPRECT lprcFrom;//变以前的大小
LPRECT lprcTo//变以后的大小
UINT nMilliSecSpeed//变化速度void DrawWireRects(LPRECT lprcFrom, LPRECT lprcTo, UINT nMilliSecSpeed)
{
const int nNumSteps = 10; GdiFlush();
Sleep(50); HDC hDC = ::GetDC(NULL); HPEN hPen = ::CreatePen(PS_SOLID, 2, RGB(255,0,0)); int nMode = ::SetROP2(hDC, R2_NOT);
HPEN hOldPen = (HPEN) ::SelectObject(hDC, hPen); for (int i = 0; i < nNumSteps; i++)
{
double dFraction = (double) i / (double) nNumSteps; RECT transition;
transition.left = lprcFrom->left + (int)((lprcTo->left - lprcFrom->left) * dFraction);
transition.right = lprcFrom->right + (int)((lprcTo->right - lprcFrom->right) * dFraction);
transition.top = lprcFrom->top + (int)((lprcTo->top - lprcFrom->top) * dFraction);
transition.bottom = lprcFrom->bottom + (int)((lprcTo->bottom - lprcFrom->bottom) * dFraction); POINT pt[5];
pt[0] = CPoint(transition.left, transition.top);
pt[1] = CPoint(transition.right,transition.top);
pt[2] = CPoint(transition.right,transition.bottom);
pt[3] = CPoint(transition.left, transition.bottom);
pt[4] = CPoint(transition.left, transition.top);
::Polyline(hDC,pt,5);
GdiFlush();
Sleep(nMilliSecSpeed);
::Polyline(hDC,pt,5);
GdiFlush();
} ::SetROP2(hDC, nMode);
::SelectObject(hDC, hOldPen); ::ReleaseDC(NULL,hDC);
}
void CTestDlg::OnTimer(UINT nIDEvent)
{ CRect dlgRect;
GetWindowRect(dlgRect);CRect desktopRect;
GetDesktopWindow()->GetWindowRect(desktopRect);
if(nIDEvent == 1)
{
MoveWindow(
(-dx+desktopRect.Width() - dlgRect.Width()) / 2,
(-dy+desktopRect.Height() - dlgRect.Height()) / 2,
+dx+dlgRect.Width(),
+dy+dlgRect.Height() );if(dlgRect.Width() >=nWidth)
dx=0; // do not over grow
if(dlgRect.Height() >=nHeight)
dy=0; // do not over grow
if((dlgRect.Width() >=nWidth) && (dlgRect.Height() >=nHeight))
KillTimer(1); //Stop the timer
}
if((dlgRect.Width() >=nWidth) && (dlgRect.Height() >=nHeight))
KillTimer(1); //Stop the timerif(nIDEvent == 2)
{
MoveWindow((+dx+desktopRect.Width() - dlgRect.Width()) / 2,
(+dy+desktopRect.Height() - dlgRect.Height()) / 2,
-dx1+dlgRect.Width(),
-dy1+dlgRect.Height() );if(dlgRect.Width() <= 0)
dx1=0; // do not over grow
if(dlgRect.Height() <= 0 )
dy1=0; // do not over grow
if((dlgRect.Width() <= 0 ) && (dlgRect.Height() <=0))
{
KillTimer(2); //Stop the timer
CDialog::OnOK();
}
}
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,DWORD,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun = (MYFUNC)GetProcAddress(hInst, "AnimateWindow");
if(fun)
{
CenterWindow();
fun(m_hWnd, 400, 0x00000010);
}
FreeLibrary(hInst);
}