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 for inserting image (logo) in titlebar (this piece of code, as I figured out, should be in every userform):
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
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