
Public pURL As String
Public FindLinks As Boolean
Public rRow As Integer
Sub FindURL(sURL As String)
Dim IE As New InternetExplorer
Dim oDoc As New MSHTML.HTMLDocument
Dim oLink As HTMLAnchorElement
Dim i As Integer
FindLinks = False
IE.navigate sURL
Do While IE.ReadyState <> READYSTATE_COMPLETE
UrlForm.Label1.Caption = "網頁:" & sURL & " 連線中請稍候......"
DoEvents
Loop
UrlForm.Label1.Caption = ""
'引用 Document 對象
Set oDoc = IE.Document
'列出網頁中的新聞資料
Call ListTableinnertext(oDoc)
'獲取以順序排列的HTML 標籤中所有 Links 對象的集合。
For i = 0 To oDoc.Links.Length - 1
On Error Resume Next
'尋找 Links 對象中innerText為"下頁"的標籤
'因為網站為簡體,而我是使用繁體,所以才會使用 Like "下*"的方法
'如果是簡體的環境可以直接改成 oDoc.Links(i).innerText="下頁"
If Len(oDoc.Links(i).innerText) = 2 And oDoc.Links(i).innerText Like "下*" Then
'href:獲取目標URL(完整網址)
UrlForm.ListBox1.AddItem oDoc.Links(i).href
Set oLink = oDoc.Links(i)
If Not oLink Is Nothing Then
pURL = oLink.href
UrlForm.WebBrowser1.navigate pURL
FindLinks = True
End If
End If
Next i
If FindLinks = False Then pURL = ""
Set oDoc = Nothing
Set IE = Nothing
End Sub
Sub ListTableinnertext(oDoc)
Dim DocElemsCnt As Integer
Dim Tbl As Object
For DocElemsCnt = 0 To oDoc.all.Length - 1
'tagName:獲取對象的標籤名稱。
If oDoc.all.Item(DocElemsCnt).tagName = "TABLE" Then
Set Tbl = oDoc.all.Item(DocElemsCnt)
'每個網頁有很多TABLE(表格),而要取得資料的TABLE(表格)共有40列, _
可以利用此特性來取得正確的TABLE(表格)
If Tbl.Rows.Length > 20 Then
'Tbl.Rows.Length:取得TABLE(表格)的列數
For RwLen = 0 To Tbl.Rows.Length - 1
rRow = rRow + 1
'Int(Tbl.Cells.Length / Tbl.Rows.Length):可取得TABLE(表格)共有幾欄
For Colen = 0 To Int(Tbl.Cells.Length / Tbl.Rows.Length) - 1
Cells(rRow, Colen + 1).Value = Tbl.Rows(RwLen).Cells(Colen).innerText
Next Colen
Next RwLen
End If
End If
Next DocElemsCnt
Application.Goto Cells(rRow, 1), Scroll:=True
End Sub
Sub FormShow()
UrlForm.Show 0
End Sub
pcman 發表在 痞客邦 留言(0) 人氣(4,892)
使用WebBrowser 控件,可以通過 ObjectForScripting 和Document
屬性在客戶端應用程序代碼和網頁腳本代碼之間實現雙向通信
'宣告
Public Property ObjectForScripting As Object
'用途
Dim instance As WebBrowser
Dim value As Object
value = instance.ObjectForScripting
instance.ObjectForScripting = value
pcman 發表在 痞客邦 留言(0) 人氣(588)
一、建立網頁
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<script language="javascript" type="text/javascript">
<!-- 提供給C#程序調用的方法 -->
function messageBox(message)

{
alert(message);
}
</script>
</head>
<body>
<!-- 調用C#方法 -->
<button onclick="window.external.MyMessageBox('javascript訪問C#代碼')" >
javascript訪問C#代碼</button>
</body>
</html>
pcman 發表在 痞客邦 留言(0) 人氣(1,138)

