Transparent window containing opaque text and buttons

前端 未结 3 1416
北恋
北恋 2020-12-11 10:58

I\'m creating a non-intrusive popup window to notify the user when processing a time-consuming operation. At the moment I\'m setting its transparency by calling SetLa

3条回答
  •  一个人的身影
    2020-12-11 11:20

    In order to do "proper" alpha in a layered window you need to supply the window manager with a PARGB bitmap by a call to UpdateLayeredWindow.

    The cleanest way to achieve this that I know of is the following:

    1. Create a GDI+ Bitmap object with the PixelFormat32bppPARGB pixel format.
    2. Create a Graphics object to draw in this Bitmap object.
    3. Do all your drawing into this object using GDI+.
    4. Destroy the Graphics object created in step 2.
    5. Call the GetHBITMAP method on the Bitmap object to get a Windows HBITMAP.
    6. Destroy the Bitmap object.
    7. Create a memory DC using CreateCompatibleDC and select the HBITMAP from step 5 into it.
    8. Call UpdateLayeredWindow using the memory DC as a source.
    9. Select previous bitmap and delete the memory DC.
    10. Destroy the HBITMAP created in step 5.

    This method should allow you to control the alpha channel of everything that is drawn: transparent for the background, opaque for the text and button.

    Also, since you are going to be outputting text, I recommend that you call SystemParametersInfo to get the default antialiasing setting (SPI_GETFONTSMOOTHING), and then the SetTextRenderingHint on the Graphics object to set the antialiasing type to the same type that is configured by the user, for a nicer look.

提交回复
热议问题