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

问题描述:

我想重新定位TabTib键盘而没有成功SetWindowPos函数返回“True”,但键盘没有移动。 我使用C#的窗口上7.TabTip键盘c#设置位置不起作用

` [的DllImport( “USER32.DLL”)] 公共静态的extern BOOL的ShowWindow(IntPtr的的hWnd,整数的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(); 

    } 

`

+0

你确定手柄是正确的吗?您可以使用'spyxx.exe'程序来确保'FindWindow'返回的句柄是正确的。 –

+0

句柄正常我用spy ++来检查它。 我通过使用“Process.GetProcessesByName(”TabTip“);” – ofer

只是创造像这样的过程之后,如果你把一个小的延迟它将工作:

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表示相应的右上角。