1 用一个对话框加一个按钮完成:即点击按钮对话框类似QQ发闪屏晃动。 见练习程序51
主要就是用了
①CWnd::GetWindowRect
void GetWindowRect(LPRECT lpRect) const;
取得窗口位置,尺寸。
eg.
CRect rect;
GetWindowRect(&rect);
②CWnd::SetWindowPos
BOOL SetWindowPos(const CWnd *pWndInsertAfter, int x, int y, int cx, int cy,UINT nFlags);
③定时器
CWnd::SetTimer //设置定时器
UINT SetTimer(UINT nIDEvent, UINT nElapse, void (CALLBACKEXPORT*lpfnTimer)(HWND,UINT,UINT,DWORD));
如果第三个参数为NULL,则用WM_TIMER消息函数来响应定时器事件
CWnd::KillTimer //取消定时器
BOOL KillTimer(int nIDEvent);
?
2 codes
//按此button设置定时器
void CMy51Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
const int elapse = 40; //定时间隔
SetTimer(IDT_TIMER0, elapse, NULL);
}
//闪屏按照上左下右的顺序,重复三次
void CMy51Dlg::OnTimer(UINT nIDEvent)
{
if( IDT_TIMER0 == nIDEvent)
{
const int xChange = 10;
const int yChange = 10; //对话框晃动幅度
static int times = 0; //记录次数
CRect rect;
GetWindowRect(&rect); //取得当前窗口在屏幕上的位置
switch (times)
{
case 0:
case 4:
case 8:
SetWindowPos(NULL, rect.left, rect.top - yChange, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
times++;
break;
case 1:
case 5:
case 9:
SetWindowPos(NULL, rect.left - xChange, rect.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
times++;
break;
case 2:
case 6:
case 10:
SetWindowPos(NULL, rect.left, rect.top + yChange, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
times++;
break;
case 3:
case 7:
case 11:
SetWindowPos(NULL, rect.left + xChange, rect.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
times++;
break;
case 12:
KillTimer(IDT_TIMER0);
times = 0;
break;
}
}
CDialog::OnTimer(nIDEvent);
}
?