2016-07-08 4 views
8

サーバー側で既に定義済みの特定の領域を受け取り、クライアント側の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; 

答えて

6

私はあなたの無関係な情報のすべてを持っていないが、私はビットマップに地域の内容をキャプチャする方法をお見せすることができます。もちろん、必要に応じて座標を適切に調整する必要があります。合計領域の外接矩形を結合した後の取得方法については、GetRgnBoxを参照してください。私の例は、私が単一の領域を持っているのでそうしません。

この例では、フォームに2つのTButtonとTImageが必要です。私はフォームのサイズを決め、3つのコンポーネントをコードに配置して、DFMを含める必要はありません。ただし、コンポーネントをフォームにドロップしてイベントハンドラを接続する必要があります。 :-)

Button1をクリックすると、フォーム上に長方形の領域が作成され、領域がどこに配置されているかを定義するために、赤い線とグリッドのグリッドタイプのパターンで塗りつぶします。 2番目のボタンをクリックすると、そのリージョンの内容のコピーがビットマップに描画され、そのビットマップがイメージコントロールに割り当てられます。

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. 
+0

ありがとうございます。 –

関連する問題