(七)抓取窗體或控件圖片窗體
創建一個新的Form2,保存為Capture2.pas。設置屬性BorderIcons的四個屬性為false.
BorderStyle設為bsNone,FormStyle設為fsStayOnTop.
兩個公共變量:fRect:TRect,fBmp:TBitmap;
unit Capture2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure ForMactivate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
fRect:TRect;
fBmp:TBitmap;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
//創建一個新的自定義光標CURSOR_1,放在Capture2.res資源
//文件中.是32*32的白色矩形邊框,用來指示抓圖的范圍.
procedure TForm2.FormCreate(Sender: TObject);
var aDC:HDC;
const crHand = -18;
begin
Screen.Cursors[crHand]:=LoadCursor(hInstance,'CURSOR_1');
Cursor:=crHand;
fBmp:= TBitmap.Create ;
fBmp.Width := Screen.Width ;
fBmp.Height:= Screen.Height ;
aDC := GetDC(0);
BitBlt(fBmp.Canvas.Handle,0,0,Screen.Width,Screen.Height,aDC,0,0,srcCopy);
ReleaseDC(0,aDC);
SetBounds(0,0,Screen.Width,Screen.Height);
end;
procedure TForm2.ForMactivate(Sender: TObject);
const crHand=-18;
begin
Screen.Cursors[crHand]:=LoadCursor(hInstance,pChar('CURSOR_1'));
Cursor:=crHand;
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
fBmp.Free;
Screen.Cursor := crDefault;
end;
procedure TForm2.FormPaint(Sender: TObject);
begin
Canvas.Draw(0,0,fBmp);
end;
procedure TForm2.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ModalResult:=mrOK;
end;
end.