我正在开发dekstop应用程序,它将在Windows平板电脑(C#)上使用,并且在某些时候想知道TabTip.exe屏幕键盘是否可见。问题是,当我在运行 Win 8.1 的计算机和装有 Win 8.1 的平板电脑上测试它时,行为是不同的。我将从我的测试应用程序中复制小代码部分。
我所做的是我用户用户32.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。值顶部、底部、左侧、右侧具有值。在常规计算机上,当 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;
}
}
}