2017-09-15 31 views
0

我已经写了一个代码来定位OSK键盘到屏幕的左下角,当过程已经注视。我发现的是,当我在把手处于活动状态时放置了一个调试器时,代码正常工作。但是,当我删除所有调试点并运行窗口窗体时,OSK不会位于左下方。我发现当所有的调试点都被删除时,KeyboardWnd的值为0。setwindowpos c#方法不起作用当调试器被删除

我试图推迟这个过程,以检查如果手柄加载到那时,但它不工作。同时“等待”是不是一种选择,因为它需要异步为其呼叫

下面是引用代码

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为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); 

    } 
+0

方法GetTabTipWindowHandle()在哪里? – jdweng

+0

我已经做了编辑。请看一看。 –

+0

调试器的优点是它会减慢程序速度。所以OSK有足够的时间来创建窗口,FindWindow()可以找到它。你必须等待给它一个初始化的机会。添加tmp.WaitForInputIdle()是最低要求。 –

回答

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);