I want to be able to remove my Win32 application's button from the Taskbar. I also want to be able to add it back later. How can this be done? I found this approach, but it is written in Delphi, and I'm using C++ instead.
I tried modifying this code by changing one line of Remy's code from:
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_APPWINDOW);
to
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_TOOLWINDOW);
But this doesn't work, the button is still on the Taskbar.
UPDATE: the code I'm using (that originated from Remy of course):
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 https://stackoverflow.com/questions/28929163/how-to-show-a-secondary-form-on-taskbar-using-fmx-c
{
LONG Style = GetWindowLong(hWnd, GWL_EXSTYLE); // <-- don't forget this step!
SetWindowLong(hWnd, GWL_EXSTYLE, Style | WS_EX_APPWINDOW);
}
}
Using C++Builder 10.2 Version 25.0.31059.3231.
It is not enough to just add the WS_EX_TOOLWINDOW
style if you also leave behind the default WS_EX_APPWINDOW
style in place. Try using this instead:
LONG_PTR ExStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, (Style & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW);
Though, the easier way to make a TForm
behave like a tool window is to simply set its BorderStyle
property to bsToolWindow
or bsSizeToolWin
.
However, note that in XE7+, you should use 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 to the question you dismissed just because it was written in Delphi instead of C++. The relevant function calls don't change, just the code syntax.
Try this:
#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
}