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

📄 suititlebar.pas

📁 新颖按钮控件
💻 PAS
📖 第 1 页 / 共 3 页
字号:
///////////////////////////////////////////////////////////////////////////////////
//
//
//  FileName    :   SUITitleBar.pas
//  Creater     :   Shen Min
//  Date        :   2001-10-15
//  Comment     :
//
//  Copyright (c) 2002-2003 Sunisoft
//  http://www.sunisoft.com
//  Email: support@sunisoft.com
//
////////////////////////////////////////////////////////////////////////////////////

unit SUITitleBar;

interface

uses Windows, Controls, Classes, Forms, Messages, Graphics, Menus, ExtCtrls,
     Dialogs,
     SUIThemes;


type
    TsuiTitleBarSections = class;
    TsuiTitleBarButtons = class;

    TsuiTitleBarButtonClickEvent = procedure (Sender : TObject; ButtonIndex : Integer) of object;

    TsuiTitleBar = class(TCustomPanel)
    private
        m_UIStyle : TsuiUIStyle;
        m_Sections : TsuiTitleBarSections;
        m_Buttons : TsuiTitleBarButtons;

        m_AutoSize : Boolean;
        m_ButtonInterval : Integer;
        m_Caption : TCaption;
        m_Active : Boolean;
        m_LeftBtnXOffset : Integer;
        m_RightBtnXOffset : Integer;
        m_RoundCorner : Integer;
        m_DrawAppIcon : Boolean;

        m_MouseDown : Boolean;
        m_InButtons : Integer;

        m_OnBtnClick : TsuiTitleBarButtonClickEvent;
        m_OnHelpBtnClick : TsuiTitleBarButtonClickEvent;

        procedure CMFONTCHANGED(var Msg : TMessage); message CM_FONTCHANGED;
        procedure SetButtons(const Value : TsuiTitleBarButtons);
        procedure SetSections(const Value : TsuiTitleBarSections);
        procedure SetUIStyle(const Value : TsuiUIStyle);
        procedure SetButtonInterval(const Value : Integer);
        procedure SetCaption(const Value : TCaption);
        procedure SetActive(const Value : Boolean);
        procedure FSetAutoSize(Value : Boolean);
        procedure SetLeftBtnXOffset(const Value: Integer);
        procedure SetRightBtnXOffset(const Value: Integer);
        procedure SetFormRoundCorner(Round : Integer);
        procedure WMERASEBKGND(var Msg : TMessage); message WM_ERASEBKGND;
        procedure SetDrawAppIcon(const Value: Boolean);

    protected
        procedure Paint(); override;
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
        procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
        procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
        procedure DblClick(); override;
        procedure MouseOut(var Msg : TMessage); message CM_MOUSELEAVE;

        procedure DrawSectionsTo(Buf : TBitmap); virtual;
        procedure DrawButtons(Buf : TBitmap); virtual;

        function InForm() : Boolean;

    public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy(); override;

        procedure OnFormReSize();

    published
        property AutoSize read m_AutoSize write FSetAutoSize;
        property BiDiMode;        
        property Sections : TsuiTitleBarSections read m_Sections write SetSections;
        property UIStyle : TsuiUIStyle read m_UIStyle write SetUIStyle;
        property Buttons : TsuiTitleBarButtons read m_Buttons write SetButtons;
        property ButtonInterval : Integer read m_ButtonInterval write SetButtonInterval;
        property Caption read m_Caption write SetCaption;
        property Font;
        property FormActive : Boolean read m_Active write SetActive;
        property LeftBtnXOffset : Integer read m_LeftBtnXOffset write SetLeftBtnXOffset;
        property RightBtnXOffset : Integer read m_RightBtnXOffset write SetRightBtnXOffset;
        property DrawAppIcon : Boolean read m_DrawAppIcon write SetDrawAppIcon;

        property OnCustomBtnsClick : TsuiTitleBarButtonClickEvent read m_OnBtnClick write m_OnBtnClick;
        property OnHelpBtnClick : TsuiTitleBarButtonClickEvent read m_OnHelpBtnClick write m_OnHelpBtnClick;

    end;

    // ---------------- Buttons ------------------------------------
    TsuiTitleBarBtnType = (suiMax, suiMin, suiClose, suiHelp, suiControlBox, suiCustom);

    TsuiTitleBarButton = class(TCollectionItem)
    private
        m_ButtonType : TsuiTitleBarBtnType;
        m_Transparent : Boolean;
        m_Top : Integer;
        m_UIStyle : TsuiUIStyle;
        m_PicNormal : TPicture;
        m_PicMouseOn : TPicture;
        m_PicMouseDown : TPicture;
        m_ControlBoxMenu : TPopupMenu;

        procedure SetButtonType(const Value : TsuiTitleBarBtnType);
        procedure SetTransparent(const Value : Boolean);
        procedure SetTop(const Value : Integer);
        procedure SetUIStyle(const Value : TsuiUIStyle);
        procedure SetPicNormal(const Value : TPicture);
        procedure SetPicMouseOn(const Value : TPicture);
        procedure SetPicMouseDown(const Value : TPicture);

        procedure UpdateUIStyle();
        procedure ProcessMaxBtn();

    public
        procedure DoClick();

        constructor Create(Collection: TCollection); override;
        destructor Destroy; override;

    published
        property ButtonType : TsuiTitleBarBtnType read m_ButtonType write SetButtonType;
        property Transparent : Boolean read m_Transparent write SetTransparent;
        property Top : Integer read m_Top write SetTop;
        property UIStyle : TsuiUIStyle read m_UIStyle write SetUIStyle;
        property ControlBoxMenu : TPopupMenu read m_ControlBoxMenu write m_ControlBoxMenu;

        property PicNormal : TPicture read m_PicNormal write SetPicNormal;
        property PicMouseOn : TPicture read m_PicMouseOn write SetPicMouseOn;
        property PicMouseDown : TPicture read m_PicMouseDown write SetPicMouseDown;

    end;

    TsuiTitleBarButtons = class(TCollection)
    private
        m_TitleBar : TsuiTitleBar;

    protected
        function GetItem(Index: Integer): TsuiTitleBarButton;
        procedure SetItem(Index: Integer; Value: TsuiTitleBarButton);
        procedure Update(Item: TCollectionItem); override;
        function GetOwner : TPersistent; override;

    public
        function Add() : TsuiTitleBarButton;
        constructor Create(TitleBar: TsuiTitleBar);
        property Items[Index: Integer]: TsuiTitleBarButton read GetItem write SetItem;

    end;


    // ---------------- Sections ------------------------------------

    TsuiTitleBarAlign = (suiLeft, suiRight, suiClient);

    TsuiTitleBarSection = class(TCollectionItem)
    private
        m_Width : Integer;
        m_Align : TsuiTitleBarAlign;
        m_Picture : TPicture;
        m_Stretch : Boolean;
        m_AutoSize : Boolean;
        procedure SetPicture(const Value : TPicture);
        procedure SetAutoSize(const Value : Boolean);
        procedure SetWidth(const Value : Integer);
        procedure SetAlign(const Value : TsuiTitleBarAlign);
        procedure SetStretch(const Value : Boolean);

    public
        constructor Create(Collection: TCollection); override;
        destructor Destroy; override;

    published
        property AutoSize : Boolean read m_AutoSize write SetAutoSize;
        property Width : Integer read m_Width write SetWidth;
        property Align : TsuiTitleBarAlign read m_Align write SetAlign;
        property Picture : TPicture read m_Picture write SetPicture;
        property Stretch : Boolean read m_Stretch write SetStretch;

    end;

    TsuiTitleBarSections = class(TCollection)
    private
        m_TitleBar : TsuiTitleBar;

    protected
        function GetItem(Index: Integer): TsuiTitleBarSection;
        procedure SetItem(Index: Integer; Value: TsuiTitleBarSection);

        procedure Update(Item: TCollectionItem); override;
        function GetOwner : TPersistent; override;        

    public
        constructor Create(TitleBar: TsuiTitleBar);
        destructor Destroy(); override;

        function Add() : TsuiTitleBarSection;
        property Items[Index: Integer]: TsuiTitleBarSection read GetItem write SetItem;

    end;

