我用的是delphi 2010
6 回答
我同意 Andreas 和 Serg 的观点,即启用主题时控件是透明的。
当项目选项中未启用运行时主题或使用操作系统选择经典主题时,我曾经尝试使 CheckBox 透明;结果并不完美。下面是应用于 RadioButton 的相同代码。
很容易注意到的问题是,正如您从代码中猜到的那样,它有点闪烁,并且在 DoubleBuffered 时不透明。一个不容易注意到的问题可以(有时)通过在包含控件的表单前面放置一个不同的窗口来复制,然后慢慢地将它移到一边,有时这会留下一些伪影。
好吧,无论如何,就在这里;
type
TMyRadioButton = class(TRadioButton)
private
procedure CnCtlColorStatic(var Msg: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
procedure WmEraseBkgnd(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure WmPaint(var Msg: TWMNCPaint); message WM_PAINT;
protected
procedure CreateParams(var Params: TCreateParams); override;
end;
implementation
uses
themes;
procedure TMyRadioButton.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;
end;
procedure TMyRadioButton.WmPaint(var Msg: TWMNCPaint);
begin
if not (ThemeServices.ThemesEnabled or DoubleBuffered) then
InvalidateRect(Handle, nil, True);
inherited;
end;
procedure TMyRadioButton.WmEraseBkgnd(var Msg: TWMEraseBkgnd);
var
R: TRect;
begin
if not (ThemeServices.ThemesEnabled or DoubleBuffered)
and (Parent <> nil) then begin
R := Rect(Left, Top, Left + Width, Height + Top);
InvalidateRect(Parent.Handle, @R, True);
UpdateWindow(Parent.Handle);
Msg.Result := 1;
end else
inherited;
end;
procedure TMyRadioButton.CnCtlColorStatic(var Msg: TWMCtlColorStatic);
begin
if not (ThemeServices.ThemesEnabled or DoubleBuffered) then begin
SetBKMode(Msg.ChildDC, TRANSPARENT);
Msg.Result := GetStockObject(NULL_BRUSH);
end else
inherited;
end;
引用 Remy Lebeau (TeamB):
TLabel 是 TGraphicControl 的后代,因此必须手动完成所有自己的绘图,因此它可以根据需要实现透明度。另一方面,TCheckBox 和 TRAdioButton 是包装标准 Win32 API 控件的 TWinControl 后代,因此受制于操作系统为它们支持的任何功能(透明度不是其中之一)。 https://forums.codegear.com/thread.jspa?threadID=24027&tstart=375
您要么需要进行一些繁重的覆盖,否则您将需要使用第三方组件......
一个简单的技巧:让按钮颜色变白,缩小到最小尺寸,只有按钮;并在其后面贴上透明标签。
否则,要使按钮真正透明,您需要所有者绘制它。您可以在网上找到一些示例。
我找到了一些关于响应 WM_CTLCOLOR 消息的信息。但我快速尝试了一下,但无法让它发挥作用。
我在 Delphi 2009 中试验了标准的 VCL TRAdioButton 控件(我想 Delphi 2010 是一样的)。
如果在启用运行时主题的情况下编译项目(Project->Options->Application->Enable Runtime Themes),则 TRadioButton 控件是透明的,并且它的“Color”属性将被忽略。如果禁用运行时主题,则 TRAdioButton 控件不透明,其背景由其“颜色”属性定义。
所以我假设标准的 VCL TRAdioButton(和底层的 Windows 控件)是由 Windows 主题而不是控件本身使透明的。您可以在应用程序级别关闭主题支持,在这种情况下,您会得到一个不透明的单选按钮。如果您需要禁用运行时主题的透明单选按钮,请使用第 3 方自定义单选按钮(TCustomControl 后代,而不是标准的 Windows 单选按钮包装器)
最简单的方法是购买像Raize Components这样的组件集,它会为你做这件事,除此之外还有更多。Raize 特别允许您自定义 UI 的许多方面。
http://www.torry.net/quicksearchd.php?String=transparent+radiobutton&Title=No可能有帮助。这些都不是 D2010 或 D2009,但我相信移植是可能的。