You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Office 2021中UserForm标签白色背景变透明问题求助

Excel VBA UserForm Label白色区域运行后变透明问题

我正在为工作表制作登录UserForm,但运行预览时,Label的白色部分变为透明。

背景说明

  • 通过模块实现窗体背景透明和标题栏隐藏,先在PowerPoint中设计界面,再复制到Label的Picture属性中。
  • 初始状态显示正常,但运行后背景变透明,导致Label的白色区域全部透明。
  • 即使不使用代码,Label的白色部分仍会出现透明问题。
  • 使用版本:Office Professional 2021 | 2041 | Build 17231.20194

Module11(隐藏标题栏和边框的模块代码)

Option Explicit

#If VBA7 Then
'// 64位系统
'// 用于修改UserForm外观的DLL声明
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位系统
'// 用于修改UserForm外观的DLL声明
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常量
Private Const GWL_STYLE As Long = (-16)           '// 窗口样式的偏移量
Private Const GWL_EXSTYLE As Long = (-20)         '// 窗口扩展样式的偏移量
Private Const WS_CAPTION As Long = &HC00000       '// 带标题栏的样式
Private Const WS_EX_DLGMODALFRAME As Long = &H1   '// 控制窗口是否有图标

'// 透明度相关Windows常量
Private Const WS_EX_LAYERED = &H80000             '// 分层窗口样式
Private Const LWA_COLORKEY = &H1                  '// 对窗体特定颜色实现透明的色度键
Private Const LWA_ALPHA = &H2                     '// 仅在需要整个UserForm淡入淡出时使用

Function HideTitleBarAndBordar(frm As Object)

'// 隐藏窗体的标题栏和边框
    Dim lngWindow As Long
    Dim lFrmHdl As Long
    lFrmHdl = FindWindow(vbNullString, frm.Caption)
'// 修改窗口样式以移除标题栏和边框
    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)

'// 设置UserForm的透明度
Dim formhandle As Long
Dim bytOpacity As Byte

formhandle = FindWindow(vbNullString, frm.Caption)
If IsMissing(Color) Then Color = &H8000&        '// 默认白色
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

内容的提问来源于stack exchange,提问作者Ropelato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 18:35:36