Excel 在用户表单标题中使用图像

Excel 在用户表单标题中使用图像,excel,vba,Excel,Vba,我正在开发一个UserForm,我正在尝试在UserForm的caption属性中使用IE/Chrome等徽标,以便在窗口框架中显示徽标,然后显示一些文本 我做了一些浏览,并在网上找到了以下代码,但我在涉及ExtractIcon的行上得到了一个sub/function not defined错误 用户表单代码 Private Sub UserForm_Initialize Dim strIconPath As String Dim lngIcon As Long

我正在开发一个UserForm,我正在尝试在UserForm的caption属性中使用IE/Chrome等徽标,以便在窗口框架中显示徽标,然后显示一些文本

我做了一些浏览,并在网上找到了以下代码,但我在涉及ExtractIcon的行上得到了一个sub/function not defined错误

用户表单代码

Private Sub UserForm_Initialize 

    Dim strIconPath As String
     Dim lngIcon As Long
     Dim lnghWnd As Long

     ' Change to the path and filename of an icon file
     strIconPath = "C:\Users\suttond\Desktop\Picture2.gif"
     ' Get the icon from the source
     lngIcon = ExtractIcon(0, strIconPath, 0)
     ' Get the window handle of the userform
     lnghWnd = FindWindow("ThunderDFrame", UserForm1.Caption)
     'Set the big (32x32) and small (16x16) icons
     SendMessage lnghWnd, WM_SETICON, True, lngIcon
     SendMessage lnghWnd, WM_SETICON, False, lngIcon

End Sub
Private Declare Function FindWindow _
     Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
     ByVal lpWindowName As String) As Long

 Private Declare Function ExtractIcon _
     Lib "shell32.dll" Alias "ExtractIconA" _
    (ByVal hInst As Long, _
     ByVal lpszExeFileName As String, _
     ByVal nIconIndex As Long) As Long

 Private Declare Function SendMessage _
     Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Integer, _
     ByVal lParam As Long) As Long

 Private Const WM_SETICON = &H80

模块代码

Private Sub UserForm_Initialize 

    Dim strIconPath As String
     Dim lngIcon As Long
     Dim lnghWnd As Long

     ' Change to the path and filename of an icon file
     strIconPath = "C:\Users\suttond\Desktop\Picture2.gif"
     ' Get the icon from the source
     lngIcon = ExtractIcon(0, strIconPath, 0)
     ' Get the window handle of the userform
     lnghWnd = FindWindow("ThunderDFrame", UserForm1.Caption)
     'Set the big (32x32) and small (16x16) icons
     SendMessage lnghWnd, WM_SETICON, True, lngIcon
     SendMessage lnghWnd, WM_SETICON, False, lngIcon

End Sub
Private Declare Function FindWindow _
     Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
     ByVal lpWindowName As String) As Long

 Private Declare Function ExtractIcon _
     Lib "shell32.dll" Alias "ExtractIconA" _
    (ByVal hInst As Long, _
     ByVal lpszExeFileName As String, _
     ByVal nIconIndex As Long) As Long

 Private Declare Function SendMessage _
     Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Integer, _
     ByVal lParam As Long) As Long

 Private Const WM_SETICON = &H80

基本上,一个小的IE浏览器徽标将显示在标题中已有文本的左侧

编辑


模块代码函数更新为public,以允许从初始化代码调用它们。不再获取提取错误,但图像没有出现在用户表单标题中。

正如Mistella和Rory在您的问题评论中提到的,函数和常量需要声明为
Public
。如果将它们声明为
Private
,则它们仅在模块中已知,而不在表单中

第二件事是你需要从ICO文件中读取图标,而不是从gif文件中读取,所以你需要转换它。我用它来完成这样的任务,但是有很多工具(甚至在线)。我做了一个快速测试,它成功了:


您可能需要将模块声明从private更改为public…..如果您想从其他模块调用这些API函数,则这些API函数需要是public。这样可以解决错误,但不会将图像输入到标题中吗?有什么想法吗?我需要学习阅读XD我很抱歉占用你的时间,但谢谢你!