用過QQ和MSN聊天工具的人都知道,只要好友上線,就會在托盤的位置處顯示一個提示窗口,可以是拉簾式的,或者是淡入淡出的形式出現;想想何不為自己的程式也加一個漂亮的提示窗口呢:)
說做就做。
一、MSN拉簾式窗口制作
分三部分:1、窗口的顯示;2、窗口的停留;3、窗口的消失;
如果達到這樣郊果,系統中要有三個定時器,進行分別控制。定義的定時器如下:
#define ID_TIMER_POP_WINDOW 1
#define ID_TIMER_DISPALY_DELAY 2
#define ID_TIMER_CLOSE_WINDOW 3
從CWnd 繼承一個窗口,當然也可以從CFrameWnd進行派生,這不是主要問題,關鍵是看你是怎麼處理WM_PAINT,WM_MOUSEMOVE,WM_TIMER的消息。一般情況,我從OnPaint()中進行顯示圖片,在WM_TIMER中處理定時器消息,下面是處裡定時器時用到的代碼:
CMsgWnd::CMsgWnd()
{
...
SetTimer(ID_TIEMR_POP_WINDOW,20,NULL);
...
}
void CMsgWnd::OnTimer(UINT nIDEvent)
{
static int nHeight=0;
int cy=GetSystemMetrics(SM_CYSCREEN);
int cx=GetSystemMetrics(SM_CXSCREEN);
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);
int y=rect.bottom-rect.top;
int x=rect.right-rect.left;
x=x-WIN_WIDTH;
switch(nIDEvent)
{
case ID_TIMER_POP_WINDOW:
if(nHeight<=WIN_HEIGHT)
{
++nHeight;
MoveWindow(x,
y-nHeight,
WIN_WIDTH,
WIN_HEIGHT);
Invalidate(FALSE);
}
else
{
KillTimer(ID_TIMER_POP_WINDOW);
SetTimer(ID_TIMER_DISPLAY_DELAY,5000,NULL);
}
break;
case ID_TIMER_CLOSE_WINDOW:
if(nHeight>=0)
{
nHeight--;
MoveWindow(x,
y-nHeight,
WIN_WIDTH,
nHeight);
}
else
{
KillTimer(ID_TIMER_CLOSE_WINDOW);
SendMessage(WM_CLOSE);
}
break;
case ID_TIMER_DISPLAY_DELAY:
KillTimer(ID_TIMER_DISPLAY_DELAY);
SetTimer(ID_TIMER_CLOSE_WINDOW,20,NULL);
break;
}
CWnd::OnTimer(nIDEvent);
}
根據你設的定時器的長短來控制窗口的顯示過程;
二、QQ淡出淡出顯示實現
其實用到一個API函數:AnimateWindow,下面是這個函數的一些說明:
The AnimateWindow function enables you to produce special effects when showing or hiding windows.
There are two types of animation: roll animation and slide animation. BOOL AnimateWindow(
HWND hwnd, // handle to the window to animate
DWORD dwTime, // duration of animation
DWORD dwFlags // animation type
);
上面的函數前兩個參數一看就明白,不用說了,最後一個參數dwFlags設置動畫窗口的樣式:
Specifies the type of animation. This parameter can be one or more of the following flags.
各種標志說明:
標志
說明
AW_SLIDE
Uses slide animation. By default, roll animation is used.
This flag is ignored when used with the AW_CENTER flag.
AW_ACTIVATE
Activates the window. Do not use this flag with AW_HIDE.
AW_BLEND
Uses a fade effect. This flag can be used only if hwnd is a top-level window.
AW_HIDE
Hides the window. By default, the window is shown.
AW_CENTER
Makes the window appear to collapse inward if the AW_HIDE flag is used or expand outward
if the AW_HIDE flag is not used.
AW_HOR_POSITIVE
Animate the window from left to right. This flag can be used with roll or slide animation.
It is ignored when used with the AW_CENTER flag.
AW_HOR_NEGATIVE
Animate the window from right to left. This flag can be used with roll or slide animation.
It is ignored when used with the AW_CENTER flag.
AW_VER_POSITIVE
Animate the window from top to bottom. This flag can be used with roll or slide animation.
It is ignored when used with the AW_CENTER flag.
AW_VER_NEGATIVE
Animate the window from bottom to top. This flag can be used with roll or slide animation.
It is ignored when used with the AW_CENTER flag.
以上內容是從MSDN中摘錄的,要淡入淡出的效果的話,用到AW_BELND。
三、控制窗口顯示的代碼
CMainFrame::OnCreate(…)
{
//...
AnimateWindow(GetSafeHwnd(),1000,AW_CENTER|AW_BLEND);
//…
}
四、關閉的代碼
CMainFrame::OnClose(..)
{
AnimateWindow(GetSafeHwnd(),1000,AW_HIDE|AW_CENTER|AW_BLEND);
}
其實AnimateWindow只是利用了windows本身的方法,如果您要更加有個性的窗口效果,那就得多做寫些代碼了,一分辛苦,一分收獲吧
就這寫這麼多吧,不明白的,一看源碼,二查MSDN,三請教高手。
五、結束語
其實就MSN的提示窗口來說,如果要從使用復雜一點的背景和效果,那麼還有刷新屏幕的問題,應做到無閃刷新.由於時間蒼促,不足之處必然有之,同時以上代碼也參考了一部分以前 VCKBASE 技術文檔,在此表示感謝!
如果有問題,歡迎交流!共同進步!
本文配套源碼