tags:

views:

96

answers:

2

I'm trying to screen grab a window. I'm checking that I'm grabbing the foreground window using GetForegroundWindow() so in theory, nothing should be drawn on top of the window.

I then call GetDC(GetDesktop()) and copy the area of the screen that my window occupies to a bitmap.

If I instead, call GetDC(myWindowHandle) I don't get the title bar or borders (or am I missing something here?)

My problem is that I now correctly grab any menus that my application opens but I also grab pop-ups (IM client & Tweetdeck) that are drawn over my application.

I've found the function AnyPopup() which returns true when there is a popup but says this is for backwards compatibility with 16 bit versions of windows and is generally not useful. Is there a simple way of detecting the popups (and their location) or do I have to cycle through all the windows and enumerate all their child windows in turn in order to test if they are on top of my foreground window?

A: 

GetForegroundWindow() returns the window which currently receives user input, it doesn't necessarily need to be on top of all others. A window can be set to be "topmost" with the SetWindowPos() function which will cause it to be ontop of other windows even if it doesn't have the focus (i think this is what you mean by popup window).

You could probably force your window to be ontop of these by making it a topmost window too by calling SetWindowPos() with HWND_TOPMOST as the second parameter. Since the window has the input focus it should then end up on top of all topmost windows.

If the window is not your own window, you have to make sure to put it back to its original state after you did your screen grabing. To find out if the window was topmost to begin with, call GetWindowLongPtr(myWindowHandle, GWL_EXSTYLE) and see if the WS_EX_TOPMOST style was set.

freak
+1  A: 

When you call GetDC, you get the DC for the client area. To get the DC for the whole window (including the non-client area title bar and border) use GetDCEx with the DCX_WINDOW flag.

Also, check out the PrintWindow function, it lets you take a snapshot of a window even if its obscured or partially off screen - its not perfect but it works pretty well.

Mo Flanagan