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

Delphi/CppBuilder

开发平台:

Delphi

  1. unit mainunit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls,ShellApi;
  6. type
  7.   TForm1 = class(TForm)
  8.     GroupBox1: TGroupBox;
  9.     RadioButton1: TRadioButton;
  10.     RadioButton2: TRadioButton;
  11.     Button1: TButton;
  12.     Button2: TButton;
  13.     procedure Button1Click(Sender: TObject);
  14.     procedure Button2Click(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.   public
  18.     { Public declarations }
  19.   end;
  20. var
  21.   Form1: TForm1;
  22. implementation
  23. {$R *.dfm}
  24. procedure CopyCurrentDesktop(IncludeCur:Boolean);
  25. var 
  26.  DesktophWnd:hWnd;
  27.  DesktopDC:hWnd;
  28.  CursorhWnd:hWnd;
  29.  CurPos:Tpoint;
  30.  Rect:TRect; 
  31.  Bitmap:TBitmap;
  32. begin
  33.  DesktophWnd := GetDesktopWindow();
  34.  DesktopDC := GetDC(DesktophWnd);
  35.  GetWindowRect(DesktophWnd, Rect);
  36.  if IncludeCur then
  37.   begin
  38.    CursorhWnd:=GetCursor();            //捕获当前鼠标指针句柄
  39.    GetCursorPos(CurPos);
  40.   end;                  //获取当前鼠标指针的位置坐标
  41.  Bitmap := TBitmap.Create;//生成一个Tbitmap类型的实例对象
  42.  Bitmap.Width := Rect.Right-Rect.Left; 
  43.  Bitmap.Height := Rect.Bottom-Rect.Top; 
  44.  BitBlt(Bitmap.Canvas.Handle, 0, 0,
  45.  Bitmap.Width, Bitmap.Height, DesktopDC, 0, 0, SRCCOPY);
  46. //在抓取到的位图对象上绘制鼠标
  47.  if IncludeCur then
  48.   DrawIcon(Bitmap.Canvas.Handle, CurPos.X, CurPos.Y, CursorhWnd);
  49.  ReleaseDC(DesktophWnd, DesktopDC); 
  50.  Bitmap.SaveToFile('C:Desktop.bmp'); //使用类方法SaveToFile保存文件
  51.  Bitmap.Free;
  52.  ShowMessage('成功抓取屏幕并保存图像至C:Desktop.bmp文件!');
  53. end;
  54. procedure TForm1.Button1Click(Sender: TObject);
  55. begin
  56.  Button2.Enabled:=False;
  57.  if RadioButton1.Checked then
  58.   CopyCurrentDesktop(True)
  59.  else
  60.   CopyCurrentDesktop(False);
  61.  Button2.Enabled:=True; 
  62. end;
  63. procedure TForm1.Button2Click(Sender: TObject);
  64. begin
  65.  ShellExecute(handle,'open','C:Desktop.bmp',nil,nil,SW_Normal);
  66. end;
  67. end.