在用户窗体标题中使用图像

时间:2019-04-26 13:48:43

标签: excel vba

我正在使用UserForm,并且尝试在UserForm的标题属性中使用IE / Chrome等徽标,以便徽标在窗口框架中显示,后跟一些文本。

我已经进行了一些浏览,并且在线找到了以下代码,但是在涉及ExtractIcon的行上出现未定义的子/函数错误。

用户表单代码

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 Explorer徽标将显示在标题中已有文本的左侧。

修改

模块代码功能已更新为公共功能,允许从初始化代码中调用它们。不再出现提取错误,但是图像未出现在用户窗体标题中。

1 个答案:

答案 0 :(得分:1)

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

第二件事是您需要从ICO文件而不是从gif中读取图标,因此您需要对其进行转换。我将IrfanView用于此类任务,但有大量工具(甚至在线)可用。我做了一个快速测试,就可以了:

enter image description here