简体   繁体   中英

Win32 - How to Animate a Borderless Window

I am having a lot of trouble getting a borderless window to animate using Win32.

My goal is to create a sort of "pop-under" window that will slide into view on the bottom right hand corner of the screen upon some event. At the moment I am just experimenting with getting the aesthetics correct.

I wrote some code that very effectively creates the popup with the formatting I desire. It is a completely borderless window that has a black background.

Using ShowWindow() I can display this no problem. What I would like to do, however, is use AnimateWindow() to slide it into the corner from right to left.

Here's my current code:

    /* create the window */
    RECT coordinates;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &coordinates, 0);
    HWND hWnd = CreateWindowEx(0, CLASS_NAME, L"Test", WS_OVERLAPPEDWINDOW,
                               (coordinates.right - 252), (coordinates.bottom - 102), 250, 100,
                               NULL, NULL, hInstance, NULL);

    /* could not create window, end program */
    if( hWnd == NULL ) {
        return 0;
    }

    /* configure the borderless window style */
    LONG_PTR lStyle = GetWindowLongPtr(hWnd, GWL_STYLE);
    LONG_PTR lExStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
    lStyle &= ~( WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU );
    lExStyle &= ~( WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE );
    SetWindowLongPtr(hWnd, GWL_STYLE, lStyle);
    SetWindowLongPtr(hWnd, GWL_STYLE, lExStyle);

    /* animate the newly created window */
    //ShowWindow(hWnd, SW_SHOW);
    BOOL status = AnimateWindow(hWnd, 100, AW_SLIDE|AW_HOR_NEGATIVE);
    DWORD error = GetLastError();

As you can see, I commented out the ShowWindow call because that method of displaying the window works fine. If I use AnimateWindow() instead of ShowWindow(), what I am seeing is that the status boolean is always 0 (indicating failure) and yet the error code is also 0 (indicating no error). I cannot figure out what is going on.

If I comment out the entire section that configures the borderless window (This is the section between the comments "configure borderless window" and "animate window"), everything works fine. The animation is great, the status boolean is 1 (success) and the error is 0.

Only problem is that the border on the window comes back and it doesn't look anything like I am trying to create.

I am struggling to figure out how to properly format the window style so that it is compatible with the animation.

Can anyone point me in the right direction? Why is the status boolean 0 with an error code of 0? The MSDN article suggests some reasons but none of them seem to apply to my situation. Am I formatting the window in an incompatible way?

When I get the status == 0 and the error == 0 the application is present in the taskbar but there is no visible window associated with it.

Thanks!

So, taking the suggestions on my original post, I managed to solve the issue by applying the appropriate style during the window creation. The following code solved it:

HWND hWnd = CreateWindowEx(     WS_EX_TOPMOST,
                                CLASS_NAME,
                                L"",
                                WS_POPUPWINDOW,
                                (coordinates.right - 252),
                                (coordinates.bottom - 102),
                                250,
                                100,
                                NULL, 
                                NULL, 
                                hInstance, 
                                NULL          );

The key was using WS_POPUPWINDOW. This seemed to apply the proper styles. I couldnt figure out any way to make it work using WS_OVERLAPPEDWINDOW as the base, though it may be possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM