'Handle WM_CLOSE message send to C# Tray App
I found a couple of articles telling me how to make use of the WM_CLOSE message but never the less my application is the one who has to handle the WM_CLOSE message.
Is there a way to hook up the WM_CLOSE and handle it? Because the WM_CLOSE only closes the tray icon but does not terminate the process itself ...
Regards,
Solution 1:[1]
We can deal with WM_CLOSE message by adding a MessageFilter to Application:
private class CloseMessageFilter : IMessageFilter
{
const int WM_CLOSE = 0x0010;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_CLOSE)
Application.Exit();
return false;
}
}
static void Main()
{
// ...
Application.AddMessageFilter(new CloseMessageFilter());
Application.Run();
}
Tested under .NET 6 only.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Winston Feng |
