Send text with Client Socket from one form that is displayed above another Form that is always on top

1

I'm having trouble sending text messages from a Client Socket . The form that sends the text is always displayed above the form that always stays on the top, when FormOnTop does not exist (not shown), works fine, since FormOnTop exists and form that sends the text is displayed above the FormOnTop , the submission fails, and the text (message) does not even exit Client.exe from my software.

Is there a solution to this?

To understand better, I'll leave below the code I use:

Form containing Client Socket component:

unit Unit1;

interface

uses
  FormSender;

type
 ......

end;

 var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CS1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  StrCommand: String;
begin

    StrCommand := Socket.ReceiveText;

  if Pos('<|Command_From_Server|>', StrCommand) > 0 then

   begin

     FormSender.PopupMode:= pmExplicit;
     FormSender.PopupParent:= FormOnTop;
     FormSender.Show;

   end;
end;

end.

Form that sends the text (FormSender) and appears above the form that always remains at the top:

unit FormSender;

interface

uses
  Unit1;

type
 ......

end;

 var
  FormSender: TFormSender;

implementation

{$R *.dfm}

procedure TFormSender.Button1Click(Sender: TObject);
begin

 Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);

end;

end.

Form that always stays on top:

NOTE: The FormStyle property is: fsStayOnTop .

unit FormOnTop;

        interface

        uses
          .......

        type
         ......

        end;

         var
          FormOnTop: TFormOnTop;

        implementation

        {$R *.dfm}

    procedure TFormOnTop.FormCreate(Sender: TObject);
    begin
    { Position form }
      Top := 0 ;
      Left := 0 ;

      { Go full screen }
      BorderStyle := bsNone ;
      WindowState  := wsmaximized;
      ClientWidth  := Screen.Width ;
      ClientHeight := Screen.Height;
      Refresh;
      SetForegroundWindow(Handle) ;
      SetActiveWindow(Application.Handle);

    end;

    procedure TFormOnTop.CreateParams(var Params: TCreateParams);
    begin
      inherited;
      if (FormStyle = fsStayOnTop) then begin
        Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
        Params.WndParent := GetDesktopWindow;
      end;
    end;

procedure TFormOnTop.FormShow(Sender: TObject);
begin
 SetWindowPos(FormOnTop.handle, HWND_TOPMOST, FormOnTop.Left, FormOnTop.Top, FormOnTop.Width, FormOnTop.Height, 0);
end;


end.
    
asked by anonymous 07.11.2015 / 14:06

2 answers

0

If I understood this correctly:

StrCommand := Socket.ReceiveText;

You receive text sent by:

Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);

Then note that you wrote <|Hello_To_Server|> in:

Form1.CS1.Socket.SendText('<|Hello_To_Server|>' + Edit1.Text + '<<|);

But here you requested <|Command_From_Server|> for function Pos :

if Pos('<|Command_From_Server|>', StrCommand) > 0 then

As far as I understand the Pos function looks for a string that contains <|Command_From_Server|> and not <|Hello_To_Server|> and therefore what exists inside:

   begin

     FormSender.PopupMode:= pmExplicit;
     FormSender.PopupParent:= FormOnTop;
     FormSender.Show;

   end;

It will never be fired.

Maybe you can use TRegEx.IsMatch(Input, 'SUA REGEX AQUI') instead of Pos .

    
07.11.2015 / 14:35
0

The problem was a snippet of code that has " FormOnTop " and I forgot to post it up on the question, which is this:

uses
 Unit1;

    procedure TFormOnTop.TTimer1Timer(Sender: TObject);
    begin
      {if FormExists(FormOnTop) then begin
        Timer2.Enabled:= True;
        Form1.CS1.Socket.SendText('<|OK|>');
        Timer1.Enabled:= False;
      end;}
    end;

    procedure TFormOnTop.TTimer2Timer(Sender: TObject);
    begin
    //ForceForegroundWindow(FormOnTop.Handle);
    end;

I was conflicting by using the same Socket (" CS1 ") that the sending of the message and the Timer trigger could know the message button sending, / p>     

07.11.2015 / 22:31