【问题标题】:IsWindowVisible always return true on Win 8.1 tabletIsWindowVisible 在 Win 8.1 平板电脑上总是返回 true
【发布时间】:2014-04-02 10:53:46
【问题描述】:

我正在开发 dekstop 应用程序,它将在 Windows 平板电脑(C#)上使用,并且在某些时候想知道 TabTip.exe 屏幕键盘是否可见。问题是,当我在运行 Win 8.1 的计算机和运行 Win 8.1 的平板电脑上测试它时,行为是不同的。我将从我的测试应用程序中复制小的代码部分。

我要做的是使用 user32.dll 来找出答案

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(String sClassName, String sAppName);

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool IsWindowVisible(IntPtr hWnd);

我的示例应用程序中有一个按钮,当我按下按钮时,它会打印出 TabTip 是否可见

    public void IsVisible(object sender, RoutedEventArgs e)
    {
        IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null);
        Console.WriteLine("Keyboard is visible :" + IsWindowVisible(KeyboardWnd));
    }

问题是...当我在运行 Windows 8.1 的计算机上启动它时,我从 IsWindowVisible 获得了正确的值。

在平板电脑上...我总是很真实。有人知道为什么吗?

更新

我尝试使用建议的 GetWindowRect。这与 isWindowVisible 相同。在平板电脑上,无论是否看到键盘,GetWindowRect 都返回 TRUE。值 上、下、左、右 aleays 具有价值。在常规计算机上,当 TabTip 不可见时,GetWindowRect 返回 FALSE。谁能解释我如何检测 TabTip 在 TABLET 上是否可见(在当前窗口中可见)?

【问题讨论】:

  • 实际上IsWindowVisible 只是检查标志 WS_VISIBLE。尝试使用GetWindowRectGetWindowLong。可能有一个线索,比如:窗口实际上并没有隐藏,只是在你看不到它的地方移动......
  • 错误检查不充分。 IsWindowVisible(null) 将返回一个猜测,因为 NULL 窗口句柄可以替代桌面窗口。
  • GetWindowRect 似乎与 IsWindowVisible 一样可用。我可以在运行在 8.1 上但不能在平板电脑上运行的计算机上检测到它,例如 GetWindowRect 返回 TRUE、Top、bottom 在平板电脑上总是有值(不是 0)...似乎平板电脑无法识别关闭(隐藏?) TabTip 的事件。任何人都知道我如何在平板电脑上检测到 TabTip 是否可见?

标签: c# windows-8.1 tablet .net-4.5


【解决方案1】:

我已对此in another thread 发布了答案,但在这里再次为您节省点击:

using System;
using System.Diagnostics;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Threading;

namespace CSharpTesting
{
    class Program
    {
        /// <summary>
        /// The window is disabled. See http://msdn.microsoft.com/en-gb/library/windows/desktop/ms632600(v=vs.85).aspx.
        /// </summary>
        public const UInt32 WS_DISABLED = 0x8000000;

        /// <summary>
        /// Specifies we wish to retrieve window styles.
        /// </summary>
        public const int GWL_STYLE = -16;

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String sClassName, String sAppName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);

        static void Main(string[] args)
        {
            // Crappy loop to poll window state.
            while (true)
            {
                if (IsKeyboardVisible())
                {
                    Console.WriteLine("keyboard is visible");
                }
                else
                {
                    Console.WriteLine("keyboard is NOT visible");
                }

                Thread.Sleep(1000);
            }
        }

        /// <summary>
        /// Gets the window handler for the virtual keyboard.
        /// </summary>
        /// <returns>The handle.</returns>
        public static IntPtr GetKeyboardWindowHandle()
        {
            return FindWindow("IPTip_Main_Window", null);
        }

        /// <summary>
        /// Checks to see if the virtual keyboard is visible.
        /// </summary>
        /// <returns>True if visible.</returns>
        public static bool IsKeyboardVisible()
        {
            IntPtr keyboardHandle = GetKeyboardWindowHandle();

            bool visible = false;

            if (keyboardHandle != IntPtr.Zero)
            {
                UInt32 style = GetWindowLong(keyboardHandle, GWL_STYLE);
                visible = ((style & WS_DISABLED) != WS_DISABLED);
            }

            return visible;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多