delphi dev cxbutton 如何实现自定义各种状态的颜色,包括去掉边框

cxbuttons.pas

function TcxSpeedButtonOptions.NeedDrawParts: Boolean;
begin
  Result := not CanBeFocused and (Transparent or Flat);
end;
procedure TcxCustomButton.InternalPaint;

 //要不要画边框的条件

function NeedDrawBorder(AState: TcxButtonState): Boolean;
begin
Result := (LookAndFeel.SkinPainter = nil) and
(not SpeedButtonOptions.Flat or IsDesigning or ((AState <> cxbsDisabled) and
((bisHot in InternalState) or (AState <> cxbsNormal)))) and not
(LookAndFeel.NativeStyle or (LookAndFeel.Kind = lfOffice11));
end;

procedure InternalDrawButton(R: TRect; AState: TcxButtonState; AColor: TColor; AIsMenuButton: Boolean = False);
  begin
    if SpeedButtonOptions.NeedDrawParts then
    begin
      if NeedDrawBorder(AState) then
      begin
        Painter.DrawButtonBorder(FCanvas, R, AState);
        InflateRect(R, -Painter.ButtonBorderSize(AState), -Painter.ButtonBorderSize(AState));
      end;
      FCanvas.SaveClipRegion;
      try
        FCanvas.SetClipRegion(TcxRegion.Create(R), roSet);
        if not SpeedButtonOptions.Transparent or ((AState <> cxbsDisabled) and
          (not (bisHot in InternalState) and (AState <> cxbsNormal)) or
          ((bisHot in InternalState) and Painter.IsButtonHotTrack))
        then
          Painter.DrawButton(FCanvas, R, '', AState, False, AColor, FCanvas.Font.Color, WordWrap, True)
        else
          cxDrawTransparentControlBackground(Self, FCanvas, ClientRect);
      finally
        FCanvas.RestoreClipRegion;
      end;
    end
    else
      Painter.DrawButton(FCanvas, R, '', AState, True, AColor, FCanvas.Font.Color, WordWrap);
  end;


procedure TcxCustomLookAndFeelPainter.DrawButton(ACanvas: TcxCanvas;
  R: TRect; const ACaption: string; AState: TcxButtonState; ADrawBorder: Boolean = True;
  AColor: TColor = clDefault; ATextColor: TColor = clDefault; AWordWrap: Boolean = False;
  AIsToolButton: Boolean = False);
var
  AFlags: Integer;
begin
  with ACanvas do
  begin
    if ADrawBorder then
    begin
      DrawButtonBorder(ACanvas, R, AState);
      InflateRect(R, -ButtonBorderSize(AState), -ButtonBorderSize(AState));
    end;
    if AColor = clDefault then
      Brush.Color := ButtonColor(AState)
    else
      Brush.Color := AColor;
    FillRect(R);
    if ATextColor = clDefault then
      Font.Color := ButtonSymbolColor(AState)
    else
      Font.Color := ATextColor;
    Brush.Style := bsClear;
    with R do // for compatible with standard buttons
    begin
      Dec(Bottom, Ord(Odd(Bottom - Top)));
      if (Bottom - Top) < 18 then Dec(Top);
    end;
    if AState = cxbsPressed then
      OffsetRect(R, ButtonTextShift, ButtonTextShift);
    if Length(ACaption) > 0 then
    begin
      AFlags := cxAlignVCenter or cxShowPrefix or cxAlignHCenter;
      if AWordWrap then
        AFlags := AFlags or cxWordBreak
      else
        AFlags := AFlags or cxSingleLine;
      DrawText(ACaption, R, AFlags, AState <> cxbsDisabled);
    end;
    Brush.Style := bsSolid;
  end;
end;

 

第一种方式:

object Button1: TcxButton

Left = 424
Top = 401
Width = 75
Height = 25
Caption = 'Menu_1'
TabOrder = 13
LookAndFeel.NativeStyle = False
LookAndFeel.SkinName = 'SideNav'
SpeedButtonOptions.Transparent = True
end

然后重写一个painter, skinmae 指定一下'sidenav'就可以了

uses
  Windows,Types,Graphics, cxGraphics, cxLookAndFeelPainters;

 TSideNavPainter = class(TcxUltraFlatLookAndFeelPainter)
  public
    function LookAndFeelName: string; override;
    function LookAndFeelStyle: TcxLookAndFeelStyle; override;
    procedure DrawButtonBorder(ACanvas: TcxCanvas; R: TRect; AState: TcxButtonState); override;
  end;

{ TSideNavPainter }
function TSideNavPainter.LookAndFeelName: string;
begin
  Result := 'SideNav';
end;

function TSideNavPainter.LookAndFeelStyle: TcxLookAndFeelStyle;
begin
  Result := lfsSkin;
end;

procedure TSideNavPainter.DrawButtonBorder(
  ACanvas: TcxCanvas; R: TRect; AState: TcxButtonState);

  //不要边框
  function GetBorderColor: TColor;
  begin
    Result := ClNone;
  end;

begin
  if AState = cxbsDefault then
  begin
    ACanvas.FrameRect(R, GetBorderColor);
    InflateRect(R, -1, -1);
    ACanvas.FrameRect(R, GetBorderColor);
  end
  else
    ACanvas.FrameRect(R, GetBorderColor);
end;


initialization
  cxLookAndFeelPaintersManager.Register(TSideNavPainter.Create);
finalization
  cxLookAndFeelPaintersManager.UnRegister('SideNav');

第二种方式: 按以下设置,然后设置SkinName  跟上面的一样,不需要处理DrawButtonBorder .原理是因为有 LookAndFeel.SkinPainter 就不会画边框

object Menu_cy: TcxButton
Left = 0
Top = 54
Width = 161
Height = 50
Caption = #24120#29992
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
ParentShowHint = False
ShowHint = False
TabOrder = 1
OnClick = Menu_cyClick
Align = alTop
Colors.Normal = 13199402
Colors.NormalText = clWhite
Colors.HotText = clWhite
Colors.Pressed = 9915936
Colors.PressedText = clWhite
Colors.Disabled = 9915936
Colors.DisabledText = clWhite
LookAndFeel.NativeStyle = False
LookAndFeel.SkinName = 'SideNav'
OptionsImage.ImageIndex = 0
OptionsImage.Images = cxmglst_NavImage
OptionsImage.Margin = 12
OptionsImage.Spacing = 12
SpeedButtonOptions.GroupIndex = 1
SpeedButtonOptions.CanBeFocused = False
SpeedButtonOptions.Flat = True
SpeedButtonOptions.Transparent = True
OnMouseEnter = Menu_cyMouseEnter
end

效果:

 

posted @ 2024-04-26 17:03  Tag  阅读(39)  评论(0编辑  收藏  举报