mainunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:4k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit mainunit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, ExtCtrls, StdCtrls,ShellApi;
  6. type
  7.   TForm1 = class(TForm)
  8.     Image1: TImage;
  9.     Button1: TButton;
  10.     Button2: TButton;
  11.     procedure Button1Click(Sender: TObject);
  12.     procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
  13.       Shift: TShiftState; X, Y: Integer);
  14.     procedure Button2Click(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.   public
  18.     procedure CaptureParScr(StartPoint,EndPoint:TPoint);
  19.     { Public declarations }
  20.   end;
  21. var
  22.   Form1: TForm1;
  23.   ClickCount:Integer;
  24.   StartPoint,EndPoint:TPoint;
  25. implementation
  26. {$R *.dfm}
  27. procedure CopyCurrentDesktop(IncludeCur:Boolean);
  28. var 
  29.  DesktophWnd:hWnd;
  30.  DesktopDC:hWnd;
  31.  CursorhWnd:hWnd;
  32.  CurPos:Tpoint;
  33.  Rect:TRect; 
  34.  Bitmap:TBitmap;
  35. begin
  36.  DesktophWnd := GetDesktopWindow();
  37.  DesktopDC := GetDC(DesktophWnd);
  38.  GetWindowRect(DesktophWnd, Rect);
  39.  if IncludeCur then
  40.   begin
  41.    CursorhWnd:=GetCursor();            //捕获当前鼠标指针句柄
  42.    GetCursorPos(CurPos);
  43.   end;                  //获取当前鼠标指针的位置坐标
  44.  Bitmap := TBitmap.Create;//生成一个Tbitmap类型的实例对象
  45.  Bitmap.Width := Rect.Right-Rect.Left;
  46.  Bitmap.Height := Rect.Bottom-Rect.Top;
  47.  BitBlt(Bitmap.Canvas.Handle, 0, 0,
  48.  Bitmap.Width, Bitmap.Height, DesktopDC, 0, 0, SRCCOPY);
  49. //在抓取到的位图对象上绘制鼠标
  50.  if IncludeCur then
  51.   DrawIcon(Bitmap.Canvas.Handle, CurPos.X, CurPos.Y, CursorhWnd);
  52.  ReleaseDC(DesktophWnd, DesktopDC); 
  53.  Bitmap.SaveToFile('C:Desktop.bmp'); //使用类方法SaveToFile保存文件
  54.  Bitmap.Free;
  55. end;
  56. procedure TForm1.CaptureParScr(StartPoint,EndPoint:TPoint);
  57. var
  58.   Bitmap:TBitmap;
  59. begin
  60.  Bitmap := TBitmap.Create;//生成一个Tbitmap类型的实例对象
  61.  Bitmap.Width := Abs(StartPoint.x-EndPoint.x);
  62.  Bitmap.Height := Abs(StartPoint.y-EndPoint.y);
  63.  bitblt(BitMap.Canvas.Handle,0,0,Bitmap.Width, Bitmap.Height, Image1.Canvas.Handle, StartPoint.x, StartPoint.y, SRCCOPY);
  64.  Bitmap.SaveToFile('C:PartDesktop.bmp'); //使用类方法SaveToFile保存文件
  65.  Bitmap.Free;
  66.  Button2.Click;
  67.  Form1.Close;
  68. end;
  69. procedure TForm1.Button1Click(Sender: TObject);
  70. begin
  71.  Form1.Button2.Enabled:=False;
  72.  if MessageDlg('是否连同鼠标指针一起抓取?',mtConfirmation,
  73.                [mbYes, mbNo], 0) = mrYes  then
  74.   begin
  75.    CopyCurrentDesktop(True);
  76.   end
  77.  else
  78.   begin
  79.    CopyCurrentDesktop(False);
  80.   end;
  81.  Button1.Visible:=False;
  82.  Button2.Visible:=False;
  83.  Form1.Left:=0;
  84.  Form1.Top:=0;
  85.  Form1.Width:=Screen.Width;
  86.  Form1.Height:=Screen.Height;
  87.  Form1.BorderStyle:=bsNone;
  88.  Form1.Image1.Left:=0;
  89.  Form1.Image1.Top:=0;
  90.  Form1.Image1.Width:=Screen.Width;
  91.  Form1.Image1.Height:=Screen.Height;
  92.  ShowMessage('请您在此用鼠标选择要拷贝的屏幕区域起点和终点!');
  93.  Form1.Image1.Picture.LoadFromFile('C:Desktop.bmp');
  94.  ClickCount:=0;
  95.  Form1.Image1.Enabled:=True;
  96. end;
  97. procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  98.   Shift: TShiftState; X, Y: Integer);
  99. begin
  100.  ClickCount:=ClickCount+1;
  101.  if ClickCount=1 then
  102.   begin
  103.     StartPoint.X:=X;
  104.     StartPoint.Y:=Y;
  105.     ShowMessage('您已经选择了区域起点坐标!请继续选择区域终点坐标.');
  106.   end;
  107.  if ClickCount=2 then
  108.   begin
  109.     EndPoint.X:=X;
  110.     EndPoint.Y:=Y;
  111.     if MessageDlg('您已经选择了区域终点坐标,是否抓取由这两点决定的区域?',mtConfirmation,
  112.                [mbYes, mbNo], 0) = mrYes  then
  113.      begin
  114.       ClickCount:=0;
  115.       CaptureParScr(StartPoint,EndPoint);
  116.       Form1.Image1.Enabled:=False;
  117.      end
  118.      else
  119.       ClickCount:=0; 
  120.   end;
  121. end;
  122. procedure TForm1.Button2Click(Sender: TObject);
  123. begin
  124.  ShellExecute(handle,'open','C:PartDesktop.bmp',nil,nil,SW_Normal);
  125. end;
  126. end.