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

📄 suititlebar.pas

📁 SUIPack是一款为Delphi和C++Builder开发的所见即所得的界面增强VCL组件
💻 PAS
📖 第 1 页 / 共 4 页
字号:
///////////////////////////////////////////////////////////////////////////////////
//
//
//  FileName    :   SUITitleBar.pas
//  Creater     :   Shen Min
//  Date        :   2001-10-15 V1-V3
//                  2003-06-15 V4
//  Comment     :
//
//  Copyright (c) 2002-2006 Sunisoft
//  http://www.sunisoft.com
//  Email: support@sunisoft.com
//
////////////////////////////////////////////////////////////////////////////////////

unit SUITitleBar;

interface

{$I SUIPack.inc}

uses Windows, Controls, Classes, Forms, Messages, Graphics, Menus, ExtCtrls,
     Dialogs, SysUtils, Math,
     SUI2Define, SUIThemes, SUIMgr, SUIPopupMenu;


type
    TsuiTitleBarSections = class;
    TsuiTitleBarButtons = class;
    TsuiTitleBar = class;

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

    TsuiTitleBarPopupMenu = class(TsuiPopupMenu)
    private
        m_TitleBar : TsuiTitleBar;
        
        procedure OnMin(Sender: TObject);
        procedure OnMax(Sender: TObject);
        procedure OnClose(Sender: TObject);

    public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy(); override;
        procedure Popup(X, Y: Integer); override;

        property TitleBar : TsuiTitleBar read m_TitleBar write m_TitleBar;
    end;


    TsuiTitleBar = class(TCustomPanel)
    private
        m_UIStyle : TsuiUIStyle;
        m_FileTheme : TsuiFileTheme;        
        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_RoundCornerBottom : Integer;
        m_DrawAppIcon : Boolean;
        m_SelfChanging : Boolean;
        m_DefPopupMenu : TsuiTitleBarPopupMenu;
        m_BorderColor : TColor;
        m_Custom : Boolean;
        m_FiveSec : Boolean;
        m_DrawingCap : String;
        m_Buf : TBitmap;
        m_Buf2 : TBitmap;
        m_FormColor : TColor;
        m_TransColor : TColor;
        m_InactiveCaptionColor : TColor;        

        m_MouseDown : Boolean;
        m_InButtons : Integer;
        m_BtnHeight : Integer;
        m_BtnTop : Integer;
        m_IconTop : Integer;
        m_CapTop : Integer;

        m_OnBtnClick : TsuiTitleBarButtonClickEvent;
        m_OnHelpBtnClick : TsuiTitleBarButtonClickEvent;

        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 SetAutoSize2(Value : Boolean);
        procedure SetLeftBtnXOffset(const Value: Integer);
        procedure SetRightBtnXOffset(const Value: Integer);
        procedure SetDrawAppIcon(const Value: Boolean);
        procedure SetRoundCorner(const Value: Integer);
        procedure SetRoundCornerBottom(const Value: Integer);
        procedure SetFileTheme(const Value: TsuiFileTheme);
        procedure SetInactiveCaptionColor(const Value: TColor);        

        procedure UpdateInsideTheme(UIStyle : TsuiUIStyle);
        procedure UpdateFileTheme();

        procedure WMERASEBKGND(var Msg : TMessage); message WM_ERASEBKGND;
        procedure CMFONTCHANGED(var Msg : TMessage); message CM_FONTCHANGED;
        procedure WMNCLBUTTONDOWN(var Msg: TMessage); message WM_NCLBUTTONDOWN;
        procedure WMNCLBUTTONUP(var Msg: TMessage); message WM_NCLBUTTONUP;
        procedure WMNCMOUSEMOVE(var Msg: TMessage); message WM_NCMOUSEMOVE;
        procedure CMDesignHitTest(var Msg: TCMDesignHitTest); message CM_DESIGNHITTEST;

    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 Notification(AComponent: TComponent; Operation: TOperation); override;        

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

        function InForm() : Boolean;
        function InMDIForm() : Boolean;

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

        procedure ProcessMaxBtn();
        procedure GetTitleImage(index : Integer; out Bmp : TBitmap);
        function CanPaint() : Boolean;
        procedure ForcePaint();

        property DefPopupMenu : TsuiTitleBarPopupMenu read m_DefPopupMenu;

    published
        property Custom : Boolean read m_Custom write m_Custom;
        property FileTheme : TsuiFileTheme read m_FileTheme write SetFileTheme;
        property UIStyle : TsuiUIStyle read m_UIStyle write SetUIStyle;
        property AutoSize read m_AutoSize write SetAutoSize2;
        property BiDiMode;
        property Height;
        property Sections : TsuiTitleBarSections read m_Sections write SetSections;
        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 RoundCorner : Integer read m_RoundCorner write SetRoundCorner;
        property RoundCornerBottom : Integer read m_RoundCornerBottom write SetRoundCornerBottom;
        property InactiveCaptionColor : TColor read m_InactiveCaptionColor write SetInactiveCaptionColor;
        
        property OnCustomBtnsClick : TsuiTitleBarButtonClickEvent read m_OnBtnClick write m_OnBtnClick;
        property OnHelpBtnClick : TsuiTitleBarButtonClickEvent read m_OnHelpBtnClick write m_OnHelpBtnClick;
        property OnResize;
    end;

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

    TsuiTitleBarButton = class(TCollectionItem)
    private
        m_Visible : Boolean;
        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 UpdateInsideTheme(UIStyle : TsuiUIStyle);
        procedure UpdateFileTheme();

        procedure ProcessMaxBtn();
        procedure SetVisible(const Value: Boolean);

    public
        procedure DoClick();

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

        procedure Assign(Source: TPersistent); override;        

    published
        property UIStyle : TsuiUIStyle read m_UIStyle write SetUIStyle;
        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 ControlBoxMenu : TPopupMenu read m_ControlBoxMenu write m_ControlBoxMenu;
        property Visible :Boolean read m_Visible write SetVisible;

        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;
        m_CapSec : 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);
        procedure SetCapSec(const Value: Boolean);

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

        procedure Assign(Source: TPersistent); 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;
        property CapSec : Boolean read m_CapSec write SetCapSec;

    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;