implementation

uses SUIResDef, SUIPublic, SUIForm;

constructor TsuiTitleBar.Create(AOwner: TComponent);
var
    Btn : TsuiTitleBarButton;
begin
    inherited Create(AOwner);

    m_Sections := TsuiTitleBarSections.Create(self);
    m_Buttons := TsuiTitleBarButtons.Create(self);

    Btn := m_Buttons.Add();
    Btn.ButtonType := suiControlBox;

    Btn := m_Buttons.Add();
    Btn.ButtonType := suiClose;

    Btn := m_Buttons.Add();
    Btn.ButtonType := suiMax;

    Btn := m_Buttons.Add();
    Btn.ButtonType := suiMin;

    Align := alTop;
    ButtonInterval := 0;
    FormActive := true;

    m_MouseDown := false;
    m_InButtons := -1;
    m_LeftBtnXOffset := 0;
    m_RightBtnXOffset := 0;
    m_DrawAppIcon := false;

    Font.Color := clWhite;
    Font.Name := 'Tahoma';
    Font.Style := [fsBold]; 

    UIStyle := GetSUIFormStyle(TCustomForm(AOwner));
end;

procedure TsuiTitleBar.SetLeftBtnXOffset(const Value: Integer);
begin
    m_LeftBtnXOffset := Value;

    Repaint();
