C# 失焦Windows Mobile

C# 失焦Windows Mobile,c#,windows-mobile-5.0,C#,Windows Mobile 5.0,我在C#中创建了一个应用程序,以满足企业中现有应用程序的一些需求。最近,我们不得不购买另一个应用程序来支持计费。这些正在运行的应用程序如下所示: 第一次申请->第二次申请->第三次申请 当我为第三个应用程序执行“Process.Start”时,它会打开,但几秒钟后第一个应用程序会失去焦点。有人知道如何避免这种情况吗?您需要知道窗口类和/或标题,然后可以使用FindWindow获取窗口句柄: [DllImport("coredll.dll", EntryPoint="FindWindowW", S

我在C#中创建了一个应用程序,以满足企业中现有应用程序的一些需求。最近,我们不得不购买另一个应用程序来支持计费。这些正在运行的应用程序如下所示:

第一次申请->第二次申请->第三次申请


当我为第三个应用程序执行“Process.Start”时,它会打开,但几秒钟后第一个应用程序会失去焦点。有人知道如何避免这种情况吗?

您需要知道窗口类和/或标题,然后可以使用FindWindow获取窗口句柄:

[DllImport("coredll.dll", EntryPoint="FindWindowW", SetLastError=true)]
private static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);
使用窗口手柄,您可以使用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);
例如,如果窗口的类名为“App 3”

要查找窗口的类别和标题,请在应用程序3启动时启动CE远程工具“CE Spy”(VS安装的一部分)。然后浏览窗口列表并查看应用程序3窗口。双击列表中的条目,您将获得应用程序3的类别名称和标题

除了额外的SetWindowPos,您还可以使用简单的API:

有关FindWindow的pinvoke和SetWindowPos的详细信息,请参阅和MSDN。有关Win32编程的最佳书籍是Charles Petzold的编程窗口


当您开始这个过程时,您需要操作系统在更改窗口之前给您一些时间来处理应用程序(比如说1-3秒)。

问题不是很详细。第三个应用程序窗口会发生什么情况?它是否最小化/隐藏?如果是,您需要使用FindWindow()和SetWindowPos(都可以通过pinvoke使用)来打开窗口(如果有)第三个应用程序放在前面。它使我们最小化并显示为不适用于第一个应用程序。在windows Mobile中,FindWindow()和SetWindowPos如何操作?
...
    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.");
    }
[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.");
    }