【问题标题】:Lost Focus Windows Mobile失去焦点 Windows Mobile
【发布时间】:2014-06-05 17:21:26
【问题描述】:

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

第一次申请-> 2次申请-> 3次申请

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

【问题讨论】:

  • 问题不是很详细。第三个应用程序窗口会发生什么?它是否最小化/隐藏?如果是这样,您需要使用 FindWindow() 和 SetWindowPos(两者都可以通过 pinvoke 使用)将第三个应用程序的窗口(如果有)置于前面。
  • 它被最小化并出现在第一个应用程序中。在 windows Mobile 中 FindWindow() 和 SetWindowPos 是怎么做的?

标签: c# windows-mobile-5.0


【解决方案1】:

您需要知道窗口类和/或标题,然后可以使用 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”

...
    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.");
    }

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

除了额外的 SetWindowPos,您还可以使用简单的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.");
    }

有关 FindWindow 和 SetWindowPos 的 pinvoke 的详细信息,请参阅pinvoke.net 和 MSDN。关于 Win32 编程的最佳书籍是 Charles Petzold 的 Programming Windows。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多