English 中文(简体)
Bring window to foreground after Mutex fails
原标题:

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance.

E.g.: Application X is running but is in the background. I can t see it so I try to run another instance; because the mutex returns false, the best thing to do is to bring the existing instance to the foreground.

Anyone know the best way to interact with your own application but from a separate process?

最佳回答

You will need to get a hWnd to the other application s main window. The only way I remember how to do that is to loop over all windows until you find yours. There might be a more direct way

[DllImport("user32.dll")] private static extern 
              int EnumWindows(EnumWindowsProc ewp, int lParam); 

public delegate bool EnumWindowsProc(int hWnd, int lParam);

public void EnumerateAllWindows()
{
   EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
   EnumWindows(ewp, 0);
 }

private bool EvalWindow(int hWnd, int lParam)
{
    //this will be called for each window..         
    // use GetWindowThreadProcessId to get the PID for each window
    // and use Process.GetProcessById to get the PID you are looking for
    // then bring it to foregroun using of these calls below:

}

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    public static extern bool BringWindowToTop(IntPtr hWnd);

    DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd
问题回答

one way of performing your goal is to create a named shared memory.

the first application will create the named shared memory and store inside its window handle, process identifier, or any other information useful for this purpose.

when the second application tries to create a shared memory with the same name, it will retrieve a handle for the same shared memory, while the return value will tell you that this object was already existing. the second application can then read the window handle from the shared memory and any other information. it can then use BringWindowToTop(), SetForegroundWindow() or such.

you will find an example of a named shared memory in the Windows SDK. while this example uses straight Win32 API, i am pretty sure you can perform something equivalent in C#.

There is a feature built into .Net to let a second instance of your application communicate with the first instance by raising an event in the first instance. Although it s part of VB.Net, it can be used with C# too.

However it s very important to be aware of the bugs. With some firewalls, it s impossible to open even one instance - your application crashes at startup! See this excellent article by Bill McCarthy for more details, and an alternative technique. He communicates from a second instance back to the first instance with pipes in .NET 3.5. I recommend you use that. His code is VB.Net, but you could probably convert to C# with Telerik.

google for forceForegroundWindow (if you re on windows), anyway





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签