4

I've created an application that starts off in the system tray when it is started. I used the below post to achieve this: How to start WinForm app minimized to tray?

This application also only allows a single instance to run: http://www.codeproject.com/Articles/32908/C-Single-Instance-App-With-the-Ability-To-Restore

The problem I'm getting is when I first start the application it minmizes to the system tray, but if I click the desktop icon it does not appear. I have to click on the icon in the tray to restore the application. If I then minimize it again and then click on the desktop icon it appears.

This is my second attempt at a winform application, is it something to do with the SetVisibleCore?

Any pointers in the right direction would be great.

Community
  • 1
  • 1
Boomerang
  • 782
  • 2
  • 11
  • 20
  • Have a look at this question: http://stackoverflow.com/questions/4611828/maximize-application-in-system-tray – M4N May 10 '12 at 07:38
  • I use the code from the two links above, http://www.codeproject.com/KB/cs/SingleInstanceAppMutex/SingleInstanceMutexSampleV2.txt – Boomerang May 10 '12 at 08:04
  • Visit https://stackoverflow.com/questions/21154693/activate-a-hidden-wpf-application-when-trying-to-run-a-second-instance, for a answer for WPF application – Sajjad Soomro Feb 08 '21 at 23:24

2 Answers2

1

If you make your WinForms application a singleton, then it is very easy to make the minimized window restore,

http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx

It is just another variant of using WindowsFormsApplicationBase from Microsoft.VisualBasic.ApplicationServices namespace. Easier/better than using a Mutex.

You might change

    void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
    {
        Form1 form = MainForm as Form1; //My derived form type
        form.LoadFile(e.CommandLine[1]);
    }

to

    void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
    {
        Form1 form = MainForm as Form1; //My derived form type
        form.Show();
        form.WindowState = FormWindowState.Normal;
    }
Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • Hi,it's a Winform application? – Boomerang May 10 '12 at 08:01
  • Just tried the example given and it doesn't work at all when I click on my desktop icon, its still displayed in my system tray though. Would any of the code in this http://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray cause any conflict? – Boomerang May 10 '12 at 08:57
  • You only need one of them. If you use WindowsFormsApplicationBase as I suggested, then please don't use any Mutex related trick. – Lex Li May 10 '12 at 11:45
  • 1
    I removed the mutex code in the static void Main() and used the example given and doesn't work in my scenario it doesn't work ie. When I first launch the application it starts it in the system tray. If I click on the desktop shortcut it does not maxmise it. If I double click on the icon in the system tray it maxmizes without any problem. If I the minmize to the system tray and then click on the desktop icon it opens ok. I think it's when it is first launched. I think something isn't registering. – Boomerang May 10 '12 at 12:31
0

What if you write the restore logic in your main. You can do this by using ShowWindow function and the SW_MAXIMIZE flag.

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    private const int SW_MAXIMIZE = 9; //Command to restore the window

    [STAThread]
    static void Main()
    {
        bool onlyInstance = false;
        Mutex mutex = new Mutex(true, "UniqueApplicationName", out onlyInstance);
        if (!onlyInstance) 
        {
             Process[] p = Process.GetProcessesByName("UniqueApplicationName");
             SetForegroundWindow(p[0].MainWindowHandle);
             ShowWindow(p[0].MainWindowHandle, SW_MAXIMIZE);
             return;
        }
        Application.Run(new MainForm);
        GC.KeepAlive(mutex);
}
ABH
  • 3,391
  • 23
  • 26