再给一个直接使用系统GDI实现的抓屏
procedure CaptureScreen(const FileName: string);overload;
var
nScreenWidth,nScreenHeight: integer;
ScreenDc,hCaptureDC: HDC;
bmp: TBitmap;
begin
nScreenWidth := GetSystemMetrics(SM_CXSCREEN);
nScreenHeight := GetSystemMetrics(SM_CYSCREEN);
ScreenDc := GetDC(GetDesktopWindow);
hCaptureDC := CreateCompatibleDC(ScreenDc);
bmp := TBitmap.Create;
bmp.Handle :=CreateCompatibleBitmap(ScreenDc,nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,bmp.Handle);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,ScreenDc,0,0,SRCCOPY);
bmp.SaveToFile(fileName);
ReleaseDC(GetDesktopWindow,ScreenDc);
DeleteDC(hCaptureDC);
bmp.Free;
end;
通过上面这一步,我们可以很容易联想到用来抓取任何一个控件的表面图象,函数如下:
procedure CaptureControl(Control: TWinControl;const FileName: string);overload;
var
ControlWidth,ControlHeight: integer;
ControlDc,hCaptureDC: HDC;
bmp: TBitmap;
begin
ControlWidth := Control.ClientWidth;
ControlHeight := Control.ClientHeight;
ControlDc := GetDC(Control.Handle);
hCaptureDC := CreateCompatibleDC(ControlDc);
bmp := TBitmap.Create;
bmp.Handle :=CreateCompatibleBitmap(ControlDc,ControlWidth,ControlHeight);
SelectObject(hCaptureDC,bmp.Handle);
BitBlt(hCaptureDC,0,0,ControlWidth,ControlHeight,ControlDc,0,0,SRCCOPY);
bmp.SaveToFile(fileName);
ReleaseDC(GetDesktopWindow,ControlDc);
DeleteDC(hCaptureDC);
bmp.Free;
end;