0

I created an application in C # to address some needs of an existing application in the enterprise. Recently we had to buy another application to support billing. These applications running is as follows:

1st Application-> 2 Application -> 3 Application

When I do "Process.Start" for the third application "it opens but after a few seconds it loses focus for the 1st Application. Anybody know how can I avoid this?

user6018
  • 125
  • 6
  • The question is not very detailed. What happens with the 3rd Application window? Is it minimized/hidden? If so, you need to use FindWindow() and SetWindowPos (both are usable via pinvoke) to bring the window (if any) of 3rd Application into front. – josef Jun 06 '14 at 09:46
  • It us minimized and appears de 1st Aplication. In windows Mobile how do FindWindow() and SetWindowPos? – user6018 Jun 06 '14 at 11:47

1 Answers1

0

You need to know the window class and/or title and then can use FindWindow to get a window handle for the window:

[DllImport("coredll.dll", EntryPoint="FindWindowW", SetLastError=true)]
private static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);

Using the window handle you can change the window back to normal display using SetWindowPos:

[DllImport("user32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

In example, if the window has a class name of "App 3"

...
    IntPtr handle;

    try
    {
    // Find the handle to the window with class name x
    handle = FindWindowCE("App 3", null);
    // If the handle is found then show the window
    if (handle != IntPtr.Zero)
    {
        // show the window
        SetWindowPos(handle, 0, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
    }
    }
    catch
    {
       MessageBox.Show("Could not find window.");
    }

To find the window's class and title, start the CE remote tool "CE Spy" (part of VS installation) when app 3 is started. Then browse thru the window list and look the app 3 window. Double click the entry in the list and you will get the class name and title of app 3.

Instead of the extra SetWindowPos you can also use the simple ShowWindow API:

[DllImport("coredll.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

enum ShowWindowCommands
{
    Hide = 0,
    Normal = 1,
    ShowMinimized = 2,
    Maximize = 3, // is this the right value?
    ShowMaximized = 3,
    ShowNoActivate = 4,
    Show = 5,
    Minimize = 6,
    ShowMinNoActive = 7,
    ShowNA = 8,
    Restore = 9,
    ShowDefault = 10,
    ForceMinimize = 11
}
...
    IntPtr handle;

    try
    {
    // Find the handle to the window with class name x
    handle = FindWindowCE("App 3", null);
    // If the handle is found then show the window
    if (handle != IntPtr.Zero)
    {
        // show the window
        ShowWindow(handle, ShowWindowCommands.Normal);
    }
    }
    catch
    {
       MessageBox.Show("Could not find window.");
    }

For details about pinvoke of FindWindow and SetWindowPos see pinvoke.net and MSDN. Best book about Win32 programming is Charles Petzold's Programming Windows.

When you started the process you need the OS give some time to settle the app (let's say 1-3 seconds), before you change the window.

josef
  • 5,951
  • 1
  • 13
  • 24