If you used to use MSN Messager or like fetion in China,you will find a message box will be popped out in the right-bottom of your computer screen while the message arrived.But how to do we can show our own message like that? Here,I write a method of it and as your reference.Hope this will be help your idea. I will give you the detail steps as following description.
First of all,you must understand two functions: AnimateWindow function and SetLayeredWindowAttributes functions.The MFC describe them like this:
The AnimateWindow function enables you to produce special effects when showing or hiding windows. There are three types of animation: roll, slide, and alpha-blended fade.
BOOL AnimateWindow( HWND hwnd, // handle to window DWORD dwTime, // duration of animation DWORD dwFlags // animation type );If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. The function will fail in the following situations:
The window uses the window region. The window is already visible and you are trying to show the window. The window is already hidden and you are trying to hide the window.To get extended error information, call the GetLastError function.
You can combine AW_HOR_POSITIVE or AW_HOR_NEGATIVE with AW_VER_POSITIVE or AW_VER_NEGATIVE to animate a window diagonally.
The window procedures for the window and its child windows may need to handle any WM_PRINT or WM_PRINTCLIENT messages. Dialog boxes, controls, and common controls already handle WM_PRINTCLIENT. The default window procedure already handles WM_PRINT.
Windows NT/2000: Requires Windows 2000. Windows 95/98: Requires Windows 98. Header: Declared in Winuser.h; include Windows.h. Library: Use User32.lib.
If you use Visual C++6.0,you should load it first dynamicaly.The loading method like this:
DWORD style = AW_BLEND | AW_HIDE;
HINSTANCE hIn = LoadLibrary(_T("User32.dll")); if (!hIn) { MessageBox(_T("load failed!")); } typedef BOOL(WINAPI MYFUNC(HWND,DWORD,DWORD)); MYFUNC *AnimateWindow; AnimateWindow = (MYFUNC*)GetProcAddress(hIn,"AnimateWindow"); AnimateWindow(hwnd,3000,style); FreeLibrary(hIn);
If you use VS2010,you can apply to it in your application procedure directly like this:
AnimateWindow(hwnd,3000,style);
The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.
BOOL SetLayeredWindowAttributes( HWND hwnd, // handle to the layered window COLORREF crKey, // specifies the color key BYTE bAlpha, // value for the blend function DWORD dwFlags // action );If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Note that once SetLayeredWindowAttributes has been called for a layered window, subsequent UpdateLayeredWindow calls will fail until the layering style bit is cleared and set again.
For more information, see Layered Windows.
Windows NT/2000: Requires Windows 2000. Windows 95/98: Unsupported. Header: Declared in Winuser.h; include Windows.h. Library: Use User32.lib.
The using method like AnimateWindow.
SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|0x80000);
typedef BOOL (WINAPI *FSetLayeredWindowAttributes) (HWND,COLORREF,BYTE,DWORD);
FSetLayeredWindowAttributes SetLayeredWindowAttributes ;
HINSTANCE hInst = LoadLibrary("User32.DLL");
SetLayeredWindowAttributes = (FSetLayeredWindowAttributes)GetProcAddress(hInst,"SetLayeredWindowAttributes");
if (SetLayeredWindowAttributes) SetLayeredWindowAttributes(GetSafeHwnd(),RGB(0,0,0),128,2);FreeLibrary(hInst);
You must call SetWindowLong function first when you decide to use SetLayeredWindowAttributes.About SetWindowLong function,you can refer to MFC
Here, I wrote a simple example as you reference.
1,Create your object based on dialog box.
2,Create another dialog box via resource view, and then create a class on it,Set your style by yourself.
3,add code like this:
/***********************************************************************************************************/void COrderMessage::AnimateMessage(){ DWORD style = AW_SLIDE/*|AW_BLEND*//*|AW_HIDE*/|AW_VER_NEGATIVE; HWND hwnd = this->GetSafeHwnd(); for ( int i=0; i<=100; ++i ) { SetTransparent(hwnd,0,255*i/100,LWA_ALPHA); } ::AnimateWindow(hwnd,1000,style);}
void COrderMessage::OnTimer(UINT_PTR nIDEvent){ // TODO: Add your message handler code here and/or call default DWORD style = AW_SLIDE/*|AW_BLEND*/|AW_HIDE|AW_VER_NEGATIVE; HWND hwnd = this->GetSafeHwnd(); for ( int i=0; i<=100; ++i ) { SetTransparent( hwnd,0,255*i/100,LWA_ALPHA ); } ::AnimateWindow( hwnd,1000,style ); CDialog::OnTimer( nIDEvent );}BOOL COrderMessage::SetTransparent( HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags ){ ::SetLayeredWindowAttributes(hWnd,crKey,bAlpha,dwFlags); return TRUE;}
void COrderMessage::showPlace(){ int width = GetSystemMetrics(SM_CXSCREEN); int height = GetSystemMetrics(SM_CYSCREEN); CRect ParentRect, winRect; //m_wndOrderMessagePtr->GetWindowRect( &ParentRect ); this->GetWindowRect( &winRect ); hwnd = this->GetSafeHwnd(); int m_width = width-370; int m_height = height-390; ::MoveWindow(hwnd,m_width,m_height,winRect.right-winRect.left,winRect.bottom-winRect.top,TRUE); AnimateMessage();}/*********************************************************************************************************/