Is there a way to make TRadioButton be Transparent?

泄露秘密 提交于 2019-12-10 23:29:01

问题


I'm using delphi 2010


回答1:


I agree with Andreas and Serg in that the control is transparent when themes are enabled.

I, once, had tried to make the CheckBox transparent for when runtime themes are not enabled in project options, or a classic theme is selected with the OS; the result was not perfect. The below is the same code applied to the RadioButton.

Problems easily noticable are, as you would guess from the code, it's a bit flickery and it is not transparent when DoubleBuffered. A problem not easily noticable can (sometimes) be duplicated by bringing a different window in front of the form containing the controls, and then slowly moving it aside, sometimes this leaves some artifacts.

Well, anyway, here it is;

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;



回答2:


Quote Remy Lebeau (TeamB):

TLabel is a TGraphicControl descendant, and thus has to do all of its own drawing manually, so it can implement transparency as needed. TCheckBox and TRadioButton, on the other hand, are TWinControl descendants that wrap standard Win32 API controls, and thus are subject to whatever capabilities the OS supports for them (transparency is not one of them). https://forums.codegear.com/thread.jspa?threadID=24027&tstart=375

You either need to do some heavy overriding, or else you will need to use a third party component...




回答3:


A simple trick: make the button color white, shrink it to the minimum size, only the button; and put a transparent label behind it.

Otherwise, to make a button really transparent you need to owner draw it. You may find some examples in the web.

I found some information on responding to the WM_CTLCOLOR message. But I gave a quick try but couldn't quite get it to work.




回答4:


I experimented with the standard VCL TRadioButton control in Delphi 2009 (I suppose Delphi 2010 is the same).

If you compile the project with runtime themes enabled (Project->Options->Application->Enable Runtime Themes), the TRadioButton control is transparent and its 'Color' property ignored. If the runtime themes disabled, the TRadioButton control is not transparent and its background is defined by its 'Color' property.

So I assume that the standard VCL TRadioButton (and the underlying windows control) is made transparent by the Windows theme, not by the control itself. You can switch off the theme support on application level, and in that case you get a non-transparent radio button. If you need a transparent radiobutton with runtime themes disabled, use 3rd party custom radiobutton (TCustomControl descendant, not a standard Windows radiobutton wrapper)




回答5:


The easiest way is to buy a component set like Raize Components which will do this for you and lots more besides. Raize in particular allows you to customize lots of aspects of the UI.




回答6:


http://www.torry.net/quicksearchd.php?String=transparent+radiobutton&Title=No might help. None of those are D2010 or D2009, but I believe porting would be possible.



来源:https://stackoverflow.com/questions/2461793/is-there-a-way-to-make-tradiobutton-be-transparent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!