我在按钮用SENDKEYS.SEND()发送其它窗口时能发出键值,在自己程序里发现窗口为什么发不出键值?
代码:
   IntPtr hd = FindWindow("Internet Explorer_TridentDlgFrame", "修改密码 -- 网页对话框");
                                                if (hd != IntPtr.Zero) //修改密码失败
                                                {
                                                    
                                                    Rectangle rc = new Rectangle();
                                                    GetWindowRect(hd, ref rc);
                                                    SetWindowPos(hd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                                                    Cursor.Position = new Point(rc.Left + 180, rc.Top + 50); //新密码
                                                    mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
                                                    mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);
                                                    
                                                    System.Windows.Forms.SendKeys.Send(passw);                                                    System.Windows.Forms.SendKeys.Send("{TAB}");                                                    System.Windows.Forms.SendKeys.Send(passw);
                                                    
                                                    Cursor.Position = new Point(rc.Left + 140, rc.Top + 105);
                                                    mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
                                                    mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);                                                }
麻烦高手帮忙查看下原码 其中passw是全程变量,有值。

解决方案 »

  1.   

    SendKeys.Send貌似只能一个字符一个字符的发,不能发字符串。如果发字符串,你可以用
    api : SendInput
      

  2.   

    SendKeys.Send可以发字符串,主要是现在的窗口是该程序的TIMER中网页弹出的,其它程序弹出的这窗口,要这代码正常。程序自己弹出的窗口就不行了!我试下api : SendInput
      

  3.   

    解决了,多发几次
      hd = FindWindow("Internet Explorer_TridentDlgFrame", "修改密码 -- 网页对话框");
                                                    if (hd != IntPtr.Zero) //修改密码失败
                                                    {
                                                      
                                                        Rectangle rc = new Rectangle();
                                                        GetWindowRect(hd, ref rc);
                                                        SetWindowPos(hd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                                                        Cursor.Position = new Point(rc.Left + 180, rc.Top + 50); //新密码                                                    mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
                                                        mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);                                                    SendKeys.Send(passw);                                                    SendKeys.Send("{TAB}");                                                    SendKeys.Send(passw);
                                                        k++;
                                                        if (k == 3)
                                                        {
                                                            Cursor.Position = new Point(rc.Left + 140, rc.Top + 105);
                                                            mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, 0);
                                                            mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, 0);
                                                            k = 0;
                                                            w_state++;
                                                        }
                                                    }