我很难从 Client.Socket 发送文本。发送文本的表单始终显示在始终位于顶部的表单上方,当FormOnTop
不存在(未显示)时,工作正常,已存在FormOnTop
时表单将发送文本显示在FormOntop
上方,发送失败,文本(消息)甚至没有退出我的软件Client.exe。
有一些解决方案吗?
为了更好地理解,我将保留我使用的代码:
包含客户端套接字组件的表单
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.
发送文本的表单(FormSender)并显示在始终位于顶部的表单上方:
unit FormSender;
interface
uses
Unit1;
type
......
end;
var
FormSender: TFormSender;
implementation
{$R *.dfm}
procedure TFormSender.Button1Click(Sender: TObject);
begin
Form1.CS1.Socket.SendText('<|Hello_Server!|>' + Edit1.Text + '<<|);
end;
end.
永远保持最佳状态的表单:
FormStyle属性为: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.