How to Activate an App in the System Tray

G

Guest

On May 3, I posted a question asking how to do this. A colleague and I
figured out how to do it. Thought I would pass it along.

// The following code prevents multiple instances of a program
// from being launched and if the currently executing one
// is minimized or covered by another window, it is restored.

[DllImport("User32.dll")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);

// At start-up - Get the number of instances of this app
Process[] procs = Process.GetProcessesByName(Application.ProductName);
if (procs.Length > 1)
{
// the previously running instance will be at either index 0 or 1
int index;
if ((int)procs[0].MainWindowHandle != 0) index = 0;
else index = 1;
SetForegroundWindow(procs[index].MainWindowHandle);
// 9 = SW_RESTORE (winuser.h)
ShowWindow(procs[index].MainWindowHandle, 9);
return; // exit, terminate this instance
}
 
B

Bob Powell [MVP]

Hmm, you obviously missed my Windows Forms Tips and Tricks article. Maybe I
missed your original post.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

I saw your web site but somehow missed the article. I like our solution a
little better. For some reason, when there are two instances running:

RunningProcesses[0].MainWindowHandle

as in your code, is 0. When this happens, the call to ShowWindowAsync
doesn't do anything. (This situation appears to happen when you use the
minimize button on the first instance). The other problem with your code is
that when a first instance window is hidden by another window, but is not
minimized, your code minimizes it first and then restores it. The minimizing
graphics appear visibly out of nowhere and seems a bit unusual.

Anyway, thanks for the input.

Bob Costello

Bob Powell said:
Hmm, you obviously missed my Windows Forms Tips and Tricks article. Maybe I
missed your original post.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Bob Costello said:
On May 3, I posted a question asking how to do this. A colleague and I
figured out how to do it. Thought I would pass it along.

// The following code prevents multiple instances of a program
// from being launched and if the currently executing one
// is minimized or covered by another window, it is restored.

[DllImport("User32.dll")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);

// At start-up - Get the number of instances of this app
Process[] procs = Process.GetProcessesByName(Application.ProductName);
if (procs.Length > 1)
{
// the previously running instance will be at either index 0 or 1
int index;
if ((int)procs[0].MainWindowHandle != 0) index = 0;
else index = 1;
SetForegroundWindow(procs[index].MainWindowHandle);
// 9 = SW_RESTORE (winuser.h)
ShowWindow(procs[index].MainWindowHandle, 9);
return; // exit, terminate this instance
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top