隐藏任务栏按钮有效,但在显示第二种形式时无效
Hiding Taskbar Button works, but not when second form is shown
我设法使用
隐藏了我的 winforms 应用程序任务栏按钮
ShowWindow(GetParent(Form1.Handle),SW_HIDE);
我在创建表单后 1 秒调用计时器。任务栏按钮在整个应用程序使用过程中保持隐藏状态,但直到我单击窗体上的按钮以显示另一个窗体,Form1 为所有者。
我尝试使用相同的代码来隐藏第二个表单,但无法正常工作。
编辑:添加更多代码:
Form1 中的代码:
// this fires every 1 second and works well.
procedure TForm1.scanTimerTimer(Sender: TObject);
begin
ShowWindow(GetParent(Form1.Handle),SW_HIDE);
end;
// when a user press Settings button on the Form1
// I open another form.
procedure TForm1.SettingsBtnClick(Sender: TObject);
var
settings: TSettingsForm;
begin
settings := TSettingsForm.Create(Form1);
settings.Show;
end;
SettingsForm 中的代码
// this fires every 1 second and DOESNT WORK!
procedure TSettingsForm.scanTimerTimer(Sender: TObject);
begin
ShowWindow(GetParent(SettingsForm.Handle),SW_HIDE);
end;
这就是代码的全部内容。因此,当我打开 SettingsForm 时,任务栏按钮立即重新出现并且再也不会消失。无论我从主窗体打开多少其他窗体,我都希望任务栏保持隐藏状态。
我为 windows 尝试了一个 "OS specific API",它是
ShowWindow(GetParent(Form1.Handle),SW_HIDE);
在 FormCreate 之后工作,但在主 window 打开辅助 window 之后,
任务栏按钮重新出现。所以如果你的应用只有一个 window,你可以使用它。但是对于多个windows/forms个app,就不行了!
我也尝试了非 OS 具体 API:
SettingsForm.ShowInTaskBar := stNever;
尝试将它放在 FormCreate 中,并且也放在以调用者形式显示之前,但仍然不起作用。任务栏按钮仍然出现。
最后我在 lazarus 论坛上找到了使用 OS 具体 API:
的答案
您需要添加 2 个导入:
InterfaceBase, Win32Int
并把它放在 FormCreate 中:
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
EXStyle: Long;
AppHandle: THandle;
begin
AppHandle := TWin32WidgetSet(WidgetSet).AppHandle;
EXStyle:= GetWindowLong(AppHandle, GWL_EXSTYLE);
SetWindowLong(AppHandle, GWL_EXSTYLE, EXStyle or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
end;
我设法使用
隐藏了我的 winforms 应用程序任务栏按钮ShowWindow(GetParent(Form1.Handle),SW_HIDE);
我在创建表单后 1 秒调用计时器。任务栏按钮在整个应用程序使用过程中保持隐藏状态,但直到我单击窗体上的按钮以显示另一个窗体,Form1 为所有者。
我尝试使用相同的代码来隐藏第二个表单,但无法正常工作。
编辑:添加更多代码:
Form1 中的代码:
// this fires every 1 second and works well.
procedure TForm1.scanTimerTimer(Sender: TObject);
begin
ShowWindow(GetParent(Form1.Handle),SW_HIDE);
end;
// when a user press Settings button on the Form1
// I open another form.
procedure TForm1.SettingsBtnClick(Sender: TObject);
var
settings: TSettingsForm;
begin
settings := TSettingsForm.Create(Form1);
settings.Show;
end;
SettingsForm 中的代码
// this fires every 1 second and DOESNT WORK!
procedure TSettingsForm.scanTimerTimer(Sender: TObject);
begin
ShowWindow(GetParent(SettingsForm.Handle),SW_HIDE);
end;
这就是代码的全部内容。因此,当我打开 SettingsForm 时,任务栏按钮立即重新出现并且再也不会消失。无论我从主窗体打开多少其他窗体,我都希望任务栏保持隐藏状态。
我为 windows 尝试了一个 "OS specific API",它是
ShowWindow(GetParent(Form1.Handle),SW_HIDE);
在 FormCreate 之后工作,但在主 window 打开辅助 window 之后, 任务栏按钮重新出现。所以如果你的应用只有一个 window,你可以使用它。但是对于多个windows/forms个app,就不行了!
我也尝试了非 OS 具体 API:
SettingsForm.ShowInTaskBar := stNever;
尝试将它放在 FormCreate 中,并且也放在以调用者形式显示之前,但仍然不起作用。任务栏按钮仍然出现。
最后我在 lazarus 论坛上找到了使用 OS 具体 API:
的答案您需要添加 2 个导入:
InterfaceBase, Win32Int
并把它放在 FormCreate 中:
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
EXStyle: Long;
AppHandle: THandle;
begin
AppHandle := TWin32WidgetSet(WidgetSet).AppHandle;
EXStyle:= GetWindowLong(AppHandle, GWL_EXSTYLE);
SetWindowLong(AppHandle, GWL_EXSTYLE, EXStyle or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
end;