minimize app to system tray
- C# System Tray Minimize To Tray With NotifyIcon
- Minimize window to system tray
Handle the form’s Resize event. In this handler, you override the
basic functionality of the Resize event to make the form minimize to
the system tray and not to the taskbar. This can be done by doing the
following in your form’s Resize event handler: Check whether the
form’s WindowState property is set to FormWindowState.Minimized. If
yes, hide your form, enable the NotifyIcon object, and show the
balloon tip that shows some information. Once the WindowState becomes
FormWindowState.Normal, disable the NotifyIcon object by setting its
Visible property to false. Now, you want the window to reappear when
you double click on the NotifyIcon object in the taskbar. For this,
handle the NotifyIcon’s MouseDoubleClick event. Here, you show the
form using the Show() method.
private void frmMain_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
mynotifyicon.Visible = true;
mynotifyicon.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
mynotifyicon.Visible = false;
}
}
How to minimize Windows Store app to system tray
You can't minimize Winows Store Apps into the system tray, only Windows Forms and WPF apps can do that.
You can help that happen by voting here UWP System tray support
Trouble with minimizing app to system tray
Try to call
this.Hide()
when the form is minimized, preferably in the Form.Resize
event handler:
private void frmMain_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
this.Hide();
}
At some point you have to call
this.Show()
for example, in the DoubleClick
handler of the NotifyIcon
.
Minimize window to system tray
There is no purely SDL2 way to do this, as Cody said, Shell_NotifyIcon
is the function needed to create a notification area (system tray) icon.
The code I used to get the icon is
SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200, 200, SDL_WINDOW_HIDDEN);
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
NOTIFYICONDATA icon;
if (SDL_GetWindowWMInfo(window, &info))
{
icon.uCallbackMessage = WM_USER + 1;
icon.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
icon.hIcon = LoadIcon(NULL, IDI_INFORMATION);
icon.cbSize = sizeof(icon);
icon.hWnd = info.info.win.window;
strcpy_s(icon.szTip, "Test tip");
bool success = Shell_NotifyIcon(NIM_ADD, &icon);
}
This creates a hidden window, and an icon (using the default information icon).
To interact with this from SDL, you need to enable platform specific window management events, this is done as follows SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
After this, in the main event loop you must test for SDL_SYSWMEVENT
, which contains information about how the user has interacted with the notification area icon. This also looks for the minimize event and hides the window which removes it from the task bar. This is achieved in the following snippet
SDL_Event e;
while (SDL_PollEvent(&e) != 0)
{
switch (e.type)
{
case SDL_SYSWMEVENT:
if (e.syswm.msg->msg.win.msg == WM_USER + 1)
{
if (LOWORD(e.syswm.msg->msg.win.lParam) == WM_LBUTTONDBLCLK)
{
SDL_ShowWindow(window);
SDL_RestoreWindow(window);
}
}
break;
case SDL_QUIT:
running = false;
break;
case SDL_WINDOWEVENT:
if (e.window.event == SDL_WINDOWEVENT_MINIMIZED)
SDL_HideWindow(window);
break;
}
}
minimize app to system tray - no icon appears
You need to set the Icon as shown below:
notifyIcon.Icon = new System.Drawing.Icon(Path to your Icon);
Related Topics
C# "Internal" Access Modifier When Doing Unit Testing
What Are the Benefits of Using C# VS F# or F# VS C#
Inno Setup for Windows Service
How to Easily Initialize a List of Tuples
What Really Happens in a Try { Return X; } Finally { X = Null; } Statement
Could Not Load File or Assembly ... the Parameter Is Incorrect
C# Naming Convention for Constants
Just What Is an Intptr Exactly
The Provided Uri Scheme 'Https' Is Invalid; Expected 'Http'. Parameter Name: Via
Creating Wcf Channelfactory<T>
Best Practice to Make a Multi Language Application in C#/Winforms
Why Can't C# Interfaces Contain Fields
How to Get Current Page Url in MVC 3
Why Does the Contains() Operator Degrade Entity Framework's Performance So Dramatically
Why Are Private Fields Private to the Type, Not the Instance
Error in Process.Start() -- the System Cannot Find the File Specified