2016-07-08 9 views
8

मेरे पास कोड है जो पहले से ही सर्वर पक्ष से पहले परिभाषित एक विशिष्ट क्षेत्र प्राप्त करता है और क्लाइंट पक्ष में फॉर्म पर एक छेद बनाता है। इसके बजाय, मैं इस क्षेत्र का स्क्रीन कैप्चर करना चाहता हूं लेकिन बिना किसी सामान्य डेस्कटॉप कैप्चर की तरह मेरा फॉर्म दिखाई देता है, लेकिन इस मामले में केवल इस छोटे क्षेत्र को ही पकड़ा जाएगा।किसी विशेष क्षेत्र का स्क्रीनशॉट कैसे बनाएं?

तो, मैं इसके लिए अपना कोड कैसे अनुकूलित कर सकता हूं?

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 देख सकते हैं। मेरा उदाहरण ऐसा नहीं करता है, क्योंकि मेरे पास एक ही क्षेत्र है।

उदाहरण के लिए दो टीबूटन और एक टीएममेज की आवश्यकता है। मैंने फॉर्म का आकार बदल दिया है और कोड में तीन घटकों को स्थित किया है, ताकि डीएफएम को शामिल करना जरूरी नहीं है। आपको घटकों को एक फॉर्म पर छोड़ना होगा और ईवेंट हैंडलर को कनेक्ट करना होगा। :-)

बटन 1 पर क्लिक करने से फॉर्म पर एक आयताकार क्षेत्र बन जाएगा, इसे लाल रेखाओं के ग्रिड प्रकार पैटर्न और थोड़ा सा पाठ के साथ भरें, बस यह निर्धारित करने के लिए कि क्षेत्र कहां स्थित है। दूसरे बटन पर क्लिक करने से उस क्षेत्र की सामग्री की एक बिट बिटमैप पर खींची जाएगी और उस बिटमैप को छवि नियंत्रण में असाइन किया जाएगा।

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

आपको बहुत अधिक धन्यवाद। –

संबंधित मुद्दे