WebBrowser 控件可以讓你裝載Windows Form 應用程序中的 Web 網頁和其它採用瀏覽器的文件。可以使用webbrowser 控件將現有的web框架控制項加入至 Windows Form 客戶端應用程序。
還是直接看代碼吧。
WebBrowser 控制項 提供的屬性、方法和事件,可用來實現 Internet Explorer 的控制項
webBrowser1.Navigate("www.cnblogs.com"); //將指定位置處的文件載入至 WebBrowser
webBrowser1.GoBack();//上一頁
webBrowser1.GoForward();//下一頁
webBrowser1.Refresh();//刷新
webBrowser1.GoHome();//主頁
這裡提供了WebBrowser常用的方法,
上面的代碼是將 我們園子的主頁載入到WebBrowser控件中。如果我們想要在應用程式中產生自己的網頁內容,可以設定DocumentText屬性。也可以通過Document屬性來處理目前的網頁內容。如下代碼是使用DocumentText 屬性,顯示網頁內容。並用Document屬性來處理所顯示的網頁。
private void btnDocumentText_Click(object sender, EventArgs e)
{
string szHtml = @"
<HTML>
<HEAD>
<TITLE> DocumentText </TITLE>
</HEAD>
<BODY>
Please enter your name:<br/>
<input type='text' name='Name'/><br/>
<a href='http://www.microsoft.com' >Send input to method of Form class</a>
</BODY>
</HTML>";
webBrowser1.DocumentText = szHtml;
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;
if (document != null && document.All["Name"] != null && String.IsNullOrEmpty(document.All["Name"].GetAttribute("value")))
{
e.Cancel = true;
System.Windows.Forms.MessageBox.Show("You must enter your name before you can navigate to " + e.Url.ToString());
}
}
既然我們可以通過DocumentText生成自己的網頁,那麼能不能像使用IE那樣操作這個網頁呢?,答案是肯定的,完全可以像操作Web程序那樣操作WebBrowser 控制項。比如我們可以加入腳本,CSS。當然,如果你熟悉 HTML 物件對像模型 (DOM),也可以透過 Document 屬性來處理目前的Web網頁內容。下面的例子加入了JavaScript腳本來控制網頁。如果要在Winfrom程序中寫大量的Javascriot代碼,而且這些代碼最終要轉換成String型載入到Webbrowser 那將是很痛苦的事情,不過沒有關係,我們可以創建一個js文件,放入資源中,用的時候只需從資源中載入即可。這裡我創建一個名為 ClientScript.js 的文件
<script language = "javascript">
function ClickEvent(name)
{
alert("Hello: " +name);
}
function KeyDown()
{
if (event.keyCode==116)
{
event.keyCode=0;
event.returnValue=false;
}
return false;
}
string szClientScript = ManagedWebBrowser.Properties.Resources.ResourceManager.GetString("ClientScript");
string szWebBrowserText = "<html>" +
"<head>" +
"<title></title>" +
szClientScript +
"</head>" +
"<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">" +
"Please enter your name:<br/>" +
"<input type='text' name='Name'/><br/>" +
"<font onclick = 'ClickEvent(Name.value)'>Click Here</font>" +
"</body></html>";
webBrowser1.DocumentText = szWebBrowserText;
WebBrowser 是 System.Windows.Forms 下的控制項,也就是意味著它是用在WimForm程序下,那麼WebWrower所載入的Web頁面如何實現在WinForm程序下處理呢。例如上例中的 "Click Here" 。這裡的Click事件是通過腳本處理的,如何讓這個Click事件在Winform中處理呢?這裡要做一些修改。若要從指令碼存取用戶端應用程式,需要設定ObjectForScripting屬性。指令碼可以將您指定的物件當做window.external 物件來存取。
使用ObjectForScripting屬性,可啟用 WebBrowser 控制項所裝載之 Web 網頁與包含 WebBrowser 控制項之應用程式間的通訊。
這個屬性可讓您整合動態超文字標記語言 (DHTML) 程式碼與用戶端應用程式程式碼。
指定給這個屬性的物件可讓 Web 網頁指令碼做為 window.external 物件,這個物件是為了存取主應用程式而提供的內建 DOM 物件。
private void btnScriptEvent_Click(object sender, EventArgs e)
{
// This is the handler for loading the script into the Web Browser control and allowing us to interact
// between the script in the Browser control and this form class
// Set the ObjectForScripting property of the Web Browser control to point to this form class
// This will allow us to interact with methods in this form class via the window.external property
webBrowser1.ObjectForScripting = this;
string szWebBrowserText = "<html>" +
"<head>" +
"<title></title>" +
"</head>" +
"<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">" +
"Please enter your name:<br/>" +
"<input type='text' name='Name'/><br/>" +
"<font onClick='window.external.ClickEvent(Name.value)'>Click Here</font>" +
"</body></html>";
webBrowser1.DocumentText = szWebBrowserText;
}
public void ClickEvent(string userName)
{
// Simply echo out the name that the user typed in the input box of the HTML page
if (System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft == true)
MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
else
MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
這裡的ObjectForScripting 屬性設置為 this。注意:在From1 類的開頭加入了這麼一句[ComVisible(true)], 它在System.Runtime.InteropServices下,預設值為 true,指出 Managed 型別對於 COM 為可見的。
[ComVisible(true)]
public partial class Form1 : System.Windows.Forms.Form
結束語:
本文簡單的介紹了 WebBrowser 的用法,實現了WinForm程序下與Web頁面的交互使用。本人在院子裡一直以來都是看客,這是第一次在園子裡留下技術足跡。有不妥之處,還望各位朋友指正。
感謝您能看到這裡。
綠色通道:好文要頂關注我收藏該文與我聯繫
pcman 發表在 痞客邦 留言(0) 人氣(3,791)
問:
請問一下 我用 WebBrowser 的控制項設計一Browser,我想知道如何判斷該網頁己被瀏覽器下載完成?
答:
當網頁下載完成時,便會觸發webbrowser1_navigatecomplete2的事件!!!
pcman 發表在 痞客邦 留言(0) 人氣(390)
在form1內有一個PictureBox1工具上巳顯示圖片
加了一個Button1工具去連結到一個新的form2裡PictureBox2做圖片放大功能
其中要在另一個form顯示圖片要如何實現?
pcman 發表在 痞客邦 留言(0) 人氣(587)
1.System.Threading.Thread.CurrentThread.Sleep(1000)
2.Sub delay()
Dim Start As Integer= Environment.TickCount()
Dim
TimeLast As Integer = 7000 ' 要延遲 t 秒,就設為 t *1000
Do
If
Environment.TickCount() - Start> TimeLast Then Exit Do
Application.DoEvents() ' 要記得寫這行,不然都在跑迴圈,畫面可能會不見
Loop
End
Sub
pcman 發表在 痞客邦 留言(0) 人氣(4,275)
Imports
System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential)>
_
Public Structure Struct_INTERNET_PROXY_INFO
Public dwAccessType
As Integer
Public proxy As IntPtr
Public proxyBypass As
IntPtr
End Structure
<DllImport("wininet.dll",
SetLastError:=True)> _
Private Shared Function InternetSetOption(
_
ByVal hInternet As IntPtr, _
ByVal dwOption As Integer,
_
ByVal lpBuffer As IntPtr, _
ByVal lpdwBufferLength As Integer)
_
As Boolean
End Function
Private Function
RefreshIESettings(ByVal strProxy As String) As Boolean
Dim
INTERNET_OPTION_PROXY As Integer = 38
Dim INTERNET_OPEN_TYPE_PROXY As
Integer = 3
Dim struct_IPI As Struct_INTERNET_PROXY_INFO
'Filling in structure
struct_IPI.dwAccessType =
INTERNET_OPEN_TYPE_PROXY
struct_IPI.proxy =
Marshal.StringToHGlobalAnsi(strProxy)
struct_IPI.proxyBypass =
Marshal.StringToHGlobalAnsi("local")
'Allocating memory
Dim
intptrStruct As IntPtr =
Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI))
'Converting
structure to IntPtr
Marshal.StructureToPtr(struct_IPI, intptrStruct,
True)
Return InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY,
intptrStruct, Marshal.SizeOf(struct_IPI))
End Function
pcman 發表在 痞客邦 留言(0) 人氣(219)
在VB的檔案存取方式裡,最基本的觀念就是「開檔」→「處理檔」→「關檔」。
如果要將資料存入檔案裡,操作順序為「開檔」→「寫檔」→「關檔」,如此能將資料寫入檔案裡。
如果要將資料從檔案讀出,操作順序為「開檔」→「讀檔」→「關檔」,如此取得檔案裡的資料。
存入檔案
pcman 發表在 痞客邦 留言(0) 人氣(4,441)
Private Sub TextBox1_KeyDown(ByVal
sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles
TextBox1.KeyDown
If e.KeyCode = Keys.Return Then
MessageBox.Show(TextBox1.Text)
End If
End Sub
pcman 發表在 痞客邦 留言(0) 人氣(1,812)