title: How to draw on the windows desktop using the GDI API?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/9342570/draw-on-screen-with-gdi-or-gdi-similar-to-inspect">Draw on screen with GDI+ (or GDI) similar to Inspect</a>  </p>
  </blockquote>
  
  
  
  <p>I'm attempting to write a snake game that has no windows, but freezes the foreground and draws the snake on top of it. When the game ends the foreground should be unfrozen.</p>
  
  <p>I have written some testing code that is supposed to draw a square on the foreground, but all it seems to do is freeze the desktop for a second and freeze the window in the foreground until I minimize, maximize, close it, or bring another window into the foreground, and it doesn't draw anything. In the code, I try to store a bitmap of the desktop so that I can essentially reset it to it's original state and paint the square in a different position. Can anybody spot the problem with my code?</p>
  
  <pre><code>//Handle to the desktop window
  HWND hDesktopWindow = GetDesktopWindow();
  
  //Lock the window to prevent other applications drawing on it
  if(!LockWindowUpdate(hDesktopWindow)){
      return 1;
  }
  
  //Calling GetDC with argument NULL retrieves the desktop's DC
  HDC hdcDesktop = GetDCEx(hDesktopWindow, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
  
  //Create a compatible DC to allow us to store a bitmap of the desktop
  HDC hdcCompatible;
  if((hdcCompatible = CreateCompatibleDC(hdcDesktop)) == NULL){
      return 1;
  }
  
  //Create a compatible bitmap with the same dimensions as the desktop
  HBITMAP hScrBitmap;
  int cx = GetSystemMetrics(SM_CXSCREEN);
  int cy = GetSystemMetrics(SM_CYSCREEN);
  if((hScrBitmap = CreateCompatibleBitmap(hdcDesktop, cx, cy)) == NULL){
      return 1;
  }
  
  //Select the bitmap into the compatible DC
  SelectObject(hdcCompatible, hScrBitmap);
  
  //Copy the Desktop into the bitmap
  if(!BitBlt(hdcCompatible, 0, 0, cx, cy, hdcDesktop, 0, 0, SRCCOPY)){
      return 1;
  }
  
  //Create a DC compatible with the bitmaps DC for drawing the rectangle
  HDC hdcRectangle;
  if(!CreateCompatibleDC((HDC)hScrBitmap)){
      return 1;
  }
  
  //Create a compatible bitmap for the rectangle to be drawn in
  HBITMAP hRectangleBitmap;
  if(!CreateCompatibleBitmap(hdcRectangle, 100, 100)){
      return 1;
  }
  
  //Fill the rectangle bitmap
  if(!FloodFill(hdcRectangle, 0, 0, RGB(255,0,0))){
      return 1;
  }
  
  //Copy the rectangle onto the desktop bitmap
  if(!BitBlt(hdcCompatible, 100, 100, 100, 100, hdcRectangle, 0, 0, SRCCOPY)){
      return 1;
  }
  
  //Copy the bitmap onto the actual desktop
  if(!BitBlt(hdcDesktop, 0, 0, cx, cy, hdcCompatible, 0, 0, SRCCOPY)){
      return 1;
  }
  
  //Allow time to view the result
  Sleep(1000);
  
  //Allow other applications to draw on the desktop again
  LockWindowUpdate(NULL);
  
  //Cleanup
  ReleaseDC(hDesktopWindow, hdcDesktop);
  DeleteDC(hdcCompatible);
  DeleteObject(hScrBitmap);
  </code></pre>
  
  <p>Any help would be greatly appreciated :)</p>