⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 suitoolbar.pas

📁 新颖按钮控件
💻 PAS
字号:
////////////////////////////////////////////////////////////////////////////////
//
//
//  FileName    :   SUIToolBar.pas
//  Creator     :   Shen Min
//  Date        :   2002-12-12
//  Comment     :
//
//  Copyright (c) 2002-2003 Sunisoft
//  http://www.sunisoft.com
//  Email: support@sunisoft.com
//
////////////////////////////////////////////////////////////////////////////////

unit SUIToolBar;

interface

uses Windows, Messages, SysUtils, Classes, Controls, ToolWin, ComCtrls, Graphics,
     Forms, Imglist,
     SUIThemes;

type
    TsuiToolBar = class(TToolBar)
    private
        m_UIStyle : TsuiUIStyle;
        m_ButtonColor : TColor;
        m_ButtonBorderColor : TColor;
        m_ButtonDownColor : TColor;

        procedure DrawBar(Sender: TToolBar; const ARect: TRect; var DefaultDraw: Boolean);
        procedure DrawButton(Sender: TToolBar; Button: TToolButton; State: TCustomDrawState; var DefaultDraw: Boolean);

        procedure DrawDownButton(
            ACanvas : TCanvas;
            ARect : TRect;
            ImgLst : TCustomImageList;
            ImageIndex : Integer;
            Caption : TCaption
        );
        procedure DrawHotButton(
            ACanvas : TCanvas;
            ARect : TRect;
            ImgLst : TCustomImageList;
            ImageIndex : Integer;
            Caption : TCaption
        );

        procedure SetUIStyle(const Value: TsuiUIStyle);
        procedure SetButtonBorderColor(const Value: TColor);
        procedure SetButtonColor(const Value: TColor);
        procedure SetButtonDownColor(const Value: TColor);

    public
        constructor Create(AOwner : TComponent); override;

    published
        property UIStyle : TsuiUIStyle read m_UIStyle write SetUIStyle;
        property ButtonColor : TColor read m_ButtonColor write SetButtonColor;
        property ButtonBorderColor : TColor read m_ButtonBorderColor write SetButtonBorderColor;
        property ButtonDownColor : TColor read m_ButtonDownColor write SetButtonDownColor;

        property Color;
        property Transparent;

    end;

implementation

uses SUIPublic;

{ TsuiToolBar }

constructor TsuiToolBar.Create(AOwner: TComponent);
begin
    inherited;

    if not (csDesigning in ComponentState) then
        OnCustomDrawButton := DrawButton;
    OnCustomDraw := DrawBar;

    Flat := true;
    Transparent := false;
    UIStyle := GetSUIFormStyle(TCustomForm(AOwner));
end;

procedure TsuiToolBar.DrawBar(Sender: TToolBar; const ARect: TRect; var DefaultDraw: Boolean);
begin
    if Transparent then
        DoTrans(Canvas, self);
end;

procedure TsuiToolBar.DrawButton(
    Sender: TToolBar;
    Button: TToolButton;
    State: TCustomDrawState;
    var DefaultDraw: Boolean
);
var
    ACanvas : TCanvas;
    ARect : TRect;
    ImgLst : TCustomImageList;
begin
    ACanvas := Sender.Canvas;
    ARect := Button.BoundsRect;

    if cdsHot in State then
    begin
        if (HotImages <> nil) then
            ImgLst := HotImages
        else
            ImgLst := Images;

        if cdsSelected in State then
            DrawDownButton(ACanvas, ARect, ImgLst, Button.ImageIndex, Button.Caption)
        else
            DrawHotButton(ACanvas, ARect, ImgLst, Button.ImageIndex, Button.Caption);

        DefaultDraw := false;
    end
    else if (cdsSelected in State) or (cdsChecked in State) then
    begin
        if (HotImages <> nil) then
            ImgLst := HotImages
        else
            ImgLst := Images;

        DrawHotButton(ACanvas, ARect, ImgLst, Button.ImageIndex, Button.Caption);
        DefaultDraw := false;
    end;
