gpt4 book ai didi

forms - 类似于 TApplication 的 SendToBack

转载 作者:行者123 更新时间:2023-12-03 18:27:16 25 4
gpt4 key购买 nike

祝大家有美好的一天!

我想在我的应用程序中将不同形式的工作模拟为不同的程序。
所有表单在任务栏上都有自己的按钮,应用程序的主窗体是不可见的并且 Application.ShowMainForm := false。
但是如果我显示两个表单,然后打开一些覆盖两个表单的程序,然后打开第一个表单(第二个表单在某个程序后面),然后关闭第一个表单,第二个表单在某个程序前面激活并恢复。
我知道它会恢复,因为在关闭第一个表单后,我的应用程序保持事件状态,这就是显示第一个可见表单的原因。如何防止恢复第二种形式?似乎我需要在关闭后发送回我的应用程序,但我不知道如何。

@David Heffernan
It's actually Windows that is behind all of that. When one of your forms is closed, Windows has to decide where to move the focus too. And it chooses to move it to another top-level window in your process, since a visible one exists. It makes this happen by sending the window that it selects a WM_SETFOCUS message. No doubt you can intercept this and stop it happening



我试图在我的窗口上拦截 WM_SETFOCUS,但没有这样的消息。
type
TfMyForm = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FOldWindowProc: TWndMethod;
procedure NewWindowProc(var Message: TMessage);
end;

implementation

procedure TfMyForm.FormCreate(Sender: TObject);
begin
FOldWindowProc := WindowProc;
WindowProc := NewWindowProc;
end;

procedure TfMyForm.NewWindowProc(var Message: TMessage);
begin
if Message.Msg = WM_SETFOCUS then
Beep;
FOldWindowProc(Message);
end;

最佳答案

这是我自己找到的解决方案。

procedure SwitchToPreviousWindow(AHandle: HWND);
var PrevWindow: HWND;
begin
PrevWindow := GetNextWindow(AHandle, GW_HWNDNEXT);
while PrevWindow <> NULL do
begin
if IsWindowVisible(PrevWindow) then
begin
SetForegroundWindow(PrevWindow);
Exit;
end;
PrevWindow := GetNextWindow(PrevWindow, GW_HWNDNEXT);
end;
end;

procedure TfMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SwitchToPreviousWindow(Self.Handle);
end;

关于forms - 类似于 TApplication 的 SendToBack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20545142/

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