Question
How to hide console application into Tray?
I want my console application to be hidden in the system tray after it starts. When working with a GUI application, I used TTrayIcon, and it worked without any problems. For the console application, I used the ShowWindow
function with SW_HIDE
and TNotifyIconData
to create an icon in the tray, but I encountered a problem with the console icon still appearing in the Windows taskbar.
procedure HideShowConsoleWindow(isShowForm: Boolean);
var
Style: LongInt;
app: THandle;
begin
app := GetConsoleWindow;
Style := GetWindowLong(GetConsoleWindow, GWL_EXSTYLE);
if isShowForm then
begin
SetWindowLong(app, GWL_EXSTYLE, Style or WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW);
ShowWindow(app, SW_HIDE);
end
else
begin
SetWindowLong(app, GWL_EXSTYLE, Style and not WS_EX_TOOLWINDOW or WS_EX_APPWINDOW);
ShowWindow(app, SW_SHOW);
end;
end;
procedure AddTrayIcon;
var
NotifyIconData: TNotifyIconData;
begin
FillChar(NotifyIconData, SizeOf(TNotifyIconData), 0);
NotifyIconData.cbSize := SizeOf(TNotifyIconData);
NotifyIconData.Wnd := GetConsoleWindow;
NotifyIconData.uID := 1;
NotifyIconData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
NotifyIconData.uCallbackMessage := $0400 + 1;
NotifyIconData.hIcon := LoadIcon(0, IDI_APPLICATION);
StrPCopy(NotifyIconData.szTip, 'My Console App');
Shell_NotifyIcon(NIM_ADD, @NotifyIconData);
end;
Is there any way to hide the console application from the taskbar and add it to the system tray?