扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
实现方法
要添加这种自定义按钮,需要在 Windows 注册表的HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Extensions 子键下建立一个 GUID 项,然后在注册表的右栏建立相应的参数并赋值。这些参数及其含义如下:
ButtonText:自定义按钮上显示的文本字符串;
Clsid:IE 工具条的类标识码,为“{1FBA04EE-3024-11D2-8F1F0000F87ABD16}”;
Default Visible:自定义按钮是否可见,一般为 “Yes";
Exec:自定义按钮执行的目标,为可执行文件或超文本链接等;
HotIcon:鼠标移上按钮时显示的图标,一般取自 EXE 文件或 DLL 文件;
Icon:按钮正常显示的图标,一般取自 EXE 文件或 DLL 文件;
MenuText:在 IE 的“工具”菜单中显示的菜单项;
MenuStatusBar:IE的“工具”菜单项的注释,在状态条上显示。
制作过程
在 VB 5.0/6.0 下建立新的“ActiveX 控件”工程。
首先,给控件 AddButton 的 Picture 属性添加一个微型 Icon 图片,这样当使用该控件时,会在控件工具箱上显示这个小图标。然后,再在设计窗口上放置一个 image 控件,给它也设置一个小图片,使用该控件时,会在其 Form 上显示该控件的外观。最后,调出代码编辑窗口,在该窗口中键入以下代码,其中 API 函数调用的代码可以直接从“API 文本浏览器”中获得。
''注意:Guid的值是特定的,才可以获取
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
''定义注册表中的主键、子键
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
’从注册表中删除字符串值
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
''从注册表中删除自定义按钮
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
''从存储器中加载属性值
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
’显示“关于”对话框
Public Sub ShowAboutBox()
frmAbout.Show vbModal
Unload frmAbout
Set frmAbout = Nothing
End Sub
该控件提供了 7 个属性和2 个方法。
控件的属性:
GUID:一个在注册表唯一标识应用软件的数字序列,共36位;
ButtonText:按钮上显示的文本;
Exec:按钮执行的目标,为可执行文件或超文本链接等;
HotIcon:鼠标移上按钮时显示的图标,取自 EXE 文件或 DLL 文件;
Icon:按钮正常显示的图标,取自 EXE 文件或 DLL 文件;
MenuText:在 IE “工具”菜单中显示的菜单项;
MenuStatusBar:IE“工具”菜单项的解释,显示在底部的状态条上。
控件的方法:
AddBtn2IEtoolbar:添加自定义按钮;
DelBtnFromIEtoolbar:删除自定义按钮。
使用方法(以VB为例)
在 VB 中,首先将该用户控件加入“部件”列表中,然后把该控件从“工具箱”拖放到 EXE 工程窗体上,再在窗体上放两个命令按钮,命名为 cmdADD 和 cmdDel,键入以下代码:
Option Explicit
''该 GUID 号码用特殊工具产生
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 = “测试我制作的 ActiveX 控件"
AddButton1.AddBtn2IEtoolbar ''执行
End Sub
Private Sub cmdDel_Click() ''删除按钮
AddButton1.Guid = theGUID
AddButton1.DelBtnFromIEtoolbar
End Sub
以上代码中,GUID 号可以用 VC++ 6.0 提供的 GUID 生成器工具产生。方法是:运行 VC++ 6.0,打开一个工程,在“Project\Add to Project\Components and Controls\Visual C++ Components\GUID Generator”中,按“Insert”按钮,确定后,先选第 4 项“Registry Format”,再按“Copy”按钮,GUID 生成器就将一个新产生的 GUID 号拷贝到了剪贴板上。
按钮的图标包含在 C:\Add2IE Toolbar\Add2IEToolbar.exe 文件中,101 和 102 是它们的顺序号。按 cmdAdd 按钮,然后打开 IE 浏览器,就会看到 IE 工具条上添加的自定义按钮了。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者