做的一个winform窗体,里面有一个 webbrowser ,窗体一直开着运行正常,但最小化后就行动出错了。
出错的原因是 焦点问题
核心代码如下:
        private void btnStart_Click(object sender, EventArgs e)
        {
          for (int i = 0; i < 100; i++)
         {
          REG();
          webBrowser1.Navigate("http://xxxx.com");
         }
        }
       private void REG()
        {            
            //帐号
            HtmlElement email = webBrowser1.Document.GetElementById("Email");
            while (email == null)
            {
                Application.DoEvents();
                email = webBrowser1.Document.GetElementById("Email");
            }
            email.Focus(); //获得焦点
            email.InnerText = Rodom(10) + "@hotmail.com";  // Rodom()是个生成随机字母的函数
            //密码
            HtmlElement password = webBrowser1.Document.GetElementById("Passwd");
            while (password == null)
            {
                Application.DoEvents();
                password = webBrowser1.Document.GetElementById("Passwd");
            }
            password.Focus();
            password.InnerText = "thankyou8o8";
            //确认密码
            while (password2 == null)
            {
                Application.DoEvents();
                password2 = webBrowser1.Document.GetElementById("PasswdAgain");
            }
            HtmlElement password2 = webBrowser1.Document.GetElementById("PasswdAgain");
            password2.Focus();
            password2.InnerText = "thankyou8o8";            //省
            HtmlElement st = webBrowser1.Document.GetElementById("state");
            while (st == null)
            {
                Application.DoEvents();
                st = webBrowser1.Document.GetElementById("state");
            }
            st.Focus();
            Delay(500);  // 延时
            SendKeys.Send("{DOWN}");           //点击确定
            HtmlElement sub = webBrowser1.Document.GetElementById("submit");
            while (sub == null)
            {
                Application.DoEvents();
                sub = webBrowser1.Document.GetElementById("submit");
            }
          sub.InvokeMember("click");
        }窗口一直开在那运行没有问题,
但是我最小化后,(因为想干些其它事情)下一次循环到 REG() 里就有问题了,就是 xx.Focus(); SendKeys.Send("{DOWN}");
这些都无效了。 比如我打开IE看电影,  SendKeys.Send("{DOWN}"); 作用在我看电影的这里了。
怎么样能让它最小化后不失 焦点呢, 最小化后程序里的 xx.Focus(); SendKeys.Send("{DOWN}"); 仍然作用于它本身呢?

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form10 : Form
        {
            public Form10()
            {
                InitializeComponent();
            }
            //双击图标事件
            private void notifyIcon1_DoubleClick(object sender, EventArgs e)
           {
               if (this.WindowState == FormWindowState.Minimized)
               {               this.WindowState = FormWindowState.Normal;           }           else
               {
                   this.WindowState = FormWindowState.Minimized;
               }
            }        private void Form10_SizeChanged(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {            }
            }
            //关闭窗体
            private void Form10_FormClosing(object sender, FormClosingEventArgs e)
            {
                // 取消关闭事件
                e.Cancel = true;            // 将事件转换成窗体状态改变事件(即最小化),只需将窗体一属性ShowInTaskBar设置为false就不会在任务栏显示了
                this.WindowState = FormWindowState.Minimized;
            }        //加载时
            private void Form10_Load(object sender, EventArgs e)        {
                this.notifyIcon1.ShowBalloonTip(1, "系统提示", "酒店管理系统正在运行~~~", ToolTipIcon.Info);
            }
            //单击托盘图标显示右键菜单图标
            private void notifyIcon1_Click(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {                this.WindowState = FormWindowState.Normal;            }            else
                {
                    this.WindowState = FormWindowState.Minimized;
                }
            }
        
            //退出
            private void toolStripMenuItem1_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("您确定要退出化验数据接收程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    this.notifyIcon1.Visible = false;
                    this.ShowInTaskbar = false;//窗体不在任务栏显示
                    this.Close();
                    this.Dispose();
                }
            }        private void toolStripMenuItem2_Click(object sender, EventArgs e)
            {        }        private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
            {
                contextMenuStrip1.Show();        }
        }
    }