end;

procedure TsuiTitleBar.SetRightBtnXOffset(const Value: Integer);
begin
    m_RightBtnXOffset := Value;

    Repaint();
end;

procedure TsuiTitleBar.DblClick;
var
    i : Integer;
begin
    inherited;

    if m_InButtons <> -1 then
        Exit;

    for i := 0 to m_Buttons.Count - 1 do
    begin
        if m_Buttons.Items[i].ButtonType = suiMax then
        begin
            m_Buttons.Items[i].DoClick();
            Exit;
        end;
    end;
end;

destructor TsuiTitleBar.Destroy();
begin
    m_Buttons.Free();
    m_Buttons := nil;

    m_Sections.Free();
    m_Sections := nil;

    inherited Destroy();
end;

procedure TsuiTitleBar.DrawButtons(Buf: TBitmap);
var
    i : Integer;
    Btn : TsuiTitleBarButton;
    nControlBox : Integer;
    BtnRect : TRect;
    nLeft : Integer;
    nLeft2 : Integer;
    MousePoint : TPoint;
    BtnBuf : TBitmap;
    Cap : String;
    Icon : TIcon;
begin
    nLeft := Buf.Width + 1 + m_RightBtnXOffset;
    nControlBox := -1;

    BtnBuf := TBitmap.Create();

    GetCursorPos(MousePoint);
    MousePoint := ScreenToClient(MousePoint);
    m_InButtons := -1;

    for i := 0 to m_Buttons.Count - 1 do
    begin
        Btn := m_Buttons.Items[i];

        BtnBuf.Width := Btn.PicNormal.Width;
        BtnBuf.Height := Btn.PicNormal.Height;
        BtnBuf.Transparent := Btn.Transparent;

        if Btn.ButtonType = suiControlBox then
        begin
            nControlBox := i;
            continue;
        end;

        Dec(nLeft, Btn.PicNormal.Width + 1 + m_ButtonInterval);

        BtnRect := Rect(nLeft, Btn.Top, nLeft + Btn.PicNormal.Width, Btn.Top + Btn.PicNormal.Height);
        if InRect(MousePoint, BtnRect) then
        begin
            m_InButtons := i;

            if m_MouseDown then
            begin
                if Btn.PicMouseDown <> nil then
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseDown.Graphic)
            end
            else
            begin
                if Btn.PicMouseOn <> nil then
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseOn.Graphic);
            end;
        end
        else
            BtnBuf.Canvas.Draw(0, 0, Btn.PicNormal.Graphic);

        Buf.Canvas.Draw(nLeft, Btn.Top, BtnBuf);
    end;

    nLeft2 := 0 + m_LeftBtnXOffset;

    if nControlBox <> -1 then
    begin
        Btn := m_Buttons.Items[nControlBox];

        BtnRect := Rect(nLeft2, Btn.Top, nLeft2 + Btn.PicNormal.Width, Btn.Top + Btn.PicNormal.Height);

        if not m_DrawAppIcon then
        begin
            if InRect(MousePoint, BtnRect) then
            begin
                m_InButtons := nControlBox;

                if m_MouseDown then
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseDown.Graphic)
                else
                    BtnBuf.Canvas.Draw(0, 0, Btn.PicMouseOn.Graphic);
            end
            else
                BtnBuf.Canvas.Draw(0, 0, Btn.PicNormal.Graphic);
            Buf.Canvas.Draw(nLeft2, Btn.Top, BtnBuf);
            Inc(nLeft2, Btn.PicNormal.Width + 5);
        end
        else
        begin
            if InRect(MousePoint, BtnRect) then
                m_InButtons := nControlBox;

            BtnBuf.Height := 16;
            BtnBuf.Width := 16;
            Icon := TIcon.Create();
            Icon.Handle := LoadImage(hInstance, 'MAINICON', IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
            Buf.Canvas.Draw(nLeft2 + 3, Btn.Top, Icon);
            Icon.Free();
            Inc(nLeft2, 21);
        end;
    end;

    if nLeft2 = 0 then
        Inc(nLeft2, 6);

    if not m_Active then
        Buf.Canvas.Font.Color := clInactiveCaption
    else if m_UIStyle = MacOS then
        Buf.Canvas.Font.Color := clBlack;


⌨️ 快捷键说明

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