views:

205

answers:

1

I know how to make my own application transparent using Layered Windows but I want to make a different application transparent (for example notepad). I wrote code like this but it doesn't work with other windows except my app main window:

SetWindowLongPtr(WindowFromPoint(p), GWL_EXSTYLE, 
                 GetWindowLongPtr(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(WindowFromPoint(p), 0, (255 * 50) / 100, LWA_ALPHA);

where p is a point on screen ( for example the window I select with my mouse )

I am also interested if there is a way to do this directly from Windows 7 (not necessarily programmatic). I figure there must be a way to do it since every application is rendered in it's own surface and DWM composites them into the final image.

+1  A: 

You are using a different window handle for SetWindowLongPtr than the one you use for GetWindowLongPtr is that a bug in your code or just a typo in your question?

The following code works for me on Windows Server 2003 and on Windows 7

   POINT ptScreen = pt;
   ClientToScreen(pwnd->hdr.hwnd, &ptScreen);
   HWND hctl = WindowFromPoint(ptScreen);
   if (IsWindow(hctl))
      {
      LONG lExStyle = GetWindowLong(hctl, GWL_EXSTYLE);
      lExStyle ^= WS_EX_LAYERED;
      SetWindowLong(hctl, GWL_EXSTYLE, lExStyle);
      SetLayeredWindowAttributes(hctl, 0, 
          (lExStyle & WS_EX_LAYERED) ? (255 * 50) / 100 : 255, 
          LWA_ALPHA);
      }
   }

However, it only works if WindowFromPoint returns the top level window for the application, if it returns a child window, then the code doesn't work. So it works when the mouse is over the caption of the window I want to make transparent, but usually not anywhere else. (tested with notepad)

John Knoeller
oops, yes, it was a bug. Thanks, but it still doesn't work :P
Sil
Thank you very much, my code also works but i hard coded the values in the POINT structure and manually put the desired window to that location.. :) I guess I just didn't hit the caption. Thank you again.
Sil