const
    SUIM_GETBORDERWIDTH     = WM_USER + 8899;

implementation

uses SUIResDef, SUIPublic, SUIForm;

{ TsuiTitleBarPopupMenu }

constructor TsuiTitleBarPopupMenu.Create(AOwner: TComponent);
var
    MenuItem : TMenuItem;
begin
    inherited;

    Self.AutoHotkeys := maManual;

    MenuItem := TMenuItem.Create(nil);
    MenuItem.Caption := SUI_TITLE_MENUITEM_MINIMIZE;
    MenuItem.OnClick := OnMin;
    Items.Add(MenuItem);

    MenuItem := TMenuItem.Create(nil);
    MenuItem.Caption := SUI_TITLE_MENUITEM_MAXIMIZE;
    MenuItem.OnClick := OnMax;
    Items.Add(MenuItem);

    MenuItem := TMenuItem.Create(nil);
    MenuItem.Caption := '-';
    Items.Add(MenuItem);

    MenuItem := TMenuItem.Create(nil);
    MenuItem.Caption := SUI_TITLE_MENUITEM_CLOSE;
    MenuItem.OnClick := OnClose;
    Items.Add(MenuItem);

    MenuAdded();
end;

destructor TsuiTitleBarPopupMenu.Destroy;
begin

    inherited;
end;

procedure TsuiTitleBarPopupMenu.OnClose(Sender: TObject);
var
    ParentForm : TCustomForm;
begin
    ParentForm := GetParentForm(m_TitleBar);
    if ParentForm <> nil then
        ParentForm.Close(); 
end;

procedure TsuiTitleBarPopupMenu.OnMax(Sender: TObject);
var
    i : Integer;
begin
    for i := 0 to m_TitleBar.m_Buttons.Count - 1 do
    begin
        if (m_TitleBar.m_Buttons.Items[i].ButtonType = suiMax) and
            m_TitleBar.m_Buttons.Items[i].Visible then
            m_TitleBar.m_Buttons.Items[i].DoClick();
    end;
end;

procedure TsuiTitleBarPopupMenu.OnMin(Sender: TObject);
var
    ParentForm : TCustomForm;
begin
    ParentForm := GetParentForm(m_TitleBar);
    if (ParentForm = nil) or (Application = nil) then
        Exit;
    if ParentForm = Application.MainForm then
        SendMessage(Application.MainForm.Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0)
    else
//        ShowWindow(ParentForm.Handle, SW_SHOWMINIMIZED);
        ParentForm.WindowState := wsMinimized;
end;

procedure TsuiTitleBarPopupMenu.Popup(X, Y: Integer);
var
    i : Integer;
    MinItem, MaxItem, CloseItem : Boolean;
begin
    MinItem := false;
    MaxItem := false;
    CloseItem := false;
    if (m_TitleBar.BidiMode = bdRightToLeft) and SysLocale.MiddleEast then
        Inc(X, 176);

    for i := 0 to m_TitleBar.m_Buttons.Count - 1 do
    begin
        if (m_TitleBar.m_Buttons.Items[i].ButtonType = suiMin) and
            m_TitleBar.m_Buttons.Items[i].Visible then
            MinItem := true;
        if (m_TitleBar.m_Buttons.Items[i].ButtonType = suiMax) and
            m_TitleBar.m_Buttons.Items[i].Visible then
            MaxItem := true;
        if (m_TitleBar.m_Buttons.Items[i].ButtonType = suiClose) and
            m_TitleBar.m_Buttons.Items[i].Visible then
            CloseItem := true;
    end;

    Items[0].Enabled := MinItem;
    Items[1].Enabled := MaxItem;
    Items[3].Enabled := CloseItem;

    inherited;
end;

{ TsuiTitleBar }

constructor TsuiTitleBar.Create(AOwner: TComponent);
var
    Btn : TsuiTitleBarButton;
begin
    inherited Create(AOwner);
    ControlStyle := ControlStyle - [csAcceptsControls];
    ParentBidiMode := true;
    m_Custom := false;
    m_FiveSec := false;
    m_DrawingCap := '';
    m_Buf := TBitmap.Create();
    m_Buf.PixelFormat := pf24Bit;
    m_Buf2 := TBitmap.Create();
    m_Buf2.PixelFormat := pf24Bit;

    m_InactiveCaptionColor := clInactiveCaption;
    
    m_DefPopupMenu := TsuiTitleBarPopupMenu.Create(self);
    m_DefPopupMenu.TitleBar := self;

    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 := true;
    m_SelfChanging := false;

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

    UIStyle := GetSUIFormStyle(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;

⌨️ 快捷键说明

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