Im working on a login UserForm for my worksheets. However, when I run it to preview how it will appear on my screen, the white part of my label becomes transparent.
For some context, I’m utilizing a module to enable the background and buttons to disappear. I designed the interface in PowerPoint and then copied it into a Picture on the Label. Initially, everything appears normal, as shown in this image. However, upon running it, the background becomes transparent, leading to the issue in this image, Consequently, all the white part become transparent, glitched Label. I have tested the interface without the code, and the white part still becomes transparent.
I’m using Office Professional 2021 | 2041 | Build 17231.20194
Module11 (UserForm without background and buttons)
Option Explicit
#If VBA7 Then
'// 64 Bits
'// DLL declarations to alter the UserForm appearance
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare PtrSafe Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare PtrSafe Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
#Else
'// 32 Bits
'// DLL declarations to alter the UserForm appearance
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
#End If
'// Windows constants for title bar
Private Const GWL_STYLE As Long = (-16) '// The offset of a window's style
Private Const GWL_EXSTYLE As Long = (-20) '// The offset of a window's extended style
Private Const WS_CAPTION As Long = &HC00000 '// Style to add a title bar
Private Const WS_EX_DLGMODALFRAME As Long = &H1 '// Controls if the window has an icon
'// Windows constants for transparency
Private Const WS_EX_LAYERED = &H80000 '// Color
Private Const LWA_COLORKEY = &H1 '// Chroma key for fading a certain color on your Form
Private Const LWA_ALPHA = &H2 '// Only needed if you want to fade the entire UserForm
Function HideTitleBarAndBordar(frm As Object)
'// Hide title bar and border around the form
Dim lngWindow As Long
Dim lFrmHdl As Long
lFrmHdl = FindWindow(vbNullString, frm.Caption)
'// Build window and set window until you remove the caption, title bar and frame around the window
lngWindow = GetWindowLong(lFrmHdl, GWL_STYLE)
lngWindow = lngWindow And (Not WS_CAPTION)
SetWindowLong lFrmHdl, GWL_STYLE, lngWindow
lngWindow = GetWindowLong(lFrmHdl, GWL_EXSTYLE)
lngWindow = lngWindow And Not WS_EX_DLGMODALFRAME
SetWindowLong lFrmHdl, GWL_EXSTYLE, lngWindow
DrawMenuBar lFrmHdl
End Function
Function MakeUserformTransparent(frm As Object, Optional Color As Variant)
'// Set transparencies on UserForm
Dim formhandle As Long
Dim bytOpacity As Byte
formhandle = FindWindow(vbNullString, frm.Caption)
If IsMissing(Color) Then Color = &H8000& '// rgbWhite
bytOpacity = 0
SetWindowLong formhandle, GWL_EXSTYLE, GetWindowLong(formhandle, GWL_EXSTYLE) Or WS_EX_LAYERED
frm.BackColor = Color
SetLayeredWindowAttributes formhandle, Color, bytOpacity, LWA_COLORKEY
End Function
Code on UserForm1
Private Sub UserForm_Activate()
HideTitleBarAndBordar Me
MakeUserformTransparent Me
End Sub