日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# Webbrowser 常用方法及多线程调用

發布時間:2024/9/20 C# 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# Webbrowser 常用方法及多线程调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設置控件的值

///<summary>
/// 根據ID,NAME雙重判斷并設置值
/// </summary>
/// <param name="tagName"></param>
/// <param name="id"></param>
/// <param name="value"></param>
private void SetTxt(string tagName, string name, string id, string value)
{
HtmlDocument docx = <strong><font color="#FF0000">webBrowser</font></strong>1.Document;
foreach (HtmlElement item in docx.GetElementsByTagName(tagName))
{

if (item.GetAttribute("name") != null && item.GetAttribute("name") == name || item.Id == id)
{
try
{
item.Focus();
item.SetAttribute("value", value);
}
catch
{
item.SetAttribute("value", value);
}

}
}
}

返回指定WebBrowser中圖片<IMG></IMG>中的圖內容? ?需要引用MsHtml

///<summary>
/// 返回指定WebBrowser中圖片<IMG></IMG>中的圖內容
/// </summary>
/// <param name="WebCtl">WebBrowser控件</param>
/// <param name="ImgeTag">IMG元素</param>
/// <returns>IMG對象</returns>
private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag)
{
HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument;
HTMLBody body = (HTMLBody)doc.body;
IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //圖片地址
Image oldImage = Clipboard.GetImage();
rang.add(Img);
rang.execCommand("Copy", false, null); //拷貝到內存
Image numImage = Clipboard.GetImage();
try
{
Clipboard.SetImage(oldImage);
}
catch { }
return numImage;
}

一個通用webbrowser類,封裝常用方法

public partial class htmlElement
{
//根據Name獲取元素
public HtmlElement GetElement_Name(WebBrowser wb, string Name)
{
HtmlElement e = wb.Document.All[Name];
return e;
}

//根據Id獲取元素
public HtmlElement GetElement_Id(WebBrowser wb, string id)
{
HtmlElement e = wb.Document.GetElementById(id);
return e;
}

//根據Index獲取元素
public HtmlElement GetElement_Index(WebBrowser wb, int index)
{
HtmlElement e = wb.Document.All[index];
return e;
}

// 據Type獲取元 ,在沒有NAME和ID的情況下使用
public HtmlElement GetElement_Type(WebBrowser wb, string type)
{
HtmlElement e = null;
HtmlElementCollection elements = wb.Document.GetElementsByTagName("input");
foreach (HtmlElement element in elements)
{
if (element.GetAttribute("type") == type)
{
e = element;
}
}
return e;
}
// 據Type獲取元 ,在沒有NAME和ID的情況下使用,并指定是同類type的第 個,GetElement_Type()升級版
public HtmlElement GetElement_Type_No(WebBrowser wb, string type, int i)
{
int j = 1;
HtmlElement e = null;
HtmlElementCollection elements = wb.Document.GetElementsByTagName("input");
foreach (HtmlElement element in elements)
{
if (element.GetAttribute("type") == type)
{
if (j == i)
{
e = element;
}
j++;
}
}
return e;
}
//獲取form表單名name,返回表單
public HtmlElement GetElement_Form(WebBrowser wb, string form_name)
{
HtmlElement e = wb.Document.Forms[form_name];
return e;
}


//設置元素value屬性的值
public void Write_value(HtmlElement e, string value)
{
e.SetAttribute("value", value);
}

//執行元素的方法,如:click,submit(需Form表單名)等
public void Btn_click(HtmlElement e, string s)
{

e.InvokeMember(s);
}
}

多線程執行webbrowser的方法:
webbrowser 只支持STA模式

private void ThreadWebBrowser(string url)
{
Thread tread = new Thread(new ParameterizedThreadStart(BeginCatch));
tread.SetApartmentState(ApartmentState.STA);//注意創建的線程單元
tread.Start(url);
}

private void BeginCatch(object obj)
{
string url = obj.ToString();
WebBrowser wb = new WebBrowser();
wb.ScriptErrorsSuppressed = true;
//在這里Navigate一個空白頁面 必須
wb.Navigate("about:blank");
string htmlcode = "";//這里賦值 HTML字符串數據
wb.Document.Write(htmlcode);
//執行其他操作

總結

以上是生活随笔為你收集整理的C# Webbrowser 常用方法及多线程调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。