【问题标题】:Set position of TabTip keyboard c# is not working设置 TabTip 键盘 c# 的位置不起作用
【发布时间】: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");" 得到相同的句柄

标签: c# winforms dllimport


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 2023-03-29
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多