gpt4 book ai didi

windows - 在多窗体应用程序中更改窗口顺序

转载 作者:可可西里 更新时间:2023-11-01 10:03:01 25 4
gpt4 key购买 nike

我有一个包含一些非模态表单的应用程序,每个表单都有自己的图标。我需要任务栏上所有窗体的图标在最小化/恢复时不会消失,经过一些测试,这是我的解决方案。

申请

Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;

TForm1 - 带有一个 TButton

的主窗体
procedure TForm1.btn1Click(Sender: TObject);
begin
TForm2.Create(Application).Show;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
SetWindowLong(Handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
Application.OnRestore := FormShow;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
ShowWindow(Application.Handle, SW_HIDE);
end;

TForm2

procedure TForm2.FormCreate(Sender: TObject);
begin
SetWindowLong(Handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
end;

这将在任务栏上创建 2 个图标,在 Alt + Tab 中创建 2 个窗口,两者都按预期工作,除了一件事......切换应用程序会移动所有以前的应用程序窗口在当前应用程序窗口之前,而不仅仅是一个窗口。

例如,我的应用程序有主窗体和其他非模态窗体。如果我在 Google Chrome 中并按 Alt + Tab,那么这将应用程序,这很好。

enter image description here

但这会将我所有的应用程序窗口移动到 Google Chrome 之前,然后在下一个 Alt + Tab 我看到这个,所以我必须按 Alt + 2x Tab 返回 Chrome。

enter image description here

我想实现这种行为,就好像我有更多的应用程序,而不是一个有多个窗口的应用程序。

enter image description here

我不确定它到底是如何工作的,但我假设后台有多个列表,一个用于所有应用程序,一个用于应用程序的窗口,所以当我切换应用程序时,它会在列表中移动前一个,因此它的所有窗口。

如果这是它的工作原理,是否可以选择切换应用程序而不仅仅是窗口?如果不是,是否可以将行为更改为不移动所有窗口,而只移动一个事件窗口,或者我的整个过程是错误的,并且可以以不同的方式实现相同的效果,但它应该在哪里工作?

最佳答案

现在我更好地理解了您的问题,问题是 TApplication 也有一个窗口,并且在您看到的行为中发挥作用。解决方案相当简单,确保所有顶层窗口都有 WS_EX_APPWINDOW EXCEPT TApplication。第二个问题是 TApplication 是这些窗口的父级,而不是桌面,因此您需要指定它。

表格 1:

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
strict protected
procedure CreateParams(var Params: TCreateParams); override;
public
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses
Unit2;

{ TForm1 }

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
// make desktop the owner, not the TApplication window
Params.WndParent := GetDesktopWindow;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
SetWindowLong(Application.Handle, GWL_EXSTYLE, GetWindowLong(Application.Handle,GWL_EXSTYLE) and not WS_EX_APPWINDOW or WS_EX_TOOLWINDOW);
Form2 := TForm2.Create(Application);
Form2.Show;
end;

end.

表格 2:

unit Unit2;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
TForm2 = class(TForm)
strict protected
procedure CreateParams(var Params: TCreateParams); override;
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

{ TForm2 }

procedure TForm2.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
// make desktop the owner, not the TApplication window
Params.WndParent := GetDesktopWindow;
end;

end.

关于windows - 在多窗体应用程序中更改窗口顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796493/

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