解决方案 »

  1.   

    先要激活窗口参考
    http://stackoverflow.com/questions/9503027/pinvoke-setfocus-to-a-particular-control
      

  2.   


    先感谢下,这个非常有用,但是我现在的问题是我激活了还是不行啊,代码这样的
                        SetActiveWindow(hwndCalc);// hwndCalc是父窗口的句柄
                        SetFocus(Codebox);//  CodeBox是子控件文本框的句柄
    但是我只要焦点不再文本框,就还是无法设置焦点在codebox上啊
      

  3.   

      private bool SetFocusOnText()
            {
                int targetThreadID;
                GetWindowThreadProcessId(hwndCalc,out targetThreadID);
                int myThreadID = Thread.CurrentThread.ManagedThreadId;
                AttachThreadInput(targetThreadID, myThreadID, true );
                bool a = BringWindowToTop(hwndCalc);
                bool b = SetForegroundWindow(hwndCalc);
                IntPtr c = SetFocus(hwndCalc);//设置焦点
                return true;
            }求助啊。。我具体的代码是这样了,前面那么多都是按照那个例子做的铺垫,为的只是最后异步设置焦点,但是还是不成功,断点看下a,b都是true,但c是0,这步还是不行,不知为何了