用VB制作IE工(gōng)具條自定義按鈕 二

發布時間:2007年06月04日      浏覽次數:3104 次
用VB制作IE工(gōng)具條自定義按鈕

來源:
閱讀:574 次
日期:2004-5-14 23:35:48
西安市 張 慶 現在,有許多軟件安裝後會在 IE 浏覽器的工(gōng)具條上添加一(yī)個自定義按鈕,爲用戶運行程序提供了方便,如“網絡螞蟻”、“金山詞霸2000”等等。實際上添加這個自定義按鈕并不難,隻要找到了它在注冊表中(zhōng)的位置和有關參數,通過手工(gōng)或程序都可以輕易完成。而最方便的辦法就是調用現成的 ActiveX 控件來完成這個功能,但 Windows 似乎沒有提供這種控件。下(xià)面介紹如何開(kāi)發和使用這種自定義控件。

實現方法

要添加這種自定義按鈕,需要在 Windows 注冊表的 HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Extensions 子鍵下(xià)建立一(yī)個 GUID 項,然後在注冊表的右欄建立相應的參數并賦值。這些參數及其含義如下(xià):

ButtonText:自定義按鈕上顯示的文本字符串;

Clsid:IE 工(gōng)具條的類标識碼,爲“{1FBA04EE

-3024-11D2-8F1F0000F87ABD16}";

Default Visible:自定義按鈕是否可見,一(yī)般爲 “Yes";

Exec:自定義按鈕執行的目标,爲可執行文件或超文本鏈接等;

HotIcon:鼠标移上按鈕時顯示的圖标,一(yī)般取自 EXE 文件或 DLL 文件;

Icon:按鈕正常顯示的圖标,一(yī)般取自 EXE 文件或 DLL 文件;

MenuText:在 IE 的“工(gōng)具”菜單中(zhōng)顯示的菜單項;

MenuStatusBar:IE的“工(gōng)具”菜單項的注釋,在狀态條上顯示。

制作過程

在 VB 5.0/6.0 下(xià)建立新的“ActiveX 控件”工(gōng)程。

首先,給控件 AddButton 的 Picture 屬性添加一(yī)個微型 Icon 圖片,這樣當使用該控件時,會在控件工(gōng)具箱上顯示這個小(xiǎo)圖标。然後,再在設計窗口上放(fàng)置一(yī)個 image 控件,給它也設置一(yī)個小(xiǎo)圖片,使用該控件時,會在其 Form 上顯示該控件的外(wài)觀。最後,調出代碼編輯窗口,在該窗口中(zhōng)鍵入以下(xià)代碼,其中(zhōng) API 函數調用的代碼可以直接從“API 文本浏覽器”中(zhōng)獲得。

Option Explicit

……'Default Property Values

……'Property Variables

'定義常量

Const HKEY_LOCAL_MACHINE = &H80000002

Const REG_SZ = 1

'聲明存取注冊表的 API 函數

Private Declare Function RegCloseKey Lib “advapi32.dll" (ByVal hKey As Long) As Long

Private Declare Function RegCreateKey Lib “advapi32.dll" Alias “RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegDeleteValue Lib “advapi32.dll" Alias “RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long

Private Declare Function RegOpenKey Lib “advapi32.dll" Alias “RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegSetValueEx Lib “advapi32.dll" Alias “RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String,ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

'定義注冊表中(zhōng)的主鍵、子鍵

Const hKey = HKEY_LOCAL_MACHINE

Const subKey0=“Software\Microsoft\Internet Explorer\Extensions\"

’把字符串值存入注冊表

Private Sub SaveString(hKey As Long, strPath As String, strValue As String, strdata As String)

Dim keyhand As Long

Dim r As Long

r=RegCreateKey(hKey, strPath, keyhand)

r=RegSetValueEx(keyhand, strValue, 0,REG_SZ, ByVal strdata, Len(strdata))

r=RegCloseKey(keyhand)

End Sub

’從注冊表中(zhōng)删除字符串值

Private Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)

Dim r, keyhand As Long

r = RegOpenKey(hKey, strPath, keyhand)

r = RegDeleteValue(keyhand, strValue)

r = RegCloseKey(keyhand)

End Function

'把設置寫入注冊表,定義按鈕

Public Sub AddBtn2IEtoolbar()

Dim subKey As String

subKey = subKey0 & Trim(GUID) & “\"

