如何创建特定区域的屏幕截图?

时间:2016-07-08 13:28:20

标签: delphi screenshot rectangles

我的代码接收了之前在服务器端定义的特定区域,并在客户端的Form上创建了一个漏洞。而不是这个,我希望得到同一区域的屏幕截图,但没有在最终结果中显示我的表格,就像普通的桌面捕捉一样,但在这种情况下只会捕获这个小区域。

那么,我如何调整下面的代码呢?

procedure TForm1.CS1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  X1, X2, Y1, Y2: Integer;
  List: TStrings;
  FormRegion, HoleRegion: HRGN;
  StrCommand: String;
begin
  StrCommand := Socket.ReceiveText;

  if Pos('§', StrCommand) > 0 then
  begin
    List := TStringList.Create;
    try
      FormRegion := CreateRectRgn(0, 0, Form12.Width, Form12.Height);
      ExtractStrings(['§'], [], PChar(StrCommand), List);

      X1 := StrToIntDef(List[0], 0) - Form12.Left - 2;
      Y1 := StrToIntDef(List[1], 0) - Form12.Top - 2;
      X2 := StrToIntDef(List[2], 0) - Form12.Left - 2;
      Y2 := StrToIntDef(List[3], 0) - Form12.Top - 2;

      HoleRegion := CreateRectRgn(X1, Y1, X2, Y2);
      CombineRgn(FormRegion, FormRegion, HoleRegion, RGN_DIFF);
      SetWindowRgn(Form12.Handle, FormRegion, True);
    finally
      List.Free;
    end;
  end;
end;

1 个答案:

答案 0 :(得分:6)

我没有提供所有无关信息,但我可以向您展示如何将区域内容捕获到位图中。当然,您需要适当调整坐标以满足您的需求。您可能希望看到GetRgnBox看看如何在合并之后获取整个区域的边界矩形。我的例子没有这样做,因为我有一个区域。

该示例在表单上需要两个TButtons和一个TImage。我对表单进行了调整,并将三个组件放在代码中,因此不需要包含DFM。但是,您需要删除表单上的组件并连接事件处理程序。 : - )

单击Button1将在表单上创建一个矩形区域,用红线和一些文本的网格类型图案填充它,只是为了定义区域的位置。单击第二个按钮将在位图上绘制该区域内容的副本,并将该位图指定给图像控件。

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    // Region coords
    R: TRect;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Create the region (hole)
procedure TForm1.Button1Click(Sender: TObject);
var
  Region: HRGN;
begin
  Canvas.TextOut(R.Left + 60, R.Top + 60, 'Test text');
  Canvas.Brush.Style := bsCross;
  Canvas.Brush.Color := clRed;
  Region := CreateRectRgn(R.Left, R.Top, R.Right, R.Bottom);
  { 
    Note: Normally you'd want to check the result of the above API call
    and only proceed if it's not NULL (0). You'd also want to use a
    try..finally to make sure that the region was deleted properly.
    Omitted here because 
      a) This code was tested to work properly, and
      b) It's a demo app for doing something with the region and
         nothing else. If the call to create the region fails, the
         app is useless, and you'll close it anyway, which means
         the region will be automatically destroyed.
  }
  FillRgn(Canvas.Handle, Region, Canvas.Brush.Handle);
  DeleteObject(Region);
  Button2.Enabled := True;
end;

// Capture the region (hole) and display in the TImage.
procedure TForm1.Button2Click(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.SetSize(R.Right - R.Left, r.Bottom - R.Top);
    Bmp.Canvas.CopyRect(Rect(0, 0, Bmp.Width, Bmp.Height), Canvas, R);
    Image1.Picture.Assign(Bmp);
  finally
    Bmp.Free;
  end;
end;

// Set up the coordinates for the region (hole) in the form
procedure TForm1.FormCreate(Sender: TObject);
begin
  R := Rect(10, 40, 175, 175);
  // Size the image we'll use later to fit the rectangle. We set
  // the position below.
  Image1.Width := R.Right - R.Left;
  Image1.Height := R.Bottom - R.Top;

  Self.Height := 375;
  Self.Width := 350;
  Button1.Left := 238;
  Button1.Top := 16;
  Button2.Left := 238;
  Button2.Top := 48;
  Image1.Left := 160;
  Image1.Top := 190;
  // Disable the second button until the first has been clicked
  Button2.Enabled := False;
end;

end.