Шаги, которые я предпринял:
Я запускаю экранную клавиатуру (TabTip.exe) с помощью Process.Start().
Я использую FindWindow для поиска окна по его классу имя (IPTip_Main_Window).
Я пытаюсь получить размер окна с помощью GetWindowRect.
Даже после ожидания инициализации окна размеры по-прежнему отображаются как Ширина = 0 и Высота = 0.
Я попытался увеличить задержку с помощью Thread.Sleep(), а также попробовал использовать FindWindowEx для поиска дочерних окон. Несмотря на это, размер окна по-прежнему равен 0x0.
Не могли бы вы подсказать мне следующее:
Почему я получаю 0x0 для размера окна? TabTip.exe?
Существует ли лучший метод или рекомендуемый подход для получения размера окон системного уровня, таких как TabTip.exe?
Существуют ли какие-либо конкретные конфигурации системы (например, режим планшета), которые влияют на то, как размер окна сообщили?
Код: Выделить всё
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class KeyboardControl
{
// P/Invoke declarations
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
// RECT structure to hold window coordinates
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
// Constants for window positioning
static readonly IntPtr HWND_TOP = new IntPtr(0); // To move window to the top
static readonly IntPtr HWND_BOTTOM = new IntPtr(1); // To move window to the bottom
public void OpenKeyboard()
{
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe")
{
WindowStyle = ProcessWindowStyle.Hidden // Hide the keyboard process window
};
// Start the keyboard process
Process keyboardProcess = Process.Start(startInfo);
// Wait for the keyboard process to initialize fully
WaitForWindowInitialization();
// Try to find the main window handle of the TabTip process
IntPtr hwnd = FindWindow("IPTip_Main_Window", null); // Class name for TabTip
// If not found, try looking for child windows of TabTip
if (hwnd == IntPtr.Zero)
{
hwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "IPTip_Main_Window", null);
}
// If still not found, print the failure
if (hwnd == IntPtr.Zero)
{
Console.WriteLine("On-Screen Keyboard window not found.");
return;
}
// Optionally resize the window (even if GetWindowRect reports 0x0)
ResizeKeyboardWindow(hwnd, 800, 200); // Resize to 800x200
// Get the current size of the keyboard window (even if it reports 0x0, we force resizing)
RECT rect;
if (GetWindowRect(hwnd, out rect))
{
Console.WriteLine($"Current Keyboard Window Size: Width={rect.Right - rect.Left}, Height={rect.Bottom - rect.Top}");
}
}
public void WaitForWindowInitialization()
{
int retries = 10; // Retry 10 times
IntPtr hwnd = IntPtr.Zero;
while (retries > 0 && hwnd == IntPtr.Zero)
{
hwnd = FindWindow("IPTip_Main_Window", null); // Search for the TabTip window
if (hwnd == IntPtr.Zero)
{
Console.WriteLine("Waiting for keyboard window...");
System.Threading.Thread.Sleep(2000); // Wait for 2 seconds before retrying
retries--;
}
}
if (hwnd == IntPtr.Zero)
{
Console.WriteLine("Failed to find the keyboard window after several retries.");
}
}
public void ResizeKeyboardWindow(IntPtr hwnd, int width, int height)
{
RECT rect;
if (GetWindowRect(hwnd, out rect))
{
// Resize and move the window (keep the current position and only resize)
SetWindowPos(hwnd, HWND_TOP, rect.Left, rect.Top, width, height, 0);
}
else
{
// If GetWindowRect fails, try resizing directly without checking the size
SetWindowPos(hwnd, HWND_TOP, 100, 100, width, height, 0); // Set to a fixed position
}
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of KeyboardControl and open the keyboard
KeyboardControl control = new KeyboardControl();
control.OpenKeyboard();
}
}
Существует ли лучший метод или рекомендуемый подход для получения размера окон системного уровня, таких как TabTip.exe ?
Существуют ли какие-либо конкретные конфигурации системы (например, режим планшета), влияющие на то, как сообщается размер окна?
Подробнее здесь: https://stackoverflow.com/questions/792 ... -using-win