procedure CopyCurrentDesktop(IncludeCur:Boolean); var DesktophWnd:hWnd; DesktopDC:hWnd; CursorhWnd:hWnd; CurPos:Tpoint; Rect:TRect; Bitmap:TBitmap; begin DesktophWnd := GetDesktopWindow(); DesktopDC := GetDC(DesktophWnd); GetWindowRect(DesktophWnd, Rect); if IncludeCur then begin CursorhWnd:=GetCursor(); //捕获当前鼠标指针句柄 GetCursorPos(CurPos); end; //获取当前鼠标指针的位置坐标 Bitmap := TBitmap.Create;//生成一个Tbitmap类型的实例对象 Bitmap.Width := Rect.Right-Rect.Left; Bitmap.Height := Rect.Bottom-Rect.Top; BitBlt(Bitmap.Canvas.Handle, 0, 0, Bitmap.Width, Bitmap.Height, DesktopDC, 0, 0, SRCCOPY);
//在抓取到的位图对象上绘制鼠标 if IncludeCur then DrawIcon(Bitmap.Canvas.Handle, CurPos.X, CurPos.Y, CursorhWnd); ReleaseDC(DesktophWnd, DesktopDC); Bitmap.SaveToFile('C:\Desktop.bmp'); //使用类方法SaveToFile保存文件 Bitmap.Free; ShowMessage('成功抓取屏幕并保存图像至C:\Desktop.bmp文件!'); end;
procedure TForm1.Button1Click(Sender: TObject); begin Button2.Enabled:=False; if RadioButton1.Checked then CopyCurrentDesktop(True) else CopyCurrentDesktop(False); Button2.Enabled:=True; end;
procedure TForm1.Button2Click(Sender: TObject); begin ShellExecute(handle,'open','C:\Desktop.bmp',nil,nil,SW_Normal); end;