UserForm 删除白色背景

问题描述 投票:0回答:1

我正在为我的工作表创建登录用户表单。但是,当我运行它来预览它在屏幕上的显示方式时,标签的白色部分变得透明。

对于某些情况,我正在利用一个模块来使背景和按钮消失。我在 PowerPoint 中设计了界面,然后将其复制到标签上的图片中。最初,一切看起来都很正常,如图image所示。然而,运行后,背景变成透明的,导致了这个问题image,结果,所有白色部分都变成透明了,glitched Label。我测试了没有代码的界面,白色部分还是透明的。

我正在使用 Office Professional 2021 | 2041 | 2041内部版本 17231.20194

Module11(没有背景和按钮的用户窗体)

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

UserForm1 上的代码

Private Sub UserForm_Activate()
    HideTitleBarAndBordar Me
    MakeUserformTransparent Me
End Sub

excel vba userform
1个回答
0
投票

试试这个:

Function MakeUserformTransparent(frm As Object, Optional Color As Variant)
Dim formhandle As Long
Dim bytOpacity As Byte
formhandle = FindWindow(vbNullString, frm.Caption)
Color = vbWhite
frm.BackColor = Color
SetLayeredWindowAttributes formhandle, Color, bytOpacity, LWA_COLORKEY
End Function
© www.soinside.com 2019 - 2024. All rights reserved.