C# 在Win 8.1平板电脑上,IsWindowVisible始终返回true

C# 在Win 8.1平板电脑上,IsWindowVisible始终返回true,c#,windows-8.1,tablet,.net-4.5,C#,Windows 8.1,Tablet,.net 4.5,我正在开发dekstop应用程序,该应用程序将在windows平板电脑(C#)上使用,并在某个时候想知道屏幕上的TabTip.exe键盘是否可见。问题是,当我在运行Win 8.1的计算机和使用Win 8.1的平板电脑上测试它时,行为是不同的。我将从测试应用程序中复制小代码部分 我要做的是使用user32.dll来找出这个问题 [DllImport("user32.dll")] public static extern IntPtr FindWindow(String sClass

我正在开发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相同。在tblet上,无论是否看到键盘,GetWindowRect都返回TRUE。值Top、Bottom、Left、Right aleays具有值。在普通计算机上,当TabTip不可见时,GetWindowRect返回FALSE。有谁能解释一下我如何检测TabTip在平板电脑上是否可见(在当前窗口中可见)?

我发布了一个答案,但这里再次为您保存一个点击:

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;
        }
    }
}
使用系统;
使用系统诊断;
使用Microsoft.Win32;
使用System.Runtime.InteropServices;
使用系统线程;
命名空间测试
{
班级计划
{
/// 
///窗口已禁用。请参阅http://msdn.microsoft.com/en-gb/library/windows/desktop/ms632600(v=vs.85)。
/// 
公共警察UInt32 WS_DISABLED=0x8000000;
/// 
///指定要检索的窗口样式。
/// 
公共const int GWL_STYLE=-16;
[DllImport(“user32.dll”)]
公共静态外部IntPtr FindWindow(字符串sClassName,字符串sAppName);
[DllImport(“user32.dll”,SetLastError=true)]
静态外部UInt32 GetWindowLong(IntPtr hWnd、int nIndex);
静态void Main(字符串[]参数)
{
//轮询窗口状态的糟糕循环。
while(true)
{
如果(IsKeyboardVisible())
{
Console.WriteLine(“键盘可见”);
}
其他的
{
Console.WriteLine(“键盘不可见”);
}
睡眠(1000);
}
}
/// 
///获取虚拟键盘的窗口处理程序。
/// 
///把手。
公共静态IntPtr GetKeyboardWindowHandle()
{
返回FindWindow(“IPTip_主窗口”,null);
}
/// 
///检查虚拟键盘是否可见。
/// 
///如果可见,则为True。
公共静态bool IsKeyboardVisible()
{
IntPtr keyboardHandle=GetKeyboardWindowHandle();
bool可见=错误;
if(键盘手柄!=IntPtr.Zero)
{
UInt32样式=GetWindowLong(键盘手柄,GWL_样式);
可见=((样式和WS-U禁用)!=WS-U禁用);
}
返回可见;
}
}
}

实际上
IsWindowVisible
只是检查标志WS\u VISIBLE。尝试使用
GetWindowRect
GetWindowLong
。可能有一个线索,比如:窗口实际上并没有隐藏,只是在你们看不到的情况下移动了一些东西……错误检查是不够的。IsWindowVisible(null)将返回什么是猜测,因为null窗口句柄可以替代桌面窗口。GetWindowRect似乎与IsWindowVisible一样可用。我可以在运行8.1的计算机上检测到它,但不能在平板电脑上检测到,例如GetWindowRect返回TRUE,平板电脑上的Top和bottom总是有值(不是0)。。。平板电脑似乎无法识别TabTip的关闭(隐藏?)事件。有人知道我如何在平板电脑上检测到标签提示是否可见吗?可能重复: