程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi圖象截取編程示例(6)

Delphi圖象截取編程示例(6)

編輯:Delphi

  (六)區域抓圖窗體
  創建一個新的Form1,保存為Capture1.pas。設置屬性BorderIcons的四個屬性為false.
  BorderStyle設為bsNone,Cursor設為crCross,FormStyle設為fsStayOnTop.
  添加一個私有變量:fDragging:Boolean;兩個公共變量:fRect:TRect,fBmp:TBitmap;
  Form1的作用:在區域抓圖時創建,抓圖後消失.

  unit Capture1;

  interface

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

  type
    TForm1 = class(TForm)
      procedure FormCreate(Sender: TObject);
      procedure FormDestroy(Sender: TObject);
      procedure FormPaint(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);
    private
      fDragging:Boolean;
    public
      fRect:TRect;
      fBmp:TBitmap;
    end;

  var
    Form1: TForm1;

  implementation

  {$R *.dfm}

  procedure TForm1.FormCreate(Sender: TObject);
  var aDC:HDC;  //設備描述表的句柄
  
begin
    fBmp := TBitmap.Create ;
    fBmp.Width := Screen.Width ;
    fBmp.Height:= Screen.Height;
    aDC := GetDC(0); //獲取一個窗口的設備描述表的句柄,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 TForm1.FormDestroy(Sender: TObject);
  begin
    fBmp.Free ;
  end;

  procedure TForm1.FormPaint(Sender: TObject);
  begin
    Canvas.Draw(0,0,fBmp);  //將圖形繪制到 Canvas
  end;

  //如果按下鼠標左鍵,用SetRect設置矩形fRect,使其只是一個點。
  //用 DrawFocusRect 把這個矩形畫出來
  
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);
  begin
    if Button=mbLeft then begin
      fDragging:=true;
      SetRect(fRect,x,y,x,y);
      Canvas.DrawFocusRect(fRect);
    end;
  end;

  //鼠標移動時, 判斷是否在畫圖中(按下鼠標左鍵),DrawFocusRect重新設置矩形
  //使右下角為當前鼠標位置,在調用DrawFocusRect畫出矩形

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

  //判斷是否在畫圖中(按下鼠標左鍵),鼠標彈起時,
  //DrawFocusRect重新設置矩形  .關閉窗體

  procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);
  begin
    if fDragging then begin
      Canvas.DrawFocusRect(fRect);
      fDragging:=false;
    end;
    ModalResult:=mrOK;   //關閉窗體
  end;

  end.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved