RAD Studio XE7 VCL组件TSpeedButton在Windows11悬停时背景透明丢失求助
我在VCL应用中用TSpeedButton实现鼠标悬停高亮效果,该组件在Windows 7和Windows 10上悬停表现正常,但到了Windows 11,悬停时背景会变成不透明的,不符合需求。需要不用更换或重载组件的快速方案,恢复它的背景透明特性。
最小复现代码
Main.h
#ifndef MainH #define MainH #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> #include <Vcl.Buttons.hpp> #include <Vcl.ExtCtrls.hpp> class TMainForm : public TForm { __published: TPanel *paBackground; TLabel *laCaption; TSpeedButton *btGlowEffect; public: __fastcall TMainForm(TComponent* pOwner); }; extern PACKAGE TMainForm *MainForm; #endif
Main.cpp
#include <vcl.h> #pragma hdrstop #include "Main.h" #pragma package(smart_init) #pragma resource "*.dfm" TMainForm *MainForm; __fastcall TMainForm::TMainForm(TComponent* pOwner) : TForm(pOwner) {}
Main.dfm
object MainForm: TMainForm Left = 0 Top = 0 Caption = 'MainForm' ClientHeight = 321 ClientWidth = 678 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object paBackground: TPanel Left = 64 Top = 16 Width = 545 Height = 49 BevelOuter = bvNone TabOrder = 0 object laCaption: TLabel Left = 0 Top = 0 Width = 545 Height = 49 Align = alClient Alignment = taCenter Caption = 'This is a demo text' Layout = tlCenter ExplicitWidth = 90 ExplicitHeight = 13 end object btGlowEffect: TSpeedButton Left = 0 Top = 0 Width = 545 Height = 49 Align = alClient Flat = True ExplicitLeft = 104 ExplicitTop = 8 ExplicitWidth = 23 ExplicitHeight = 22 end end end
快速解决方案
不用重载或更换组件,只需两个简单设置就能恢复透明背景:
- 设置
ParentBackground为True:让按钮继承父容器的背景,避免绘制独立的不透明背景。 - 移除
seBackground样式元素:禁用Windows 11视觉样式对按钮背景的绘制逻辑。
方法一:通过代码设置
修改Main.cpp的构造函数,添加以下代码:
__fastcall TMainForm::TMainForm(TComponent* pOwner) : TForm(pOwner) { btGlowEffect->ParentBackground = true; // 去掉背景样式元素,保留字体和边框 btGlowEffect->StyleElements = seFont | seBorder; }
方法二:直接修改DFM文件
在btGlowEffect的属性块中添加两行:
object btGlowEffect: TSpeedButton // 原有属性... ParentBackground = True StyleElements = [seFont, seBorder] end
原理:Windows 11的VCL控件默认启用了seBackground样式,会调用系统视觉样式绘制悬停背景,导致不透明。移除该元素后,按钮会使用父控件的背景,同时保留高亮的字体/边框效果,符合需求。
内容的提问来源于stack exchange,提问作者Jean-Milost Reymond
相关产品推荐
相关产品推荐

