Class modules - code to affect all userforms - logo image and close button hidden in titlebar

matija385

Board Regular
Joined
Sep 17, 2014
Messages
77
Hi all,

I need help with class modules. I need to show image (logo) in every userform in titlebar and hide close [x] button on titlebar.

I have two working pieces of code - one for logo and one for hiding close [x] button.

The problem I'm having is that I never worked with class modules and am not quite sure how, and other problem is that I'm still quite new at vba.

So if anyone would like to help me with this, I would much appriciate it.

Code for hiding close [X] button (it's in standard module, and in every userform at initialize i have line HideCloseButton Me):

Code:
 'Find the Dialog's Window
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 
 'Get the current window style
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
 
 'Set the new window style
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
 
Const WS_SYSMENU = &H80000
Const GWL_STYLE = (-16)

 'Routine to hide the close button on a userform or dialogsheet
 '   oDialog is either the Userform or Dialog object
 
Sub HideCloseButton(oDialog As Object)
     
    Dim hWnd As Long, lStyle As Long
     
     'Were we given a userform or a dialog sheet
    If TypeName(oDialog) = "DialogSheet" Then
         
         'We had a dialog sheet.  Note that pressing Escape still closes the dialog
         
        Select Case Int(val(Application.Version))
        Case 5 'Doesn't work in Excel 5 - we only have 32-bit DLL calls here
        Case 7 'Excel 95
            hWnd = FindWindow("bosa_sdm_XL", oDialog.DialogFrame.Caption) 'DialogSheet
        Case 8 'Excel 97
            hWnd = FindWindow("bosa_sdm_XL8", oDialog.DialogFrame.Caption) 'DialogSheet
        Case Else 'Excel 2000 and newer
            hWnd = FindWindow("bosa_sdm_XL9", oDialog.DialogFrame.Caption) 'DialogSheet
        End Select
    Else
         'We had a userform
        Select Case Int(val(Application.Version))
        Case 8 'Excel 97
            hWnd = FindWindow("ThunderXFrame", oDialog.Caption) 'UserForm
        Case Else 'Excel 2000 and newer
            hWnd = FindWindow("ThunderDFrame", oDialog.Caption) 'UserForm
        End Select
    End If
     
     'Get the current window style
    lStyle = GetWindowLong(hWnd, GWL_STYLE)
     
     'Turn off the System Menu bit
    SetWindowLong hWnd, GWL_STYLE, lStyle And Not WS_SYSMENU
     
End Sub


Code for inserting image (logo) in titlebar (this piece of code, as I figured out, should be in every userform):

Code:
Option Explicit


'API functions
Private Declare Function FindWindow Lib "user32" _
                                    Alias "FindWindowA" _
                                    (ByVal lpClassName As String, _
                                     ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" _
                                     Alias "SendMessageA" _
                                     (ByVal hWnd As Long, _
                                      ByVal wMsg As Long, _
                                      ByVal wParam As Long, _
                                      lParam As Any) As Long
Private Declare Function DrawMenuBar Lib "user32" _
                                     (ByVal hWnd As Long) As Long




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


Private Sub UserForm_Activate()
   AddIcon    'Add an icon on the titlebar
End Sub


Private Sub AddIcon()
'Add an icon on the titlebar
   Dim hWnd              As Long
   Dim lngRet            As Long
   Dim hIcon             As Long
   hIcon = Sheet1.Image1.Picture.Handle 'Me.Image1.Picture.Handle
   hWnd = FindWindow(vbNullString, Me.Caption)
   lngRet = SendMessage(hWnd, WM_SETICON, ICON_SMALL, ByVal hIcon)
   Debug.Print lngRet
   lngRet = SendMessage(hWnd, WM_SETICON, ICON_BIG, ByVal hIcon)
   Debug.Print lngRet


   lngRet = DrawMenuBar(hWnd)
   Debug.Print lngRet
End Sub

P.S.: both codes are not mine :), i found them on the internet and am very much thankfull to the authors :)

Thnx for every help,

Kind regards,
Matija
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top