
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)
1.記 憶 體 配 置 、 釋 放 和 轉 移
void *kmalloc(unsigned int size, int priority)
void *kfree_s(void * obj, int size)
kfree(char *)
memcpy_fromfs(dest, src, size)
memcpy_tofs(dest, src, size)
put_fs_byte(src, dest)
byte get_fs_byte(char *s)
pcman 發表在 痞客邦 留言(0) 人氣(1,245)
Block Device Driver是 以 固 定 大 小 長 度 來 傳 送 轉 移 資 料
Character Device Driver是 以 不 定 長 度 的 字 元 傳 送 資 料 。
pcman 發表在 痞客邦 留言(0) 人氣(2,287)
pcman 發表在 痞客邦 留言(0) 人氣(173)
方法一:用 module 的方式撰寫 driver
將 driver 製作成 module 的好處是可以在不重開機的狀下,重複載入更新過的 driver ,在開發 driver 時是一個方便的許選擇。
在編譯 module 前,要先更新 ubuntu 的套件。請在終端機下鍵入:
sudo apt-get install module-assistant
1.調整 kernel 為接受 module
因為原本預設的 kernel config 並不接受動態掛載 module ,我們要新增對 loadable module 的支援。
首先找到在實驗二中所使用的 linux kernel 原始碼,接著在的 menuconfig 中 [3]
,找到 「 Loadable Module Support 」,並將它裡面的 「 Enable loadable module support
」、「 Module unloading 」以及「 Forced module unloading 」勾選為 built-in
,再存檔離開即可。
接著請重新編譯 kernel ,即可產生支援動態掛載 module 的 kernel image 了。
[3] |
編譯 linux kernel 的相關步驟可參考實驗二 |
2 編譯 driver module
編譯 module 的方法和一般的程式有一些不同,請先到 opencsl 網站下載 Makefile :
wget http://opencsl.openfoundry.org/src/Makefile
將它和 demo.c 放到同一層目錄,並在上面鍵入 [4]
make -C <linux> M=$(pwd) modules ARCH=arm CROSS_COMPILE=arm-linux-uclibc-
即可產生 demo.ko ,這是我們之後要拿來掛載的 module 。
[4] |
<linux> 為 linux source 的根目錄路徑 |
3 掛載 driver
在 linux 中和 module 有關的指令有三:
- insmod : 掛載 module
- lsmod : 檢查目前 module 的狀態
- rmmod :卸載 module
在用 QEMU 載入新 kernel image 後,可以在 demo.ko 的目錄下鍵入
insmod demo.ko
即可將 demo 載入 kernel 中。此時可用 lsmod 來確定 demo 是否有被成功掛載。
在掛載 demo 的同時,我們也可以發現 linux 有印出我們在 demo.c 裡定義的訊息。
若要將 demo 移除或重新掛載,可以鍵入
rmmod demo.ko
就可以將 demo 移除。
pcman 發表在 痞客邦 留言(0) 人氣(16,814)
引入標頭檔
在撰寫 driver 前必須先 include 一些標頭檔:
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
撰寫控制元件的函式
struct file_operations 即為定義各個 function pointer 的 structure。
static ssize_t drv_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
{
printk("device read\n");
return count;
}
static ssize_t drv_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
{
printk("device write\n");
return count;
}
static int drv_open(struct inode *inode, struct file *filp)
{
printk("device open\n");
return 0;
}
int drv_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
printk("device ioctl\n");
return 0;
}
static int drv_release(struct inode *inode, struct file *filp)
{
printk("device close\n");
return 0;
}
struct file_operations 即為定義各個 function pointer 的 structure。
struct file_operations drv_fops =
{
read: drv_read,
write: drv_write,
ioctl: drv_ioctl,
open: drv_open,
release: drv_release,
};
撰寫初始化、結束元件的函式
#define MAJOR_NUM 60
#define MODULE_NAME "DEMO"
static int demo_init(void) {
if (register_chrdev(MAJOR_NUM, "demo", &drv_fops) < 0)
{
printk("<1>%s: can't get major %d\n", MODULE_NAME, MAJOR_NUM);
return (-EBUSY);
}
printk("<1>%s: started\n", MODULE_NAME);
return 0;
}
static void demo_exit(void) {
unregister_chrdev(MAJOR_NUM, "demo");
printk("<1>%s: removed\n", MODULE_NAME);
}
module_init(demo_init);
module_exit(demo_exit);
其中 MAJOR_NUM 即為 driver 所對應的 device 的 major number
pcman 發表在 痞客邦 留言(0) 人氣(1,043)