设置TabTip键盘c#的位置不起作用

时间:2014-03-25 09:49:14

标签: c# winforms dllimport

我试图重新定位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();

    }

`

1 个答案:

答案 0 :(得分:0)

如果您在创建流程之后稍微延迟,它将起作用:

 Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
 await Task.Delay(150); // Just a tiny delay before continuing
 ...
 MoveKeyBoard(TabTipHandle, 100, 100);
 ...

然而,现在我面临着一个奇怪的问题,即使用SetWindowPos键盘将无法准确定位到我想要的位置。窗口似乎在点附近摆动,直到几次调用SetWindowPos后它保持固定状态。很奇怪,如果你问我的话。在没有任何文档的情况下,我搜索了注册表,因为我注意到TabTip.exe将从它关闭的完全相同的位置开始。所以我找到了这两个DWORD注册表值:

HKCU\SOFTWARE\Microsoft\TabletTip\1.7\OptimizedKeyboardRelativeXPositionOnScreen
HKCU\SOFTWARE\Microsoft\TabletTip\1.7\OptimizedKeyboardRelativeYPositionOnScreen

我尝试了这些值,似乎在开始处理之前将两者都设置为50000会将键盘的左上角定位到屏幕的中心。将两者设置为0都会将其精确定位在左上角,而100000两者都会相应地设置在右上角。