forms - 类似于 TApplication 的 SendToBack

标签 forms delphi z-order

祝大家有美好的一天!

我想在我的应用程序中将不同形式的工作模拟为不同的程序。
所有表单在任务栏上都有自己的按钮,应用程序的主表单不可见,并且 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/

相关文章:

python - Matplotlib zorder ax 隐藏其他轴的线

javascript - HTML 表单到 XMLHttprequest

delphi - Delphi 中的选项卡式文档界面

delphi - Delphi 中的丰富 GUI 应用程序

python - 在 matplotlib 中,有没有办法在条/线/补丁下方设置网格线,同时保留上方的刻度标签?

java - JavaFX 中的 Z 顺序

php - 如何解决ContextErrorException : Catchable Fatal Error:

ruby-on-rails - "Please confirm which user you are changing the password for"

php - Wordpress:使用 wp_insert_post() 填充自定义帖子类型字段

delphi - 保存 VirtualStringTree 节点数据