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

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

using System;
2

using System.Collections.Generic;
3

using System.ComponentModel;
4

using System.Data;
5

using System.Drawing;
6

using System.Text;
7

using System.Windows.Forms;
8

9

//添加引用
10

using SHDocVw;
11

using mshtml;
12

13

namespace 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

}