setwindowpos c#方法在删除调试器时不起作用

时间:2017-09-15 09:10:21

标签: c# winforms process

我已经编写了一个代码,用于在进程启动时将OSK键盘定位到屏幕的左下角。我发现的是当我在句柄处于活动状态时放置调试器时代码正常工作。但是一旦我删除所有调试点,并运行Windows窗体,OSK就不会位于左下角。我发现当删除所有调试点时,句柄KeyboardWnd的值为0。

我也试图延迟这个过程以检查句柄是否加载到那时但它不起作用。此外,“await”不是一个选项,因为它需要异步调用

以下是参考代码

 string osk = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "system32"), "osk.exe");

            //  start the keyboard process
            ProcessStartInfo startInfo = new ProcessStartInfo(osk);                
            Process tmp = Process.Start(startInfo);
            IntPtr KeyboardWnd = GetTabTipWindowHandle();

              System.Threading.Thread.Sleep(20);

            MoveKeyBoard(KeyboardWnd, 0, Screen.PrimaryScreen.Bounds.Height / 4*3);

MoveKeyBoard inturn调用setwindowpos方法,如下所示

 public static bool MoveKeyBoard(IntPtr hWnd, int ToX, int ToY)
    {           
        return SetWindowPos(hWnd, (IntPtr)0, ToX, ToY, Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/4, SWP.NOZORDER | SWP.SHOWWINDOW | SWP.NOCOPYBITS);
    }

 public static IntPtr GetTabTipWindowHandle()
    {
        if (TabTip.UseTabTipKeyboard)
            return FindWindow("IPTip_Main_Window", null);
        else            
            return FindWindow("OSKMainClass", null);

    }

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。 下面的代码工作正常

 RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Osk", true);

                myKey.SetValue("WindowLeft", 0, RegistryValueKind.DWord);
                myKey.SetValue("WindowTop", Screen.PrimaryScreen.Bounds.Height / 4 * 3, RegistryValueKind.DWord);
                myKey.SetValue("WindowWidth", Screen.PrimaryScreen.Bounds.Width / 2, RegistryValueKind.DWord);
                myKey.SetValue("WindowHeight", Screen.PrimaryScreen.Bounds.Height / 4, RegistryValueKind.DWord);
                 IntPtr KeyboardWnd = GetTabTipWindowHandle();


                   MoveKeyBoard(KeyboardWnd, 0, Screen.PrimaryScreen.Bounds.Height / 4 * 3);