Maximizing and bringing another process main window on top

C

cvetomir.todorov

We have an app which must be single instance. When we start the app we
check if it is already running (using the Process object in the
System.Diagnostics namespace) locating it by the name of the process.
It is not the problem. When it is already running we have to maximize
the already started application and bring it on top of other windows.

When we find our process we get the main window handle from the Process
object:
IntPtr hWnd = alreadyStartedAppProcess.MainWindowHandle;

We try to maximize it using Win32 API, using methods like :

1)
public static extern int SendMessage(int hWnd, uint Msg, int wParam,
int lParam);

in that way:
SendMessage(hwnd, WM_SYSCOMMAND, WM_MAXIMIZE | WM_SETFOCUS, 0);

//obtained from http://blogs.wwwcoder.com/ThePowderRanger/
Using it in that way only maximizing works.

2)
public static extern bool ShowWindow(int hWnd,int nCmdShow);

in that way:
ShowWindow(hWnd, SW_SHOWMAXIMIZED);

//info about ShowWindow can be found on
//http://msdn.microsoft.com/library/d...indowreference/windowfunctions/showwindow.asp

Using it in that way has the desired effect except in the case when our
already started app window is already maximized. Then it does not
appear on top of the other windows.

3)
We have found a workaround which is something like:

ShowWindow(hWnd, SW_RESTORE);
ShowWindow(hWnd, SW_SHOWMAXIMIZED);

which has the desired effect in all cases. The problem with it is that
we want to avoid unneccessary resizing of the form, which happens in
the above two calls.

if you have any suggestions please help
thanks in advance
 

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