以编程方式控制屏幕键盘上的Windows 7

时间:2014-01-09 09:05:52

标签: c# .net winforms on-screen-keyboard

我有一个应用程序要在Windows 7平板电脑上运行,并且需要将屏幕键盘停靠在屏幕底部。理想情况下,我想阻止某人移动或更改这些设置。

使用发布到堆栈溢出答案的注释How do I control the text input panel programmatically (TabTip.exe) in Windows Vista/7我能够以编程方式将键盘对接到屏幕底部,这样才能开始。我必须以提升的权限运行才能让它工作

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

string onScreenKeyboardPath = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
var onScreenKeyboardProc = Process.Start(onScreenKeyboardPath);

IntPtr wKB = FindWindow("IPTip_Main_Window", null);

const uint WM_COMMAND = 0x111;
// Where message is 10021 for dock bottom, 10023 for dock top and 10020 for floating
bool x = PostMessage(wKB, WM_COMMAND, new IntPtr(10021), IntPtr.Zero);

我希望能够比这更好地控制尺寸,所以我试图像这样移动窗口:

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

private const uint SWP_SHOWWINDOW = 0x0040;
bool ok = SetWindowPos(wKB, this.Handle, 0, 500, 750, 500, SWP_SHOWWINDOW);

ok返回true但是窗口没有预算。如果我尝试用记事本做到这一点,那就完美了。那么这个特定的程序是一个问题吗?

1 个答案:

答案 0 :(得分:2)

您的问题可分为以下几部分:
1)防止窗户移动
2)防止窗口调整大小
3)防止窗口被最小化

1)似乎很容易:How do you prevent a windows from being moved?

2)& 3)可以在同一步骤中解决。

我对解决方案有不同的想法:
a)创建一个线程,定期检查键盘窗口是否已调整大小/最小化/移动,并使用(例如)SetWindowPos重置其位置 How to get and set the window position of another application in C#
b)"听"用于调整大小/最小化/移动事件(WH_CBT =在任何这些事件之前发生)并结束它。可悲的是,我不知道是否以及如何强制禁用通过WH_CBT宣布的事件 似乎还有另一种解决方案:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx
向下滚动到评论部分,向用户Pavel Shkleinik写道 EVENT_SYSTEM_MOVESIZ *和EVENT_SYSTEM_MINIMIZE *似乎对您的情况很有趣。
您可以检测之前的事件并停止它们(如果我只知道如何)或检测这些事件的结束并强制重置窗口位置(SetWindowPos)。
Pinvoke Wiki和Google将帮助您设置钩子:
http://www.pinvoke.net/default.aspx/user32.setwineventhook