Sign in to follow this  
Dusho

How to capture inactive window

Recommended Posts

Dusho    122
Hi all I'm trying to write some simple code that makes window capture of all my current windows. I have all windows in list - with theirs HWND, but I can't get their screen - when i call GetWindowDC - and make a bitmap from that DC - the result is part of my top window (active) with positioning and size of window I want to capture. Is there some way to get DC of inactive or hidden window ? Thnx for any help.

Share this post


Link to post
Share on other sites
As far as I know there is no way to capture the window contents if is not the foreground window. Many proffessional programs that make capture minimize themselves and then (usually) using a hot hey make the capture.

But a simple idea is:



// hwndIWANTTOCAPTURETHIS -> window you want to capture
// hwd -> help variable
....
// Get active window
hwd = GetActiveWindow();

// bring window to front the capture you want to capture
BringWindowToTop(hwndIWANTTOCAPTURETHIS);

// <<PLACE YOU CAPTURE CODE HERE>>

// Restore the original top window
BringWindowToTop(hwd);



Share this post


Link to post
Share on other sites
Dusho    122
Huh.. thnx for quick reply.
I see BringWindowToTop function for the first time - I never heard about that.
Thank you very much !

Share this post


Link to post
Share on other sites
BringWindowToTop:



BOOL BringWindowToTop(

HWND hWnd // handle to window
);






return zero if fails, nonzero value if succesful

NOTICE: BringWindowToTop works only within current application.

-------------------

To activate another's application window try instead:
SetForegroundWindow:


BOOL SetForegroundWindow(

HWND hWnd // handle of window to bring to foreground
);





return zero if fails, nonzero value if succesful

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
under certain versions of Windows, SetForegroundWindow will not bring the window to the foreground if that window is not owned by the current thread. the following code solves that problem:


void SetForegroundWnd( HWND hWnd )
{
if( IsWindowVisible(hWnd) )
{
if( hWnd != GetForegroundWindow() )
{
if( IsIconic(hWnd) )
ShowWindow(hWnd, SW_RESTORE);

AttachThreadInput(
GetWindowThreadProcessId(GetForegroundWindow(), NULL),
GetWindowThreadProcessId(hWnd, NULL),
TRUE);

SetForegroundWindow(hWnd);
SetFocus(hWnd);

AttachThreadInput(
GetWindowThreadProcessId(GetForegroundWindow(), NULL),
GetWindowThreadProcessId(hWnd, NULL),
TRUE);
}
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this