Windows 在 FMX 中隐藏任务栏按钮
Hide Taskbar button in FMX on Windows
我希望能够从任务栏中删除我的 Win32 应用程序按钮。我也希望能够稍后将其添加回来。如何才能做到这一点?我找到了this approach,但它是用Delphi写的,我用的是C++。
我尝试 更改 Remy 的一行代码:
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_APPWINDOW);
到
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_TOOLWINDOW);
但这不起作用,按钮仍在任务栏上。
更新:我正在使用的代码(当然源自 Remy):
void __fastcall TForm1::CreateHandle() // this is code from Remy i added to help me trap screen lock
{
TForm::CreateHandle();
HWND hWnd = Fmx::Platform::Win::FormToHWND(this);
if (SetWindowSubclass(hWnd, &SubclassWndProc, 1, reinterpret_cast<DWORD_PTR>(this)))
{
MonitoringWTS = WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_THIS_SESSION);
if (!MonitoringWTS)
RemoveWindowSubclass(hWnd, &SubclassWndProc, 1);
}
else {
MonitoringWTS = false;
}
if (hWnd != NULL) // this code added from
{
LONG Style = GetWindowLong(hWnd, GWL_EXSTYLE); // <-- don't forget this step!
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_APPWINDOW);
}
}
使用 C++Builder 10.2 版本 25.0.31059.3231.
如果还保留默认的 WS_EX_APPWINDOW
样式,仅添加 WS_EX_TOOLWINDOW
样式是不够的。试试改用这个:
LONG_PTR ExStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, (Style & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW);
不过,让 TForm
像工具 window 一样工作的更简单方法是简单地将其 BorderStyle
属性 设置为 bsToolWindow
或 bsSizeToolWin
.
但是,请注意,在 XE7+ 中,您应该对您忽略的问题使用 ApplicationHWND()
to get the HWND
that is actually on the Taskbar, as it may be different than the TForm
's window. This was even pointed out in an answer,因为它是用 Delphi 而不是 C++ 编写的。相关的函数调用没有变化,只是代码语法。
试试这个:
#include <FMX.Platform.Win.hpp>
#include <Winapi.Windows.hpp>
void HideAppOnTaskbar()
{
HWND hAppWnd = Fmx::Platform::Win::ApplicationHWND();
ShowWindow(hAppWnd, SW_HIDE);
LONG_PTR ExStyle = GetWindowLongPtr(hAppWnd, GWL_EXSTYLE);
SetWindowLongPtr(hAppWnd, GWL_EXSTYLE, (ExStyle & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW);
//ShowWindow(hAppWnd, SW_SHOW);
}
void ShowAppOnTaskbar()
{
HWND hAppWnd = Fmx::Platform::Win::ApplicationHWND();
ShowWindow(hAppWnd, SW_HIDE);
LONG_PTR ExStyle = GetWindowLongPtr(hAppWnd, GWL_EXSTYLE);
SetWindowLongPtr(hAppWnd, GWL_EXSTYLE, (ExStyle & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW);
ShowWindow(hAppWnd, SW_SHOW);
}
void __fastcall TForm1::CreateHandle()
{
//...
HideAppOnTaskbar(); // or ShowAppOnTaskbar(), as needed
}
我希望能够从任务栏中删除我的 Win32 应用程序按钮。我也希望能够稍后将其添加回来。如何才能做到这一点?我找到了this approach,但它是用Delphi写的,我用的是C++。
我尝试
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_APPWINDOW);
到
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_TOOLWINDOW);
但这不起作用,按钮仍在任务栏上。
更新:我正在使用的代码(当然源自 Remy):
void __fastcall TForm1::CreateHandle() // this is code from Remy i added to help me trap screen lock
{
TForm::CreateHandle();
HWND hWnd = Fmx::Platform::Win::FormToHWND(this);
if (SetWindowSubclass(hWnd, &SubclassWndProc, 1, reinterpret_cast<DWORD_PTR>(this)))
{
MonitoringWTS = WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_THIS_SESSION);
if (!MonitoringWTS)
RemoveWindowSubclass(hWnd, &SubclassWndProc, 1);
}
else {
MonitoringWTS = false;
}
if (hWnd != NULL) // this code added from
{
LONG Style = GetWindowLong(hWnd, GWL_EXSTYLE); // <-- don't forget this step!
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_APPWINDOW);
}
}
使用 C++Builder 10.2 版本 25.0.31059.3231.
如果还保留默认的 WS_EX_APPWINDOW
样式,仅添加 WS_EX_TOOLWINDOW
样式是不够的。试试改用这个:
LONG_PTR ExStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, (Style & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW);
不过,让 TForm
像工具 window 一样工作的更简单方法是简单地将其 BorderStyle
属性 设置为 bsToolWindow
或 bsSizeToolWin
.
但是,请注意,在 XE7+ 中,您应该对您忽略的问题使用 ApplicationHWND()
to get the HWND
that is actually on the Taskbar, as it may be different than the TForm
's window. This was even pointed out in an answer,因为它是用 Delphi 而不是 C++ 编写的。相关的函数调用没有变化,只是代码语法。
试试这个:
#include <FMX.Platform.Win.hpp>
#include <Winapi.Windows.hpp>
void HideAppOnTaskbar()
{
HWND hAppWnd = Fmx::Platform::Win::ApplicationHWND();
ShowWindow(hAppWnd, SW_HIDE);
LONG_PTR ExStyle = GetWindowLongPtr(hAppWnd, GWL_EXSTYLE);
SetWindowLongPtr(hAppWnd, GWL_EXSTYLE, (ExStyle & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW);
//ShowWindow(hAppWnd, SW_SHOW);
}
void ShowAppOnTaskbar()
{
HWND hAppWnd = Fmx::Platform::Win::ApplicationHWND();
ShowWindow(hAppWnd, SW_HIDE);
LONG_PTR ExStyle = GetWindowLongPtr(hAppWnd, GWL_EXSTYLE);
SetWindowLongPtr(hAppWnd, GWL_EXSTYLE, (ExStyle & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW);
ShowWindow(hAppWnd, SW_SHOW);
}
void __fastcall TForm1::CreateHandle()
{
//...
HideAppOnTaskbar(); // or ShowAppOnTaskbar(), as needed
}