How to adjust rectangle in the same position on the screen using Createrectrgn?

Asked

Viewed 115 times

1

I am adding extra functionality to my remote assistance software for better interaction with my users. When I will create a rectangle with a "hole" in the Client.exe in my tests is successfully created on another computer, but my problem is that this rectangle is never created in the same position that I had previously set in server.exe.

inserir a descrição da imagem aqui

Here’s my code I tried last time:

//Server.exe

    private
        { Private declarations }
        FSelecting: Boolean;
        FSelection: TRect;
        function ClientToWindow(const P: TPoint): TPoint;

    public
        Socket: TCustomWinSocket;


    function TForm1.ClientToWindow(const P: TPoint): TPoint;
    begin
      Result := pb1.ClientToScreen(P); // pb1 é um TPaintBox que fica acima de um TImage (onde são recebidos os prints atualizados do cliente) no Form1 
      Dec(Result.X, Left);
      Dec(Result.Y, Top);
    end;

    procedure TForm1.pb1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin

      FSelection.Left := X;
      FSelection.Top := Y;
      FSelecting := True;

    end;

    procedure TForm1.pb1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin


    if FSelecting then

      begin

        FSelection.Right := X;
        FSelection.Bottom := Y;
        PB1.Invalidate;
      end;

    end;

    procedure TForm1.pb1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin

    FSelecting := False;
        FSelection.Right := X;
        FSelection.Bottom := Y;
        PB1.Invalidate;

        FSelection.NormalizeRect;
        if FSelection.IsEmpty then  begin
          Socket.SendText('<|QUADRADO_FECHADO|>');
        end
        else
        begin
          Socket.SendText(

                          IntToStr(ClientToWindow(FSelection.TopLeft).X)
                          + '§' +
                          IntToStr(ClientToWindow(FSelection.TopLeft).Y)
                          + '§' +
                          IntToStr(ClientToWindow(FSelection.BottomRight).X)
                          + '§' +
                          IntToStr(ClientToWindow(FSelection.BottomRight).Y)
          )
        end;

    end;

procedure TForm1.pb1Paint(Sender: TObject);
begin
pb1.Canvas.Brush.Color := clRed;
pb1.Canvas.Rectangle(FSelection);
end;

========================================================================

//Client.exe

procedure TForm1.CS1Read(Sender: TObject; Socket: TCustomWinSocket);
var
StrCommand: string;
List: TStrings;
FormRegion, HoleRegion: HRGN;

begin
  StrCommand := Socket.ReceiveText;

if Pos('<|QUADRADO_FECHADO|>', StrCommand) > 0 then
  begin
    SetWindowRgn(Form2.Handle, 0, True)
  end;

  if Pos('§', StrCommand) > 0 then
  begin

     List := TStringList.Create;
     try

       FormRegion := CreateRectRgn(0, 0, Form2.Width, Form2.Height);
       ExtractStrings(['§'], [], PChar(StrCommand), List);
       HoleRegion := CreateRectRgn(StrToInt(List[0]), StrToInt(List[1]),    StrToInt(List[2]), StrToInt(List[3]));
       CombineRgn(FormRegion, FormRegion, HoleRegion, RGN_DIFF);
       SetWindowRgn(Form2.Handle, FormRegion, True);

     finally
       List.Free;
     end;
  end;

end;

Any suggestion would be welcome.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.