Original: How to intercept invisible windows?

Source: Internet
Author: User
Author: Land island Studio

This is an old problem that has never been solved. Yesterday I made a plug-in for the pahi game and thought of it again. So I went online and looked for it. Find the following page, which provides a detailed explanation.
Http://www.delphipages.com/threads/thread.cfm? Id = 131830 & G = 131747

It turns out that it is not easy to implement this function in Windows 2 K. There is a new API function in XP, printwindow. There is a piece of code online. Sort the information as follows. After testing, you will find OK.

The printwindow statement is as follows:
Function printwindow (sourcewindow: hwnd; destination: HDC; nflags: Cardinal): bool; stdcall; External 'user32. dll 'name' printwindow ';

Let's test it with a short piece of code:

Procedure tform1.formcreate (Sender: tobject );
VaR
BMP: tbitmap;
WND: Cardinal;
REC: trect;
Begin
WND: = findwindow ('windowtitle', nil); // get the handle
// Of your window
Getwindowrect (WND, REC );
BMP: = tbitmap. Create;
Try
BMP. Width: = Rec. Right-Rec. Left;
BMP. Height: = Rec. Bottom-Rec. Top;
BMP. pixelformat: = pf24bit;
Printwindow (WND, BMP. Canvas. Handle, 0 );
BMP. savetofile ('d: \ your saved BMP ');
Finally
BMP. Free;
End;
End;

For ease of use, I organized it into a common function:

Procedure capturewindowtobitmap (wndhandle: hwnd; bitmap: tbitmap );

Procedure docapturewindow;
VaR
Imagedc: HDC;
R: trect;
Begin
Setforegroundwindow (wndhandle );
Sleep (200 );
Imagedc: = getwindowdc (wndhandle );
Try
Getwindowrect (wndhandle, R );

If getwindowlong (wndhandle, gwl_style) and ws_maximize <> 0 then
Setrect (R, ABS (R. Left), ABS (R. Top), ABS (R. Right), ABS (R. Bottom) else
Offsetrect (R,-R. Left,-R. Top );

With bitmap do
Begin
Fillrect (canvas. Handle, rect (0, 0, width, height), getstockobject (black_brush ));

Stretchblt (canvas. handle, 0, 0, width, height, imagedc, R. left, R. top, R. right-R. left, R. bottom-R. top, cmsrccopy );
End;
Finally
Releasedc (wndhandle, imagedc );
End;
End;

VaR
R: trect;
Begin
If not assigned (Bitmap) Then exit;

Bitmap. Width: = 0;
Bitmap. Height: = 0;
Getwindowrect (wndhandle, R );

Try
Bitmap. Width: = (R. Right-R. Left );
Bitmap. Height: = (R. Bottom-R. Top );
Bitmap. pixelformat: = pf24bit;

If @ printwindow <> nil then
Printwindow (wndhandle, bitmap. Canvas. Handle, 0)
Else
Docapturewindow;
Finally
{}
End;
End;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.