Call SaveString(hKey, subKey, “ButtonText", ButtonText)

Call SaveString(hKey, subKey,“Clsid",

“{1FBA04EE-3024-11D2-8F1F0000F87ABD16}")

Call SaveString(hKey, subKey, “Default Visible", “Yes")

Call SaveString(hKey, subKey, “Exec", Exec)

Call SaveString(hKey, subKey, “HotIcon", HotIcon)

Call SaveString(hKey, subKey, “Icon", Icon)

Call SaveString(hKey, subKey, “MenuStatusBar",

MenuStatusBar)

Call SaveString(hKey,subKey,“MenuText", MenuText)End Sub

'從注冊表中(zhōng)删除自定義按鈕

Public Sub DelBtnFromIEtoolbar()

Dim subKey As String

subKey = subKey0 & Trim(GUID) & “\"

Call DeleteValue(hKey, subKey, “ButtonText")

Call DeleteValue(hKey, subKey, “Clsid")

Call DeleteValue(hKey, subKey, “Default Visible")

Call DeleteValue(hKey, subKey, “Exec")

Call DeleteValue(hKey, subKey, “HotIcon")

Call DeleteValue(hKey, subKey, “Icon")

Call DeleteValue(hKey, subKey, “MenuStatusBar")

Call DeleteValue(hKey, subKey, “MenuText")

End Sub

'初始化控件屬性

Private Sub UserControl_InitProperties()

m_ButtonText = m_def_ButtonText

m_Exec = m_def_Exec

m_HotIcon = m_def_HotIcon

m_Icon = m_def_Icon

m_MenuText = m_def_MenuText

m_MenuStatusBar = m_def_MenuStatusBar

m_GUID = m_def_GUID

End Sub

'從存儲器中(zhōng)加載屬性值

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

m_ButtonText = PropBag.ReadProperty

(“ButtonText", m_def_ButtonText)

m_Exec = PropBag.ReadProperty(“Exec",

m_def_Exec)

m_HotIcon = PropBag.ReadProperty(“HotIcon",

m_def_HotIcon)

m_Icon = PropBag.ReadProperty(“Icon",

m_def_Icon)

m_MenuText = PropBag.ReadProperty

(“MenuText",m_def_MenuText)

m_MenuStatusBar =PropBag.ReadProperty

(“MenuStatusBar", m_def_MenuStatusBar)

m_GUID =PropBag.ReadProperty(“GUID",

m_def_GUID)

End Sub

Private Sub UserControl_Resize()

UserControl.Width = 540

UserControl.Height = 540

Image1.Move 0, 0, ScaleWidth, ScaleHeight

End Sub

'将屬性值寫到存儲器

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

Call PropBag.WriteProperty(“ButtonText",

m_ButtonText, m_def_ButtonText)

Call PropBag.WriteProperty(“Exec", m_Exec,

m_def_Exec)

Call PropBag.WriteProperty(“HotIcon",

m_HotIcon, m_def_HotIcon)

Call PropBag.WriteProperty(“Icon",

m_Icon, m_def_Icon)

Call PropBag.WriteProperty(“MenuText",

m_MenuText, m_def_MenuText)

Call PropBag.WriteProperty(“MenuStatusBar",

m_MenuStatusBar, m_def_MenuStatusBar)

Call PropBag.WriteProperty(“GUID",m_GUID,

m_def_GUID)

End Sub

’獲取按鈕文本

Public Property Get ButtonText() As String

ButtonText = m_ButtonText

End Property

’設置按鈕文本

Public Property Let ButtonText(ByVal New_ButtonText As String)

m_ButtonText = New_ButtonText

PropertyChanged “ButtonText"

End Property

’獲取當前按鈕執行目标

Public Property Get Exec() As String

Exec = m_Exec

End Property

’設置當前按鈕執行目标

Public Property Let Exec(ByVal New_Exec As String)

m_Exec = New_Exec

PropertyChanged “Exec"

End Property

’獲取鼠标移上按鈕時顯示的圖标

Public Property Get HotIcon() As String

HotIcon = m_HotIcon

End Property

’設置鼠标移上按鈕時顯示的圖标

Public Property Let HotIcon(ByVal New_HotIcon As String)

m_HotIcon = New_HotIcon

PropertyChanged “HotIcon"

End Property

’獲取按鈕正常顯示的圖标

Public Property Get Icon() As String

Icon = m_Icon

End Property

’設置按鈕正常顯示的圖标

Public Property Let Icon(ByVal New_Icon As String)

m_Icon = New_Icon

PropertyChanged “Icon"

End Property

’獲取菜單項顯示文本

Public Property Get MenuText() As String

MenuText = m_MenuText

End Property

’設置菜單項顯示文本

Public Property Let MenuText(ByVal New_MenuText As String)

m_MenuText = New_MenuText

PropertyChanged “MenuText"

End Property

’獲取菜單項注釋文本

Public Property Get MenuStatusBar() As String

MenuStatusBar = m_MenuStatusBar

End Property

’設置菜單項注釋文本

Public Property Let MenuStatusBar(ByValNew_Menu

StatusBar As String)

m_MenuStatusBar = New_MenuStatusBar

PropertyChanged “MenuStatusBar"

End Property

Public Property Get GUID() As String

GUID = m_GUID

End Property

Public Property Let GUID(ByVal New_GUID As String)

m_GUID = New_GUID

PropertyChanged “GUID"

End Property

’顯示“關于”對話(huà)框

Public Sub ShowAboutBox()

frmAbout.Show vbModal

Unload frmAbout

Set frmAbout = Nothing

End Sub

該控件提供了 7 個屬性和2 個方法。

控件的屬性:

GUID:一(yī)個在注冊表唯一(yī)标識應用軟件的數字序列,共36位;

ButtonText:按鈕上顯示的文本;

Exec:按鈕執行的目标,爲可執行文件或超文本鏈接等;

HotIcon:鼠标移上按鈕時顯示的圖标,取自 EXE 文件或 DLL 文件;

Icon:按鈕正常顯示的圖标,取自 EXE 文件或 DLL 文件;

MenuText:在 IE “工(gōng)具”菜單中(zhōng)顯示的菜單項;

MenuStatusBar:IE“工(gōng)具”菜單項的解釋,顯示在底部的狀态條上。

控件的方法:

AddBtn2IEtoolbar:添加自定義按鈕;

DelBtnFromIEtoolbar:删除自定義按鈕。

使用方法(以VB爲例)

在 VB 中(zhōng),首先将該用戶控件加入“部件”列表中(zhōng),然後把該控件從“工(gōng)具箱”拖放(fàng)到 EXE 工(gōng)程窗體(tǐ)上,再在窗體(tǐ)上放(fàng)兩個命令按鈕,命名爲 cmdADD 和 cmdDel,鍵入以下(xià)代碼:

Option Explicit

'該 GUID 号碼用特殊工(gōng)具産生(shēng)

Const theGUID = “{3FA4FB86-E43A-11D4-B3DD-00E006E25C0B}"

Private Sub cmdAdd_Click() '添加按鈕

AddButton1.Guid = theGUID

AddButton1.ButtonText = “控件"

AddButton1.Exec = “http://why100000.at.

China.com"

AddButton1.HotIcon = “C:\Add2IE Toolbar\Add2IEToolbar.exe,101"

AddButton1.Icon = “C:\Add2IE Toolbar\Add2IEToolbar.exe,102"

AddButton1.MenuText = “測試控件"

AddButton1.MenuStatusBar = “測試我(wǒ)(wǒ)制作的 ActiveX 控件"

AddButton1.AddBtn2IEtoolbar '執行

End Sub

Private Sub cmdDel_Click() '删除按鈕

AddButton1.Guid = theGUID

AddButton1.DelBtnFromIEtoolbar

End Sub

以上代碼中(zhōng),GUID 号可以用 VC++ 6.0 提供的 GUID 生(shēng)成器工(gōng)具産生(shēng)。方法是:運行 VC++ 6.0,打開(kāi)一(yī)個工(gōng)程,在“Project\Add to Project\Components and Controls\Visual C++ Components\GUID Generator”中(zhōng),按“Insert”按鈕,确定後,先選第 4 項“Registry Format”,再按“Copy”按鈕,GUID 生(shēng)成器就将一(yī)個新産生(shēng)的 GUID 号拷貝到了剪貼闆上。
按鈕的圖标包含在 C:\Add2IE Toolbar\Add2IEToolbar.exe 文件中(zhōng),101 和 102 是它們的順序号。按 cmdAdd 按鈕,然後打開(kāi) IE 浏覽器,就會看到 IE 工(gōng)具條上添加的自定義按鈕了。
文章來源:http://www.vbnew.net/artcile/list.asp?unid=1049
免責聲明:本站相關技術文章信息部分(fēn)來自網絡,目的主要是傳播更多信息,如果您認爲本站的某些信息侵犯了您的版權,請與我(wǒ)(wǒ)們聯系,我(wǒ)(wǒ)們會即時妥善的處理,謝謝合作!