Delphi图象截取编程示例(8)

类别:Delphi 点击:0 评论:0 推荐:

(七)抓取图标(ICON)的窗体
创建一个新的Form3,保存为Capture3.pas。设置属性BorderIcons的四个属性为false.
BorderStyle设为bsNone,FormStyle设为fsStayOnTop.
一个私有变量:fDragging:Boolean; 两个公共变量:fRect:TRect,fBmp:TBitmap;

unit Capture3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm3 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState;
                 X,  Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormActivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    fDragging:Boolean;
  public
    fRect:TRect;
    fBmp:TBitmap;
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

//创建一个新的自定义光标CURSOR_2,放在Capture3.res资源
//文件中.是32*32的白色矩形边框,用来指示抓图的范围.
procedure TForm3.FormCreate(Sender: TObject);
var aDC:HDC ;
const crBox = -19;
begin
  Screen.Cursors[crBox]:=LoadCursor(hInstance,'CURSOR_2');
  Cursor := crBox;
  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 TForm3.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if mbLeft = Button then begin
    fDragging := true;
    SetRect(fRect,X,Y,X+32,Y+32);
    Canvas.DrawFocusRect(fRect);
  end;
end;

procedure TForm3.FormMouseMove(Sender: TObject; Shift: TShiftState;
         X, Y: Integer);
begin
  if fDragging then begin
    Canvas.DrawFocusRect(fRect);
    fRect.Left := X ;
    fRect.Top  := Y ;
    fRect.Right:= X+32;
    fRect.Bottom:=Y+32;
    Canvas.DrawFocusRect(fRect); 
  end;
end;

procedure TForm3.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ModalResult:=mrOK;
end;

procedure TForm3.FormActivate(Sender: TObject);
const crHand=-18;
begin
  Screen.Cursors[crHand]:=LoadCursor(hInstance,pChar('CURSOR_1'));
  Cursor:=crHand;
end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
  fBmp.Free;
  Screen.Cursor := crDefault;
end;

procedure TForm3.FormPaint(Sender: TObject);
begin
  Canvas.Draw(0,0,fBmp);
end;

end.

本文地址:http://com.8s8s.com/it/it4709.htm