1:创建控件:
button1
webBrowser1
textBox1 网址
textBox2 用户名
textBox3 密码
2:
using...
1 using System;
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
2 using System.Windows.Forms;
3 using System.Runtime.InteropServices;
3:完整代码。
1
2 namespace WebTest1
3 {
4 #region COM Interfaces
5
6 [ComImport,
7 Guid("00000112-0000-0000-C000-000000000046"),
8 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
9 public interface IOleObject
10 {
11 void SetClientSite(IOleClientSite pClientSite);
12 void GetClientSite(IOleClientSite ppClientSite);
13 void SetHostNames(object szContainerApp, object szContainerObj);
14 void Close(uint dwSaveOption);
15 void SetMoniker(uint dwWhichMoniker, object pmk);
16 void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
17 void InitFromData(IDataObject pDataObject, bool
18 fCreation, uint dwReserved);
19 void GetClipboardData(uint dwReserved, IDataObject ppDataObject);
20 void DoVerb(uint iVerb, uint lpmsg, object pActiveSite,
21 uint lindex, uint hwndParent, uint lprcPosRect);
22 void EnumVerbs(object ppEnumOleVerb);
23 void Update();
24 void IsUpToDate();
25 void GetUserClassID(uint pClsid);
26 void GetUserType(uint dwFormOfType, uint pszUserType);
27 void SetExtent(uint dwDrawAspect, uint psizel);
28 void GetExtent(uint dwDrawAspect, uint psizel);
29 void Advise(object pAdvSink, uint pdwConnection);
30 void Unadvise(uint dwConnection);
31 void EnumAdvise(object ppenumAdvise);
32 void GetMiscStatus(uint dwAspect, uint pdwStatus);
33 void SetColorScheme(object pLogpal);
34 }
35
36 [ComImport,
37 Guid("00000118-0000-0000-C000-000000000046"),
38 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
39 public interface IOleClientSite
40 {
41 void SaveObject();
42 void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
43 void GetContainer(object ppContainer);
44 void ShowObject();
45 void OnShowWindow(bool fShow);
46 void RequestNewObjectLayout();
47 }
48
49 [ComImport,
50 GuidAttribute("6d5140c1-7436-11ce-8034-00aa006009fa"),
51 InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
52 ComVisible(false)]
53 public interface IServiceProvider
54 {
55 [return: MarshalAs(UnmanagedType.I4)]
56 [PreserveSig]
57 int QueryService(ref Guid guidService, ref Guid riid, out IntPtr
58 ppvObject);
59 }
60
61 [ComImport, GuidAttribute("79EAC9D0-BAF9-11CE-8C82-00AA004BA90B"),
62 InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
63 ComVisible(false)]
64 public interface IAuthenticate
65 {
66 [return: MarshalAs(UnmanagedType.I4)]
67 [PreserveSig]
68 int Authenticate(ref IntPtr phwnd,
69 ref IntPtr pszUsername,
70 ref IntPtr pszPassword
71 );
72 }
73
74 #endregion
75 public partial class Form1 : Form, IOleClientSite, IServiceProvider,IAuthenticate
76 {
77
78 public static Guid IID_IAuthenticate = new Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b");
79 public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011);
80 public const int S_OK = unchecked((int)0x00000000);
81
82 public Form1()
83 {
84
85 InitializeComponent();
86 }
87
88 private void button1_Click(object sender, EventArgs e)
89 {
90 string oURL = this.textBox1.Text;
91 webBrowser1.Navigate(oURL);
92
93
94 }
95
96 private void Form1_Load(object sender, EventArgs e)
97 {
98 string oURL = "about:blank";
99 webBrowser1.Navigate(oURL);
100
101
102
103 object obj = webBrowser1.ActiveXInstance;
104 IOleObject oc = obj as IOleObject;
105 oc.SetClientSite(this as IOleClientSite);
106 }
107 #region IOleClientSite Members
108
109 public void SaveObject()
110 {
111 // TODO: Add Form1.SaveObject implementation
112 }
113
114 public void GetMoniker(uint dwAssign, uint dwWhichMoniker, object
115 ppmk)
116 {
117 // TODO: Add Form1.GetMoniker implementation
118 }
119
120 public void GetContainer(object ppContainer)
121 {
122 ppContainer = this;
123 }
124
125 public void ShowObject()
126 {
127 // TODO: Add Form1.ShowObject implementation
128 }
129
130 public void OnShowWindow(bool fShow)
131 {
132 // TODO: Add Form1.OnShowWindow implementation
133 }
134
135 public void RequestNewObjectLayout()
136 {
137 // TODO: Add Form1.RequestNewObjectLayout implementation
138 }
139
140 #endregion
141
142 #region IServiceProvider Members
143
144 public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
145 {
146 int nRet = guidService.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
147 if (nRet == 0)
148 {
149 nRet = riid.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
150 if (nRet == 0)
151 {
152 ppvObject = Marshal.GetComInterfaceForObject(this,
153 typeof(IAuthenticate));
154 return S_OK;
155 }
156 }
157 ppvObject = new IntPtr();
158 return INET_E_DEFAULT_ACTION;
159 }
160
161 #endregion
162
163 #region IAuthenticate Members
164
165 public int Authenticate(ref IntPtr phwnd, ref IntPtr pszUsername,ref IntPtr pszPassword)
166 {
167 IntPtr sUser = Marshal.StringToCoTaskMemAuto(textBox2.text);
168 IntPtr sPassword = Marshal.StringToCoTaskMemAuto(textBox3.text);
169
170 pszUsername = sUser;
171 pszPassword = sPassword;
172 return S_OK;
173 }
174
175 #endregion
176
177 }
178 }
179
2 namespace WebTest1
3 {
4 #region COM Interfaces
5
6 [ComImport,
7 Guid("00000112-0000-0000-C000-000000000046"),
8 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
9 public interface IOleObject
10 {
11 void SetClientSite(IOleClientSite pClientSite);
12 void GetClientSite(IOleClientSite ppClientSite);
13 void SetHostNames(object szContainerApp, object szContainerObj);
14 void Close(uint dwSaveOption);
15 void SetMoniker(uint dwWhichMoniker, object pmk);
16 void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
17 void InitFromData(IDataObject pDataObject, bool
18 fCreation, uint dwReserved);
19 void GetClipboardData(uint dwReserved, IDataObject ppDataObject);
20 void DoVerb(uint iVerb, uint lpmsg, object pActiveSite,
21 uint lindex, uint hwndParent, uint lprcPosRect);
22 void EnumVerbs(object ppEnumOleVerb);
23 void Update();
24 void IsUpToDate();
25 void GetUserClassID(uint pClsid);
26 void GetUserType(uint dwFormOfType, uint pszUserType);
27 void SetExtent(uint dwDrawAspect, uint psizel);
28 void GetExtent(uint dwDrawAspect, uint psizel);
29 void Advise(object pAdvSink, uint pdwConnection);
30 void Unadvise(uint dwConnection);
31 void EnumAdvise(object ppenumAdvise);
32 void GetMiscStatus(uint dwAspect, uint pdwStatus);
33 void SetColorScheme(object pLogpal);
34 }
35
36 [ComImport,
37 Guid("00000118-0000-0000-C000-000000000046"),
38 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
39 public interface IOleClientSite
40 {
41 void SaveObject();
42 void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
43 void GetContainer(object ppContainer);
44 void ShowObject();
45 void OnShowWindow(bool fShow);
46 void RequestNewObjectLayout();
47 }
48
49 [ComImport,
50 GuidAttribute("6d5140c1-7436-11ce-8034-00aa006009fa"),
51 InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
52 ComVisible(false)]
53 public interface IServiceProvider
54 {
55 [return: MarshalAs(UnmanagedType.I4)]
56 [PreserveSig]
57 int QueryService(ref Guid guidService, ref Guid riid, out IntPtr
58 ppvObject);
59 }
60
61 [ComImport, GuidAttribute("79EAC9D0-BAF9-11CE-8C82-00AA004BA90B"),
62 InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown),
63 ComVisible(false)]
64 public interface IAuthenticate
65 {
66 [return: MarshalAs(UnmanagedType.I4)]
67 [PreserveSig]
68 int Authenticate(ref IntPtr phwnd,
69 ref IntPtr pszUsername,
70 ref IntPtr pszPassword
71 );
72 }
73
74 #endregion
75 public partial class Form1 : Form, IOleClientSite, IServiceProvider,IAuthenticate
76 {
77
78 public static Guid IID_IAuthenticate = new Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b");
79 public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011);
80 public const int S_OK = unchecked((int)0x00000000);
81
82 public Form1()
83 {
84
85 InitializeComponent();
86 }
87
88 private void button1_Click(object sender, EventArgs e)
89 {
90 string oURL = this.textBox1.Text;
91 webBrowser1.Navigate(oURL);
92
93
94 }
95
96 private void Form1_Load(object sender, EventArgs e)
97 {
98 string oURL = "about:blank";
99 webBrowser1.Navigate(oURL);
100
101
102
103 object obj = webBrowser1.ActiveXInstance;
104 IOleObject oc = obj as IOleObject;
105 oc.SetClientSite(this as IOleClientSite);
106 }
107 #region IOleClientSite Members
108
109 public void SaveObject()
110 {
111 // TODO: Add Form1.SaveObject implementation
112 }
113
114 public void GetMoniker(uint dwAssign, uint dwWhichMoniker, object
115 ppmk)
116 {
117 // TODO: Add Form1.GetMoniker implementation
118 }
119
120 public void GetContainer(object ppContainer)
121 {
122 ppContainer = this;
123 }
124
125 public void ShowObject()
126 {
127 // TODO: Add Form1.ShowObject implementation
128 }
129
130 public void OnShowWindow(bool fShow)
131 {
132 // TODO: Add Form1.OnShowWindow implementation
133 }
134
135 public void RequestNewObjectLayout()
136 {
137 // TODO: Add Form1.RequestNewObjectLayout implementation
138 }
139
140 #endregion
141
142 #region IServiceProvider Members
143
144 public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
145 {
146 int nRet = guidService.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
147 if (nRet == 0)
148 {
149 nRet = riid.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
150 if (nRet == 0)
151 {
152 ppvObject = Marshal.GetComInterfaceForObject(this,
153 typeof(IAuthenticate));
154 return S_OK;
155 }
156 }
157 ppvObject = new IntPtr();
158 return INET_E_DEFAULT_ACTION;
159 }
160
161 #endregion
162
163 #region IAuthenticate Members
164
165 public int Authenticate(ref IntPtr phwnd, ref IntPtr pszUsername,ref IntPtr pszPassword)
166 {
167 IntPtr sUser = Marshal.StringToCoTaskMemAuto(textBox2.text);
168 IntPtr sPassword = Marshal.StringToCoTaskMemAuto(textBox3.text);
169
170 pszUsername = sUser;
171 pszPassword = sPassword;
172 return S_OK;
173 }
174
175 #endregion
176
177 }
178 }
179