locked
How can I clear a transparent window? RRS feed

  • Question

  • I create a transparent window (WS_EX_TRANSPARENT) that I user to overlay other windows.  The user can draw shapes on the transparent window.  However, when the window is moved or sized, I need to be able to clear what is there and repaint it.  How can I clear what has been drawn so that I can redraw the shapes?   Because there is no background color, the default processing for WM_ERASEBKGND doesn't do anything and I can't do a FillRect because there is no color to use.  I tried using SetROP2 with R2_NOTXORPEN and then redrawing the shape, but it doesn't erase the shape.  Sometimes it just leaves a white void.  Other times it makes the text or edges from the window in the background visible, but the shape is still there.

    I also thought that maybe what I need to do is create a bitmap from the shapes and then just bitblt the shapes bitmap into the window's DC when it needs to paint, but then the window is no longer transparent (I can't click through the window to the underneath windows).

    Anyone have any suggestions?
    Saturday, December 5, 2009 3:00 AM

Answers

  • The way I ended up finding that works for me is to use WS_EX_LAYERED and SetLayeredWindowAttributes to set an unused color as the transparent color for the window and then FillRect (or the default WM_ERASEBKGND behavior) to paint over everything during the 'erase' phase.  And then normal WM_PAINT type handling to paint my shapes again.  For some reason it never occurred to me that I could use both WS_EX_LAYERED and WS_EX_TRANSPARENT at the same time but it seems to work in my case.
    • Marked as answer by joevolleyball Tuesday, December 8, 2009 12:25 AM
    Tuesday, December 8, 2009 12:25 AM