Adding an icon on the title bar of the Userform

Generally an Userform does not have an icon on the title bar. For decoration, you can add an icon on the title bar of the Userform with API like the image below.

As far as I knew, the icon file (the extension is .ico) needs to be prepared apart from the Excel file.
But here is the way of embedding an icon in a worksheet as an image control, and displaying the picture on the Userform using the handle of the embedded icon image.

  1. Add an image control on the worksheet. (In this example, the image control is placed in the Sheet1)

    You can add image controls View - Toolbars - Control Toolbox from main menu bar.
  2. Right click for Propertiesof the image control and select appropriate ico file(with an extension of .ico) from picture property box in the properties window.
    Now you can see the picture on the Sheet1 as follows.

  3. Place the following in the Userform module.

Note: Some kind of ico image doesn't work. I am not sure about the reason.

Option Explicit

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

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

Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As LongAs Long

Private Const WM_SETICON = &H80
Private Const ICON_SMALL = 0&
Private Const ICON_BIG = 1&

Private Sub UserForm_Initialize()
    Dim hWnd As Long
    Dim lngRet As Long
    Dim hIcon As Long
    hIcon = Sheet1.Image1.Picture.Handle
    hWnd = FindWindow(vbNullString, Me.Caption)
    lngRet = SendMessage(hWnd, WM_SETICON, ICON_SMALL, ByVal hIcon)
    lngRet = SendMessage(hWnd, WM_SETICON, ICON_BIG, ByVal hIcon)
    lngRet = DrawMenuBar(hWnd)
End Sub

| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo