1

I have a Windows Form Application that will go to system tray when it is minimized. When I received a message to pop-up my application it will call ShowWindowFromTray() function. I do not want to steal focus on the application that has the focus because it might interrupts on what the user is doing.

private void ShowWindowFromTray()
{
    this.Show();
    this.WindowState = FormWindowState.Normal;
}

BTW this application has option that the users can check if the application will always on top or TopMost on all other windows.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Wouldn't it be better to display a [balloon tip](http://msdn.microsoft.com/en-us/library/windows/desktop/aa511497.aspx) from your app's icon in the notification area? This is the standard way of showing non-obtrusive alerts/messages. (By the way, it's called the "notification area", not the "system tray". There is no such thing as a "system tray".) – Cody Gray - on strike May 11 '13 at 08:20
  • we also have a balloon tip in the notification area. It is the user's preference, and choices will be made from the user depending on what he chooses on the preference. – Jeff Robert Dagala May 14 '13 at 01:04

1 Answers1

3

Instead of Show(), use the ShowWindow() API with SW_SHOWNA:

    private const int SW_SHOWNA = 4;

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    private void ShowWindowFromTray()
    {
        ShowWindow(this.Handle, SW_SHOWNA);
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • That's awesome. Very very minor nitpick: According to the MSDN you linked 4 is SW_SHOWNOACTIVATE, and 8 is SW_SHOWNA. I do get that they're very similar and both will work for Jeff's purposes. – Jeremy Pridemore May 11 '13 at 04:01
  • Good spot...copying and pasting too quickly. =O – Idle_Mind May 11 '13 at 05:07