gpt4 book ai didi

multithreading - Delphi:FormStyle:fsStayOnTop.InputQuery 在线程中并且位于表单后面

转载 作者:行者123 更新时间:2023-12-03 15:55:15 32 4
gpt4 key购买 nike

如何解决?表单样式:fsStayOnTop。我在线程中调用输入查询,但它出现在主窗体后面或不可见!

我在线程中动态创建 ZipForge。

procedure StartUpdating.DoPassword;
var
S: String;
begin
if PassSkip then
FSkipFile := True
else if InputQuery('Pas', FFileName, S) then
FPassword := AnsiString(S)
else
begin
PassSkip := True;
FSkipFile := True;
Terminate;
end;
end;
procedure StartUpdating.ZipForgePassword(Sender: TObject; FileName: String;
var NewPassword: AnsiString; var SkipFile: Boolean);
begin
FFileName := FileName;
FPassword := NewPassword;
FSkipFile := SkipFile;
Synchronize(DoPassword);
FileName := FFileName;
NewPassword := FPassword;
SkipFile := FSkipFile;
end;

即使我从线程中调用它,它也对我没有帮助:

Function TForm1.InQuery(cap1: string; cap2: string):bool;
var s:string;
begin
if InputQuery(cap1,cap2,s) then
begin
ThreadUpdating.MainPas:=s;
result:=true;
end else result:=false;
end;

最佳答案

编辑(2)

InputQuery() 显示的表单是一个模态表单(使用 .ShowModal 显示,Delphi 的 VCL 足够智能,可以确保在 a 后面不显示模态表单)最顶层的窗口。本质上是对 DisableTaskWindowsNormalizeAllTopMosts 进行调用,有趣的是 NormalizeAllTopMosts:这确保没有最顶层的窗口显示模态窗体时会显示窗口。这些调用不是直接进行的,VCL 使用了许多技巧来实现它。详细信息可以通过阅读 TCustomForm.ShowModal 的代码找到,Forms.DisableTaskWindowsTApplication.WndProc - 特别是 WM_ENABLE 消息的处理。

需要知道的是,如果主窗体位于最顶层,则似乎存在一个错误,允许在应用程序的主窗体后面显示模态窗体。 Delphi 2010 和 Delphi XE 上都会发生这种情况,没有用旧版本进行测试。测试非常简单:创建一个新的 Delphi VCL 应用程序,在 Form1 上设置 FormStyle=fsStayOnTop,放置一个按钮,然后在按钮的 OnClick 中执行以下操作:

procedure TForm2.Button1Click(Sender: TObject);
var s:string;
begin
s := '';
InputQuery('a', 'b', s);
end;

修复

这应该被视为临时修复,因为显然设计要求在显示模式表单之前“降级”最顶层的窗口。如果您知道您的主窗体位于最顶层,则应该在调用 ShowModal 进行任何操作之前执行类似的操作:

MainForm.FormStyle := fsNormal;
try
YourDialog.ShowModal;
finally MainForm.FormStyle := fsStayOnTop;
end;

InputQuery 的特定情况下,可以使用类似于以下函数的函数。请注意,根据OP在其他问题中的要求,我包含了将文本编辑器变成密码编辑器的功能:

function CustomInputQuery(const Caption, Prompt: string; const Password:Boolean; var Value:string): Boolean;
var F: TForm;
Ed: TEdit;
Lb: TLabel;
Bt: TButton;
WasStayOnTop:Boolean;
begin
F := TForm.Create(nil);
try
F.Caption := Caption;

Lb := TLabel.Create(F);
Lb.Parent := F;
Lb.Caption := Prompt;
Lb.Left := 8;
Lb.Top := 8;

Ed := TEdit.Create(F);
Ed.Parent := F;
Ed.Left := Lb.Left + Lb.Width + 8;
Ed.Top := 8;
Ed.Width := 150;
Ed.Text := Value;
if Password then
Ed.PasswordChar := '*';

Bt := TButton.Create(F);
Bt.Caption := 'Ok';
Bt.Default := True;
Bt.ModalResult := mrOk;
Bt.Left := 8;
Bt.Top := Ed.Top + Ed.Height + 8;
Bt.Parent := F;

Bt := TButton.Create(F);
Bt.Caption := 'Cancel';
Bt.Cancel := True;
Bt.ModalResult := mrCancel;
Bt.Left := 8 + Bt.Width + 8;
Bt.Top := Ed.Top + Ed.Height + 8;
Bt.Parent := F;

F.Width := F.Width - F.ClientWidth + Ed.Left + Ed.Width + 8;
F.Height := F.Height - F.ClientHeight + Bt.Top + Bt.Height + 8;
F.Position := poDesktopCenter;

WasStayOnTop := Assigned(Application.MainForm) and (Application.MainForm.FormStyle = fsStayOnTop);
if WasStayOnTop then Application.MainForm.FormStyle := fsNormal;
try
if F.ShowModal = mrOk then
begin
Value := Ed.Text;
Result := True;
end
else
Result := False;
finally if WasStayOnTop then Application.MainForm.FormStyle := fsStayOnTop;
end;

finally F.Free;
end;
end;

关于multithreading - Delphi:FormStyle:fsStayOnTop.InputQuery 在线程中并且位于表单后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6351648/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com