【发布时间】:2014-05-03 01:43:21
【问题描述】:
我试图重新定位 TabTib 键盘但没有成功,SetWindowPos 函数返回“True”但键盘没有移动。 我在 Windows 7 上使用 C#。
` [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string ClassName, string WindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
Rectangle KeyboardRect;
IntPtr TabTipHandle;
IntPtr GetWindowHandle()
{
return FindWindow("IPTip_Main_Window",null);
}
bool MoveKeyBoard(IntPtr hWnd, int ToX, int ToY)
{
return SetWindowPos(hWnd, this.Handle, ToX, ToY, KeyboardRect.Width, KeyaboardRect.Height, 0x0045);
}
void StartKeyboard()
{
Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
TabTipHandle = GetWindowHandle();
KeyboardRect = GetKeyboardRect(TabTipHandle);
textBox1.Text = KeyaboardRect.Top.ToString() + ", " + KeyboardRect .Left.ToString() + ", " + KeyboardRect .Width.ToString() + ", " + KeyboardRect .Height.ToString();
MoveKeyBoard(TabTipHandle, 100, 100);
KeyboardRect = GetKeyboardRect(TabTipHandle);
textBox2.Text = KeyaboardRect.Top.ToString() + ", " + KeyboardRect .Left.ToString() + ", " + KeyboardRect .Width.ToString() + ", " + KeyboardRect .Height.ToString();
}
void button1_Click(object sender, EventArgs e)
{
StartKeyboard();
}
void button2_Click(object sender, EventArgs e)
{
MoveKeyBoard(TabTipHandle, 200, 100);
KeyboardRect = GetKeyboardRect(TabTipHandle);
textBox2.Text = KeyboardRect .Top.ToString() + ", " + KeyboardRect .Left.ToString() + ", " + KeyboardRect .Width.ToString() + ", " + KeyboardRect .Height.ToString();
}
`
【问题讨论】:
-
您确定手柄是正确的吗?可以使用
spyxx.exe程序保证FindWindow返回的句柄是正确的 -
句柄没问题我用spy++检查了一下。我使用 "Process.GetProcessesByName("TabTip");" 得到相同的句柄