end;

procedure TsuiToolBar.DrawDownButton(
    ACanvas : TCanvas;
    ARect : TRect;
    ImgLst : TCustomImageList;
    ImageIndex : Integer;
    Caption : TCaption
);
var
    nLeft : Integer;
    nWidth : Integer;
begin
    ACanvas.Brush.Color := m_ButtonDownColor;
    ACanvas.Pen.Color := m_ButtonBorderColor;
    ACanvas.Rectangle(ARect);

    if ImgLst = nil then
    begin
        if ShowCaptions then
        begin
            nWidth := ACanvas.TextWidth(Caption);
            nLeft := (ARect.Right - ARect.Left - nWidth) div 2;
            ACanvas.TextOut(ARect.Left + nLeft, 5, Caption);
        end;

        Exit;
    end;

    nLeft := (ARect.Right - ARect.Left - ImgLst.Width) div 2;
    ImgLst.Draw(ACanvas, ARect.Left + nLeft, 3, ImageIndex);

    if ShowCaptions then
    begin
        nWidth := ACanvas.TextWidth(Caption);
        nLeft := (ARect.Right - ARect.Left - nWidth) div 2;
        ACanvas.TextOut(ARect.Left + nLeft, ImgLst.Height + 4, Caption);
    end;
end;

procedure TsuiToolBar.DrawHotButton(
    ACanvas : TCanvas;
    ARect : TRect;
    ImgLst : TCustomImageList;
    ImageIndex : Integer;
    Caption : TCaption
);
var
    nLeft : Integer;
    nWidth : Integer;
begin
    ACanvas.Brush.Color := m_ButtonColor;
    ACanvas.Pen.Color := m_ButtonBorderColor;
    ACanvas.Rectangle(ARect);

    if ImgLst = nil then
    begin
        if ShowCaptions then
        begin
            nWidth := ACanvas.TextWidth(Caption);
            nLeft := (ARect.Right - ARect.Left - nWidth) div 2;
            ACanvas.TextOut(ARect.Left + nLeft, 4, Caption);
        end;

        Exit;
    end;

    nLeft := (ARect.Right - ARect.Left - ImgLst.Width) div 2;
    ImgLst.Draw(ACanvas, ARect.Left + nLeft - 1, 2, ImageIndex);

    if ShowCaptions then
    begin
        nWidth := ACanvas.TextWidth(Caption);
        nLeft := (ARect.Right - ARect.Left - nWidth) div 2;
        ACanvas.TextOut(ARect.Left + nLeft, ImgLst.Height + 4, Caption);
    end;
end;

procedure TsuiToolBar.SetButtonBorderColor(const Value: TColor);
begin
    m_ButtonBorderColor := Value;

    Repaint();
end;

procedure TsuiToolBar.SetButtonColor(const Value: TColor);
begin
    m_ButtonColor := Value;

    Repaint();
end;

procedure TsuiToolBar.SetButtonDownColor(const Value: TColor);
begin
    m_ButtonDownColor := Value;

    Repaint();
end;

procedure TsuiToolBar.SetUIStyle(const Value: TsuiUIStyle);
begin
    m_UIStyle := Value;

    if m_UIStyle = Custom then
        Exit;

    m_ButtonBorderColor := GetThemeColor(m_UIStyle, SUI_TOOLBAR_BUTTON_BORDER_COLOR);
    m_ButtonColor := GetThemeColor(m_UIStyle, SUI_TOOLBAR_BUTTON_COLOR);
    m_ButtonDownColor := GetThemeColor(m_UIStyle, SUI_TOOLBAR_BUTTON_DOWN_COLOR);
    Color := GetThemeColor(m_UIStyle, SUI_TOOLBAR_COLOR);
    if m_UIStyle = MacOS then
        Transparent := true
    else
        Transparent := false;

    EdgeInner := esNone;
    EdgeOuter := esNone;

    Repaint();
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -