首 页文章中心下载中心繁體中文
设为首页
加入收藏
联系我们
您当前的位置:开源盛世-源代码下载网 -> 文章中心 -> 其他编程 -> VC# 技术 -> 文章内容 退出登录 用户管理
栏目导航
· VC# 技术 · Delphi技术
· Java技术 · 通用算法
· 编程相关
热门文章
· Tab Control控件使用...
· 学生档案管理系统
· [图文] 排列组合公式
· UTF-8与GB2312之间的...
· DirectShow下载安装...
· Virtual PC 在PAE模...
· Windows2000终端服务...
· MapInfo上的GIS系统...
· kalman filter 卡尔...
· Windows2000终端服务...
相关文章
自动登录网站和email的程序
作者:不详  来源:vscodes.com整理  发布时间:2007-8-5 14:27:08  发布人:Polaris

减小字体 增大字体

每天早上一开机就登录博客园已经成了习惯, 总要先打开浏览器, 然后输入www.cnblogs.com,然后等待... ...
要是某天有火箭队的比赛, 还要去关注一下赛事情况: 先打开浏览器, 然后输入www.sohu.com,点NBA,然后... ...
哈哈, 还有一件少不了的事情,就是要登录电子油箱查看一下是否有新邮件...

这么多操作真是麻烦! 能不能通过点击一个按纽来简化这些操作呢?

前两天从同事(ZnS04)那里得到启示, 根据自动化测试的原理, 完全可以...

好了,下面就开始编写吧 ... ...

1).  新建一个 window application 工程.
2). 在窗体上添加3个Button,设置好属性, 如图:

3). 给工程添加2个引用 Microsoft.mshtml 和SHDocVw.dll
4). 编写代码

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8
 9//添加引用
10using SHDocVw;
11using mshtml;
12
13namespace WebExplorer
14{
15    public partial class Form1 : Form
16    {
17        public Form1()
18        {
19            InitializeComponent();
20        }
21
22        /**//// <summary>
23        /// Redirect to the URL page.
24        /// </summary>
25        /// <param name="URL">Your wanted URL.</param>
26        public void GotoURL(string URL)
27        {
28            //实例化一个IE模型
29            SHDocVw.InternetExplorer IE = new InternetExplorer();            
30            IE.Visible = true;
31            object nullArg = null;
32            //引导到URL
33            IE.Navigate(URL, ref nullArg, ref nullArg, ref nullArg, ref nullArg);
34        }
35
36        private void gotocnBlogs_Click(object sender, EventArgs e)
37        {
38            this.GotoURL("www.cnblogs.com");
39        }
40
41        private void gotoNBA_Click(object sender, EventArgs e)
42        {
43            this.GotoURL("sports.sohu.com/nba"); 
44        }
45
46        private void gotoGmail_Click(object sender, EventArgs e)
47        {
48            try
49            {
50                SHDocVw.InternetExplorer IE = new InternetExplorer();
51                IE.Visible = true;
52                string URL = "http://gmail.google.com/";
53                object nullArg = null;
54                IE.Navigate(URL, ref nullArg, ref nullArg, ref nullArg, ref nullArg);
55
56                System.Threading.Thread.Sleep(3000);
57                //得到IE的文档对象模型
58                mshtml.IHTMLDocument2 DOM = (mshtml.IHTMLDocument2)IE.Document;
59                //声明用户名
60                mshtml.IHTMLInputTextElement txtUserName = (mshtml.IHTMLInputTextElement)DOM.all.item("Email", null);
61                txtUserName.value = "YOUE USERNAME";
62                //声明密码
63                mshtml.IHTMLInputTextElement txtPwd = (mshtml.IHTMLInputTextElement)DOM.all.item("Passwd", null);
64                txtPwd.value = "PASSWORD";
65                //声明登录
66                mshtml.HTMLInputElement btnLogin = (mshtml.HTMLInputElement)DOM.all.item("null", 0);
67                System.Threading.Thread.Sleep(1000);
68                btnLogin.click();
69            }
70            catch (Exception ex)
71            {
72
73            }
74        }
75    }
76}


5. 运行工程,点击上面的Button,OK!

需要说明两点:
1. 关于两个引用
   Microsoft.mshtml:这个引用可以从add reference->.NET中得到。
   SHDocVw.dll:这个引用在windows/system32目录下。
2.对上述dll进行引用后,即可实例化IE模型,通过构建页面元素进行操作。

End of《自动登录网站和email的程序》

[] [返回上一页] [打 印] [收 藏]
 
∷相关“自动登录网站和email的程序”文章评论∷
(评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 网站目录 鄂ICP备06007162
开源盛世 版权所有Copyright © 2003-2005 VSCodes.Com. All Rights Reserved.