不知你说提交表单是什么意思,可以通过重载webbrowser control的IDocHostUIHandler接口来接收用户在html页面上的操作,给你点源代码,
c#的 凑合着看吧namespace WindowsApplication5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
[Serializable]
public class Form1 : System.Windows.Forms.Form ,IDocHostUIHandler
{
private AxSHDocVw.AxWebBrowser WebBrowser;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
this.WebBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.WebBrowser_DocumentComplete);
object flags = 0;
object targetFrame = String.Empty;
object postData = String.Empty;
object headers = String.Empty;
this.WebBrowser.Navigate("about:blank", ref flags, ref targetFrame, ref postData, ref headers);
ICustomDoc cDoc = (ICustomDoc)this.WebBrowser.Document;
cDoc.SetUIHandler((IDocHostUIHandler)this);
this.WebBrowser.Navigate(@"XXXX.htm", ref flags, ref targetFrame, ref postData, ref headers);
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.WebBrowser = new AxSHDocVw.AxWebBrowser();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.WebBrowser)).BeginInit();
this.SuspendLayout();
//
// WebBrowser
//
this.WebBrowser.Enabled = true;
this.WebBrowser.Location = new System.Drawing.Point(72, 32);
this.WebBrowser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("WebBrowser.OcxState")));
this.WebBrowser.Size = new System.Drawing.Size(300, 150);
this.WebBrowser.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(232, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 197);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.WebBrowser});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.WebBrowser)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void WebBrowser_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
IHTMLDocument2 doc = (IHTMLDocument2)this.WebBrowser.Document;
HTMLButtonElement button = (HTMLButtonElement)doc.all.item("theButton", null);
HTMLButtonElement button1 = (HTMLButtonElement)doc.all.item("theButton1", null);
HTMLButtonElement button2 = (HTMLButtonElement)doc.all.item("theButton2", null);
((HTMLButtonElementEvents2_Event)button).onclick += new HTMLButtonElementEvents2_onclickEventHandler(this.Button_onclick);
((HTMLButtonElementEvents2_Event)button1).onclick += new HTMLButtonElementEvents2_onclickEventHandler(this.Button1_onclick);
((HTMLButtonElementEvents2_Event)button2).onclick += new HTMLButtonElementEvents2_onclickEventHandler(this.Button2_onclick);
}
private bool Button_onclick(IHTMLEventObj e)
{
MessageBox.Show("Alert from the app: Received theButton.onclick!");
return true;
}
private bool Button1_onclick(IHTMLEventObj e)
{
MessageBox.Show("Alert from the app: Received theButton1.onclick!");
return true;
}
private bool Button2_onclick(IHTMLEventObj e)
{
MessageBox.Show("Alert from the app: Received theButton2.onclick!");
return true;
}
// IDocHostUIHandler implementation
void IDocHostUIHandler.EnableModeless(int fEnable)
{
}
void IDocHostUIHandler.FilterDataObject(MsHtmHstInterop.IDataObject pDO, out MsHtmHstInterop.IDataObject ppDORet)
{
ppDORet = null;
}
void IDocHostUIHandler.GetDropTarget(IDropTarget pDropTarget, out IDropTarget ppDropTarget)
{
ppDropTarget = null;
}
void IDocHostUIHandler.GetExternal(out object ppDispatch)
{
ppDispatch = null;
}
void IDocHostUIHandler.GetHostInfo(ref _DOCHOSTUIINFO pInfo)
{
}
void IDocHostUIHandler.GetOptionKeyPath(out string pchKey, uint dw)
{
pchKey = null;
}
void IDocHostUIHandler.HideUI()
{
}
void IDocHostUIHandler.OnDocWindowActivate(int fActivate)
{
}
void IDocHostUIHandler.OnFrameWindowActivate(int fActivate)
{
}
void IDocHostUIHandler.ResizeBorder(ref MsHtmHstInterop.tagRECT prcBorder, IOleInPlaceUIWindow pUIWindow, int fRameWindow)
{
}
void IDocHostUIHandler.ShowContextMenu(uint dwID, ref MsHtmHstInterop.tagPOINT ppt, object pcmdtReserved, object pdispReserved)
{
}
void IDocHostUIHandler.ShowUI(uint dwID, IOleInPlaceActiveObject pActiveObject, IOleCommandTarget pCommandTarget, IOleInPlaceFrame pFrame, IOleInPlaceUIWindow pDoc)
{
}
void IDocHostUIHandler.TranslateAccelerator(ref tagMSG lpmsg, ref Guid pguidCmdGroup, uint nCmdID)
{
}
void IDocHostUIHandler.TranslateUrl(uint dwTranslate, ref ushort pchURLIn, IntPtr ppchURLOut)
{
}
void IDocHostUIHandler.UpdateUI()
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
} private void button1_Click(object sender, System.EventArgs e)
{
}
}
}
c#的 凑合着看吧namespace WindowsApplication5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
[Serializable]
public class Form1 : System.Windows.Forms.Form ,IDocHostUIHandler
{
private AxSHDocVw.AxWebBrowser WebBrowser;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
this.WebBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.WebBrowser_DocumentComplete);
object flags = 0;
object targetFrame = String.Empty;
object postData = String.Empty;
object headers = String.Empty;
this.WebBrowser.Navigate("about:blank", ref flags, ref targetFrame, ref postData, ref headers);
ICustomDoc cDoc = (ICustomDoc)this.WebBrowser.Document;
cDoc.SetUIHandler((IDocHostUIHandler)this);
this.WebBrowser.Navigate(@"XXXX.htm", ref flags, ref targetFrame, ref postData, ref headers);
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.WebBrowser = new AxSHDocVw.AxWebBrowser();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.WebBrowser)).BeginInit();
this.SuspendLayout();
//
// WebBrowser
//
this.WebBrowser.Enabled = true;
this.WebBrowser.Location = new System.Drawing.Point(72, 32);
this.WebBrowser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("WebBrowser.OcxState")));
this.WebBrowser.Size = new System.Drawing.Size(300, 150);
this.WebBrowser.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(232, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 197);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.WebBrowser});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.WebBrowser)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void WebBrowser_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
IHTMLDocument2 doc = (IHTMLDocument2)this.WebBrowser.Document;
HTMLButtonElement button = (HTMLButtonElement)doc.all.item("theButton", null);
HTMLButtonElement button1 = (HTMLButtonElement)doc.all.item("theButton1", null);
HTMLButtonElement button2 = (HTMLButtonElement)doc.all.item("theButton2", null);
((HTMLButtonElementEvents2_Event)button).onclick += new HTMLButtonElementEvents2_onclickEventHandler(this.Button_onclick);
((HTMLButtonElementEvents2_Event)button1).onclick += new HTMLButtonElementEvents2_onclickEventHandler(this.Button1_onclick);
((HTMLButtonElementEvents2_Event)button2).onclick += new HTMLButtonElementEvents2_onclickEventHandler(this.Button2_onclick);
}
private bool Button_onclick(IHTMLEventObj e)
{
MessageBox.Show("Alert from the app: Received theButton.onclick!");
return true;
}
private bool Button1_onclick(IHTMLEventObj e)
{
MessageBox.Show("Alert from the app: Received theButton1.onclick!");
return true;
}
private bool Button2_onclick(IHTMLEventObj e)
{
MessageBox.Show("Alert from the app: Received theButton2.onclick!");
return true;
}
// IDocHostUIHandler implementation
void IDocHostUIHandler.EnableModeless(int fEnable)
{
}
void IDocHostUIHandler.FilterDataObject(MsHtmHstInterop.IDataObject pDO, out MsHtmHstInterop.IDataObject ppDORet)
{
ppDORet = null;
}
void IDocHostUIHandler.GetDropTarget(IDropTarget pDropTarget, out IDropTarget ppDropTarget)
{
ppDropTarget = null;
}
void IDocHostUIHandler.GetExternal(out object ppDispatch)
{
ppDispatch = null;
}
void IDocHostUIHandler.GetHostInfo(ref _DOCHOSTUIINFO pInfo)
{
}
void IDocHostUIHandler.GetOptionKeyPath(out string pchKey, uint dw)
{
pchKey = null;
}
void IDocHostUIHandler.HideUI()
{
}
void IDocHostUIHandler.OnDocWindowActivate(int fActivate)
{
}
void IDocHostUIHandler.OnFrameWindowActivate(int fActivate)
{
}
void IDocHostUIHandler.ResizeBorder(ref MsHtmHstInterop.tagRECT prcBorder, IOleInPlaceUIWindow pUIWindow, int fRameWindow)
{
}
void IDocHostUIHandler.ShowContextMenu(uint dwID, ref MsHtmHstInterop.tagPOINT ppt, object pcmdtReserved, object pdispReserved)
{
}
void IDocHostUIHandler.ShowUI(uint dwID, IOleInPlaceActiveObject pActiveObject, IOleCommandTarget pCommandTarget, IOleInPlaceFrame pFrame, IOleInPlaceUIWindow pDoc)
{
}
void IDocHostUIHandler.TranslateAccelerator(ref tagMSG lpmsg, ref Guid pguidCmdGroup, uint nCmdID)
{
}
void IDocHostUIHandler.TranslateUrl(uint dwTranslate, ref ushort pchURLIn, IntPtr ppchURLOut)
{
}
void IDocHostUIHandler.UpdateUI()
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
} private void button1_Click(object sender, System.EventArgs e)
{
}
}
}
例如
server那边的页面如下
<form action="aaa.asp" method=post>
<input type="text" name="input1">
</form>
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
SHDocVw.WebBrowserClass wb = this.WebBrowser.GetOcx() as SHDocVw.WebBrowserClass;
wb.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.WebBrowser_DocumentComplete); object flags = 0;
object targetFrame = String.Empty;
object postData = String.Empty;
object headers = String.Empty;
this.WebBrowser.Navigate("about:blank", ref flags, ref targetFrame, ref postData, ref headers);
ICustomDoc cDoc = (ICustomDoc)this.WebBrowser.Document;
cDoc.SetUIHandler((IDocHostUIHandler)this);
this.WebBrowser.Navigate(@"www.csdn.net", ref flags, ref targetFrame, ref postData, ref headers);
}3,你可以在客户端的button1按钮上的点击事件中加如下代码
private void button1_Click(object sender, System.EventArgs e)
{
IHTMLDocument2 doc = (IHTMLDocument2)this.WebBrowser.Document;
mshtml.HTMLInputTextElement text1 = (HTMLInputTextElement)doc.all.item("name",0);
((mshtml.HTMLInputElementClass)(text1)).value = "username";
mshtml.HTMLInputTextElement text2 = (HTMLInputTextElement)doc.all.item("pass",0);
((mshtml.HTMLInputElementClass)(text2)).value = "password";
mshtml.HTMLFormElement f1 =(mshtml.HTMLFormElement)doc.all .item ("a",0) ;
((HTMLFormElement)(f1)).submit ();
}4,如果你把username,和password两个字符串替换成自己的csdn帐号,当你点击按钮button1时就可以直接登陆到论坛界面了
不好意思,还的请教你怎么引入相关的命名空间啊。
IDocHostUIHandler,IOleCommandTarget等都提示找不道命名空间
我用我的程序登陆csdn.net 时可以添入用户名和密码了但是他的submit
按钮是<input type="image">没name 我该怎么提交呢?
多谢
所以.. mshtml.HTMLFormElement f1 =(mshtml.HTMLFormElement)doc.all .item ("a",0) ;
((HTMLFormElement)(f1)).submit ();