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;