none
[Win32] Draw on desktop

    Question

  • How can i force repaint of the desktop in order to draw on it?

    I set a mouse hook and on MOUSEMOVE i want to draw lines on the screen following the cursor position.
    Saturday, June 04, 2011 8:34 PM

Answers

  • Not sure how you can safely draw on the screen as GDI is not inherently thread safe and I don't know how you could coordinate that drawing with the system.  If you are trying to annotate the screen, you might try some of the techniques to copy the full screen (like print screen does) then draw to that, possibly creating your own full screen window to show it.

    As far as InvalidateRect goes, the only thing I can suggest is make sure that the HWND you have is really the one you want.  I know that Explorer creates a full screen window that, I think, sits  on top of the desktop.  You probably have the desktop HWND and not Explorer's (This is why when you have multiple monitors in XP and your wallpaper is set on tile, the origin of the tiles will be different depending on whether explorer is running or not.  Because Explorer uses a different origin for its window than the desktop does).


    Saturday, June 04, 2011 10:44 PM
  •  If you are trying to annotate the screen, you might try some of the techniques to copy the full screen (like print screen does) then draw to that, possibly creating your own full screen window to show it.


    How can i create my own full screen window to show ?

    That depends on what framework you are using.  At the lowest level, CreateWindow with appropriate parameters (i.e. the size of the whole screen) and SetWindowPos (to place your window topmost).

    Another approach to screen annotation (which I should have thought of originally) would be to create one or more layered windows in the area(s) you are trying to annotate.  A layered window can be transparent except where you choose to draw on it.  That would probably be the better choice than trying to copy the screen or draw directly to the desktop. 

    Again, CreateWindow can be used here, with the WS_EX_LAYERED extended style.  The simplest approach would be to then use SetLayeredWindowAttributes() to enable color key transparency, and then in your WM_PAINT fill the window with the transparent color.  After that just draw whatever you want.

    Sunday, June 05, 2011 1:09 PM

All replies

  • I have never tried drawing on another program's window before.  I would imagine that is 100% disallowed.  Still, have you tried the usual way?  It involves InvalidateRect(), UpdateWindow(), but since that is probably not going to work, maybe use GetDC() to obtain the desktop's device context, etc.
    MCP
    Saturday, June 04, 2011 9:17 PM
  • yes, i can draw on the screen...the problem is how force the refresh of the dirty screen. InvalidareRect,UpdateWin etc. don't work.
    Saturday, June 04, 2011 9:53 PM
  • Not sure how you can safely draw on the screen as GDI is not inherently thread safe and I don't know how you could coordinate that drawing with the system.  If you are trying to annotate the screen, you might try some of the techniques to copy the full screen (like print screen does) then draw to that, possibly creating your own full screen window to show it.

    As far as InvalidateRect goes, the only thing I can suggest is make sure that the HWND you have is really the one you want.  I know that Explorer creates a full screen window that, I think, sits  on top of the desktop.  You probably have the desktop HWND and not Explorer's (This is why when you have multiple monitors in XP and your wallpaper is set on tile, the origin of the tiles will be different depending on whether explorer is running or not.  Because Explorer uses a different origin for its window than the desktop does).


    Saturday, June 04, 2011 10:44 PM
  •  If you are trying to annotate the screen, you might try some of the techniques to copy the full screen (like print screen does) then draw to that, possibly creating your own full screen window to show it.


    How can i create my own full screen window to show ?

    Sunday, June 05, 2011 8:13 AM
  •  If you are trying to annotate the screen, you might try some of the techniques to copy the full screen (like print screen does) then draw to that, possibly creating your own full screen window to show it.


    How can i create my own full screen window to show ?

    That depends on what framework you are using.  At the lowest level, CreateWindow with appropriate parameters (i.e. the size of the whole screen) and SetWindowPos (to place your window topmost).

    Another approach to screen annotation (which I should have thought of originally) would be to create one or more layered windows in the area(s) you are trying to annotate.  A layered window can be transparent except where you choose to draw on it.  That would probably be the better choice than trying to copy the screen or draw directly to the desktop. 

    Again, CreateWindow can be used here, with the WS_EX_LAYERED extended style.  The simplest approach would be to then use SetLayeredWindowAttributes() to enable color key transparency, and then in your WM_PAINT fill the window with the transparent color.  After that just draw whatever you want.

    Sunday, June 05, 2011 1:09 PM