delphi 截取屏幕

Posted yangxuming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi 截取屏幕相关的知识,希望对你有一定的参考价值。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Imaging.jpeg, Vcl.StdCtrls, Vcl.ExtCtrls;

type // define an ENUM to describe the possible screenshot types.
  TScreenShotType = (sstActiveWindow, sstActiveClientArea, sstPrimaryMonitor, sstDesktop);

  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure GetScreenShot(shotType: TScreenShotType; var img: TJpegImage);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.GetScreenShot(shotType: TScreenShotType; var img: TJpegImage);
var
  w, h: integer;
  DC: HDC;
  hWin: Cardinal;
  r: TRect;
  bmp: TBitmap;
begin
  hWin := 0;
  case shotType of
    sstActiveWindow:
      begin
        // only the active window
        hWin := GetForegroundWindow;
        DC := GetWindowDC(hWin);
        GetWindowRect(hWin, r);
        w := r.Right - r.Left;
        h := r.Bottom - r.Top;
      end; // sstActiveWindow
    sstActiveClientArea:
      begin
        // only the active client area (active window minus title bars)
        hWin := GetForegroundWindow;
        DC := GetDC(hWin);
        GetWindowRect(hWin, r);
        w := r.Right - r.Left;
        h := r.Bottom - r.Top;
      end; // sstActiveClientArea
    sstPrimaryMonitor:
      begin
        // only the primary monitor.  If 1 monitor, same as sstDesktop.
        hWin := GetDesktopWindow;
        DC := GetDC(hWin);
        w := GetDeviceCaps(DC, HORZRES);
        h := GetDeviceCaps(DC, VERTRES);
      end; // sstPrimaryMonitor
    sstDesktop:
      begin
        // ENTIRE desktop (all monitors)
        DC := GetDC(GetDesktopWindow);
        w := Screen.DesktopWidth;
        h := Screen.DesktopHeight;
      end; // sstDesktop
  else
    begin
      Exit;
    end; // case else
  end; // case

  // convert to jpg
  bmp := TBitmap.Create;
  try
    bmp.Width := w;
    bmp.Height := h;
    BitBlt(bmp.Canvas.Handle, 0, 0, bmp.Width, bmp.Height, DC, 0, 0, SRCCOPY);
    img.Assign(bmp);
  finally
    ReleaseDC(hWin, DC);
    FreeAndNil(bmp);
  end; // try-finally
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  image: TJPEGImage;
begin
  image := TJpegImage.Create;
  GetScreenShot(TScreenShotType(ComboBox1.ItemIndex), image);
  image.SaveToFile(tmp.jpg);
  Image1.Picture.LoadFromFile(tmp.jpg);
end;

end.
ComboBox1 下拉值:ActiveWindow ActiveClientArea PrimaryMonitor Desktop

以上是关于delphi 截取屏幕的主要内容,如果未能解决你的问题,请参考以下文章

用Delphi或易语言如何实现在屏幕区域里查找图形?

delphi关于字符串的截取问题

delphi关于字符串的循环重复提取

delphi 如何不出乱码的截取字符串?

delphi中 怎样截取字符串

关于delphi字符长度的问题?