Animate to display a dialog message box in the right-bottom of the window screen

    技术2023-10-04  40

          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:

       

    AnimateWindow

    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 );

    Parameters

    hwnd [in] Handle to the window to animate. The calling thread must own this window. dwTime [in] Specifies how long it takes to play the animation, in milliseconds. Typically, an animation takes 200 milliseconds to play. dwFlags [in] Specifies the type of animation. This parameter can be one or more of the following values. ValueDescriptionAW_SLIDEUses slide animation. By default, roll animation is used. This flag is ignored when used with AW_CENTER. AW_ACTIVATEActivates the window. Do not use this value with AW_HIDE. AW_BLENDUses a fade effect. This flag can be used only if hwnd is a top-level window. AW_HIDEHides the window. By default, the window is shown. AW_CENTERMakes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used. AW_HOR_POSITIVEAnimates the window from left to right. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.AW_HOR_NEGATIVEAnimates the window from right to left. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.AW_VER_POSITIVEAnimates the window from top to bottom. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND. AW_VER_NEGATIVEAnimates the window from bottom to top. This flag can be used with roll or slide animation. It is ignored when used with AW_CENTER or AW_BLEND.

    Return Values

    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.

    Remarks

    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.

    Requirements

      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); 

        

       

     

     

    SetLayeredWindowAttributes

    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 );

    Parameters

    hwnd [in] Handle to the layered window. A layered window is created by specifying WS_EX_LAYERED when creating the window with the CreateWindowEx function or by setting WS_EX_LAYERED via SetWindowLong after the window has been created. crKey [in] Pointer to a COLORREF value that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF , use the RGB macro. bAlpha [in] Alpha value used to describe the opacity of the layered window. Similar to the SourceConstantAlpha member of the BLENDFUNCTION structure. When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque. dwFlags [in] Specifies an action to take. This parameter can be one or more of the following values. ValueMeaningLWA_COLORKEYUse crKey as the transparency color. LWA_ALPHAUse bAlpha to determine the opacity of the layered window..

    Return Values

    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.

    Remarks

    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.

    Requirements

      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();}/*********************************************************************************************************/

    最新回复(0)