2 Star 1 Fork 0

wyrover/FlatStyle

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
FlatBtns.pas 50.12 KB
一键复制 编辑 原始数据 按行查看 历史
wyrover 提交于 2015-06-17 17:38 . + Init FlatStyle
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
unit FlatBtns;
interface
{$I FlatStyle.inc}
uses Windows, Messages, Classes, SysUtils, Controls, Forms, Graphics,
ExtCtrls, FlatUtils;
const
InitRepeatPause = 400; // pause before repeat timer (ms)
RepeatPause = 100; // pause before hint window displays (ms)
type
{ TDefineSpeed }
TDefineSpeed = class(TVersionGraphic)
private
FOnMouseEnter: TNotifyEvent;
FOnMouseLeave: TNotifyEvent;
FTransparent: TTransparentMode;
TextBounds: TRect;
GlyphPos: TPoint;
FNumGlyphs: TNumGlyphs;
fColorDown: TColor;
FColorBorder: TColor;
FColorShadow: TColor;
fColorFocused: TColor;
FGroupIndex: Integer;
FGlyph: TBitmap;
FDown: Boolean;
FDragging: Boolean;
FAllowAllUp: Boolean;
FLayout: TButtonLayout;
FSpacing: Integer;
FMargin: Integer;
FMouseIn: Boolean;
FModalResult: TModalResult;
fColorFlat: TColor;
procedure SetColors(Index: Integer; Value: TColor);
procedure UpdateExclusive;
procedure SetGlyph(Value: TBitmap);
procedure SetNumGlyphs(Value: TNumGlyphs);
procedure SetDown(Value: Boolean);
procedure SetAllowAllUp(Value: Boolean);
procedure SetGroupIndex(Value: Integer);
procedure SetLayout(Value: TButtonLayout);
procedure SetSpacing(Value: Integer);
procedure SetMargin(Value: Integer);
procedure UpdateTracking;
procedure SetTransparent (const Value: TTransparentMode);
protected
FState: TButtonState;
function GetPalette: HPALETTE; override;
procedure Loaded; override;
procedure WMLButtonDblClk(var Message: TWMLButtonDown); message WM_LBUTTONDBLCLK;
procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
procedure CMButtonPressed(var Message: TMessage); message CM_BUTTONPRESSED;
procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure CMSysColorChange(var Message: TMessage); message CM_SYSCOLORCHANGE;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure CMParentColorChanged(var Message: TWMNoParams); message CM_PARENTCOLORCHANGED;
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 Paint; override;
property Transparent: TTransparentMode read FTransparent write SetTransparent default tmNone;
property AllowAllUp: Boolean read FAllowAllUp write SetAllowAllUp default False;
property Color default DefaultFlatColor;
property ColorFocused: TColor index 0 read fColorFocused write SetColors default DefaultFocusedColor;
property ColorDown: TColor index 1 read fColorDown write SetColors default DefaultDownColor;
property ColorBorder: TColor index 2 read FColorBorder write SetColors default DefaultBorderColor;
property ColorShadow: TColor index 3 read FColorShadow write SetColors default DefaultShadowColor;
property ColorFlat: TColor index 4 read fColorFlat write SetColors default DefaultFlatColor;
property GroupIndex: Integer read FGroupIndex write SetGroupIndex default 0;
property Down: Boolean read FDown write SetDown default False;
property Glyph: TBitmap read FGlyph write SetGlyph;
property Layout: TButtonLayout read FLayout write SetLayout default blGlyphTop;
property Margin: Integer read FMargin write SetMargin default -1;
property NumGlyphs: TNumGlyphs read FNumGlyphs write SetNumGlyphs default 1;
property ModalResult: TModalResult read FModalResult write FModalResult default 0;
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
property Spacing: Integer read FSpacing write SetSpacing default 4;
{$IFDEF DFS_DELPHI_4_UP}
procedure ActionChange(Sender: TObject; CheckDefaults: Boolean); override;
{$ENDIF}
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Click; override;
procedure MouseEnter;
procedure MouseLeave;
end;
{ TTimeBtnState }
TTimeBtnState = set of (tbFocusRect, tbAllowTimer);
{ TDefineTimer }
TDefineTimer = class(TDefineSpeed)
private
FRepeatTimer: TTimer;
FTimeBtnState: TTimeBtnState;
procedure TimerExpired( Sender: TObject);
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
property Cursor default crHandPoint;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property TimeBtnState: TTimeBtnState read FTimeBtnState write FTimeBtnState;
end;
{ TDefineSpin }
TDefineSpin = class(TWinControl)
private
FUpButton: TDefineTimer;
FDownButton: TDefineTimer;
FFocusedButton: TDefineTimer;
FFocusControl: TWinControl;
FOnUpClick: TNotifyEvent;
FOnDownClick: TNotifyEvent;
function CreateButton: TDefineTimer;
function GetUpGlyph: TBitmap;
function GetDownGlyph: TBitmap;
procedure SetUpGlyph(Value: TBitmap);
procedure SetDownGlyph(Value: TBitmap);
function GetUpNumGlyphs: TNumGlyphs;
function GetDownNumGlyphs: TNumGlyphs;
procedure SetUpNumGlyphs(Value: TNumGlyphs);
procedure SetDownNumGlyphs(Value: TNumGlyphs);
procedure BtnClick(Sender: TObject);
procedure BtnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure SetFocusBtn (Btn: TDefineTimer);
procedure AdjustSize(var W, H: Integer); reintroduce;// {$IFDEF DFS_COMPILER_4_UP} reintroduce; {$ENDIF}
procedure WMSize(var Message: TWMSize); message WM_SIZE;
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
protected
procedure Loaded; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure Notification (AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
procedure SetBounds (ALeft, ATop, AWidth, AHeight: Integer); override;
published
property DownGlyph: TBitmap read GetDownGlyph write SetDownGlyph;
property DownNumGlyphs: TNumGlyphs read GetDownNumGlyphs write SetDownNumGlyphs default 1;
property FocusControl: TWinControl read FFocusControl write FFocusControl;
property UpGlyph: TBitmap read GetUpGlyph write SetUpGlyph;
property UpNumGlyphs: TNumGlyphs read GetUpNumGlyphs write SetUpNumGlyphs default 1;
property Enabled;
property Visible;
property OnDownClick: TNotifyEvent read FOnDownClick write FOnDownClick;
property OnUpClick: TNotifyEvent read FOnUpClick write FOnUpClick;
end;
{ TDefineButton }
TDefineButton = class(TVersionControl)
private
FOnMouseEnter: TNotifyEvent;
FOnMouseLeave: TNotifyEvent;
FTransparent: TTransparentMode;
FModalResult: TModalResult;
TextBounds: TRect;
GlyphPos: TPoint;
FNumGlyphs: TNumGlyphs;
fColorDown: TColor;
FColorBorder: TColor;
FColorShadow: TColor;
fColorFocused: TColor;
FGroupIndex: Integer;
FGlyph: TBitmap;
FDown: Boolean;
FDragging: Boolean;
FAllowAllUp: Boolean;
FLayout: TButtonLayout;
FSpacing: Integer;
FMargin: Integer;
FMouseIn: Boolean;
FDefault: Boolean;
fHasFocusFrame: boolean;
fColorFlat: TColor;
procedure SetColors(Index: Integer; Value: TColor);
procedure UpdateExclusive;
procedure SetGlyph(Value: TBitmap);
procedure SetNumGlyphs(Value: TNumGlyphs);
procedure SetDown(Value: Boolean);
procedure SetAllowAllUp(Value: Boolean);
procedure SetGroupIndex(Value: Integer);
procedure SetLayout(Value: TButtonLayout);
procedure SetSpacing(Value: Integer);
procedure SetMargin(Value: Integer);
procedure UpdateTracking;
procedure SetDefault(const Value: Boolean);
procedure SetTransparent (const Value: TTransparentMode);
protected
FState: TButtonState;
function GetPalette: HPALETTE; override;
procedure Loaded; override;
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
procedure WMKeyUp(var Message: TWMKeyUp); message WM_KEYUP;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
procedure WMMove(var Message: TWMMove); message WM_MOVE;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure WMLButtonDblClk(var Message: TWMLButtonDown); message WM_LBUTTONDBLCLK;
procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
procedure CMButtonPressed(var Message: TMessage); message CM_BUTTONPRESSED;
procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;
procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure CMSysColorChange(var Message: TMessage); message CM_SYSCOLORCHANGE;
procedure CMParentColorChanged(var Message: TWMNoParams); message CM_PARENTCOLORCHANGED;
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 Paint; override;
procedure SetName(const Value: TComponentName); override;
property Transparent: TTransparentMode read FTransparent write SetTransparent default tmNone;
property HasFocusFrame:boolean read fHasFocusFrame write fHasFocusFrame default true;
property Default: Boolean read FDefault write SetDefault default False;
property AllowAllUp: Boolean read FAllowAllUp write SetAllowAllUp default False;
property ColorFocused: TColor index 0 read fColorFocused write SetColors default DefaultFocusedColor;
property ColorDown: TColor index 1 read fColorDown write SetColors default DefaultDownColor;
property ColorBorder: TColor index 2 read FColorBorder write SetColors default DefaultBorderColor;
property ColorShadow: TColor index 3 read FColorShadow write SetColors default DefaultShadowColor;
property ColorFlat: TColor index 4 read fColorFlat write SetColors default DefaultFlatColor;
property GroupIndex: Integer read FGroupIndex write SetGroupIndex default 0;
property Down: Boolean read FDown write SetDown default False;
property Glyph: TBitmap read FGlyph write SetGlyph;
property Layout: TButtonLayout read FLayout write SetLayout default blGlyphTop;
property Margin: Integer read FMargin write SetMargin default -1;
property NumGlyphs: TNumGlyphs read FNumGlyphs write SetNumGlyphs default 1;
property TabStop default true;
property Spacing: Integer read FSpacing write SetSpacing default 4;
property ModalResult: TModalResult read FModalResult write FModalResult default 0;
property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Click; override;
procedure MouseEnter;
procedure MouseLeave;
end;
{ TFlatSpeedButton }
TFlatSpeedButton = class(TDefineSpeed)
published
property Transparent;
property Version;
property AllowAllUp;
property ColorFocused;
property ColorDown;
property ColorBorder;
property ColorShadow;
property ColorFlat;
property GroupIndex;
property Down;
property Caption;
property Enabled;
property Font;
property Glyph;
property Layout;
property Margin;
property NumGlyphs;
property ModalResult;
property ParentFont;
property ParentColor;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Spacing;
property Visible;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnMouseEnter;
property OnMouseLeave;
{$IFDEF DFS_DELPHI_4_UP}
property Action;
property Anchors;
property BiDiMode;
property Constraints;
property DragKind;
property ParentBiDiMode;
property OnEndDock;
property OnStartDock;
{$ENDIF}
end;
{ TFlatButton }
TFlatButton = class(TDefineButton)
published
property Transparent;
property HasFocusFrame;
property Default;
property AllowAllUp;
property ColorFocused;
property ColorDown;
property ColorBorder;
property ColorShadow;
property ColorFlat;
property GroupIndex;
property Action;
property Down;
property Caption;
property Enabled;
property Font;
property Glyph;
property Layout;
property Margin;
property NumGlyphs;
property ParentFont;
property ParentColor;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabStop;
property TabOrder;
property Spacing;
property ModalResult;
property Visible;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnMouseEnter;
property OnMouseLeave;
{$IFDEF DFS_DELPHI_4_UP}
property Anchors;
property BiDiMode;
property Constraints;
property DragKind;
property ParentBiDiMode;
property OnEndDock;
property OnStartDock;
{$ENDIF}
end;
implementation
{$R FlatArrow.res}
{ TDefineSpeed }
constructor TDefineSpeed.Create(AOwner: TComponent);
begin
FGlyph := TBitmap.Create;
inherited Create(AOwner);
ControlStyle := [csCaptureMouse, csDoubleClicks];
ParentFont := True;
ParentColor := True;
fColorFocused := DefaultFocusedColor;
fColorDown := DefaultDownColor;
FColorBorder := DefaultBorderColor;
FColorShadow := DefaultShadowColor;
FState := bsUp;
fColorFlat := DefaultFlatColor;
FSpacing := 4;
FMargin := -1;
FNumGlyphs := 1;
FLayout := blGlyphTop;
FModalResult := mrNone;
FTransparent := tmNone;
SetBounds(0, 0, 25, 25);
end;
destructor TDefineSpeed.Destroy;
begin
FGlyph.Free;
inherited Destroy;
end;
procedure TDefineSpeed.Paint;
var
FTransColor: TColor;
FImageList: TImageList;
sourceRect, destRect: TRect;
tempGlyph: TBitmap;
Offset: TPoint;
begin
// get the transparent color
FTransColor := FGlyph.Canvas.Pixels[0, FGlyph.Height - 1];
Canvas.Font := Self.Font;
if FState in [bsDown, bsExclusive] then
Offset := Point(1, 1)
else
Offset := Point(0, 0);
CalcButtonLayout(Canvas, ClientRect, Offset, FLayout, FSpacing,
FMargin, FGlyph, FNumGlyphs, Caption, TextBounds, GlyphPos);
if not Enabled then
begin
FState := bsDisabled;
FDragging := False;
end
else
if FState = bsDisabled then
if FDown and (GroupIndex <> 0) then
FState := bsExclusive
else
FState := bsUp;
// DrawBackground
case FTransparent of
tmAlways:;
tmNone:
begin
case FState of
bsUp:
if FMouseIn then
Canvas.Brush.Color := fColorFocused
else
Canvas.Brush.Color := fColorFlat;
bsDown:
Canvas.Brush.Color := fColorDown;
bsExclusive:
if FMouseIn then
Canvas.Brush.Color := fColorFocused
else
Canvas.Brush.Color := fColorDown;
bsDisabled:
Canvas.Brush.Color := fColorFlat;
end;
Canvas.FillRect(ClientRect);
end;
tmNotFocused:
if FMouseIn then
begin
case FState of
bsUp:
if FMouseIn then
Canvas.Brush.Color := fColorFocused
else
Canvas.Brush.Color := Self.Color;
bsDown:
Canvas.Brush.Color := fColorDown;
bsExclusive:
if FMouseIn then
Canvas.Brush.Color := fColorFocused
else
Canvas.Brush.Color := fColorDown;
bsDisabled:
Canvas.Brush.Color := Self.Color;
end;
Canvas.FillRect(ClientRect);
end;
end;
// DrawBorder
case FState of
bsUp:
if FMouseIn then
DrawButtonBorder(canvas, ClientRect, FColorShadow, 1)
else
DrawButtonBorder(canvas, ClientRect, FColorBorder, 1);
bsDown, bsExclusive:
DrawButtonBorder(canvas, ClientRect, FColorShadow, 1);
bsDisabled:
DrawButtonBorder(canvas, ClientRect, FColorBorder, 1);
end;
// DrawGlyph
if not FGlyph.Empty then
begin
tempGlyph := TBitmap.Create;
case FNumGlyphs of
1: case FState of
bsUp: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
bsDisabled: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
bsDown: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
bsExclusive: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
end;
2: case FState of
bsUp: sourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsDisabled: sourceRect := Rect(FGlyph.Width div FNumGlyphs, 0, FGlyph.Width, FGlyph.Height);
bsDown: sourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsExclusive: sourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
end;
3: case FState of
bsUp: SourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsDisabled: SourceRect := Rect(FGlyph.width div FNumGlyphs, 0, (FGlyph.Width div FNumGlyphs) * 2, FGlyph.Height);
bsDown: SourceRect := Rect((FGlyph.Width div FNumGlyphs) * 2, 0, FGlyph.Width, FGlyph.Height);
bsExclusive: SourceRect := Rect((FGlyph.Width div FNumGlyphs) * 2, 0, FGlyph.Width, FGlyph.Height);
end;
4: case FState of
bsUp: SourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsDisabled: SourceRect := Rect(FGlyph.width div FNumGlyphs, 0, (FGlyph.Width div FNumGlyphs) * 2, FGlyph.Height);
bsDown: SourceRect := Rect((FGlyph.Width div FNumGlyphs) * 2, 0, (FGlyph.Width div FNumGlyphs) * 3, FGlyph.Height);
bsExclusive: SourceRect := Rect((FGlyph.width div FNumGlyphs) * 3, 0, FGlyph.Width, FGlyph.Height);
end;
end;
destRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
tempGlyph.Width := FGlyph.Width div FNumGlyphs;
tempGlyph.Height := FGlyph.Height;
tempGlyph.canvas.copyRect(destRect, FGlyph.canvas, sourcerect);
if (FNumGlyphs = 1) and (FState = bsDisabled) then
begin
tempGlyph := CreateDisabledBitmap(tempGlyph, clBlack, clBtnFace, clBtnHighlight, clBtnShadow, True);
FTransColor := tempGlyph.Canvas.Pixels[0, tempGlyph.Height - 1];
end;
FImageList := TImageList.CreateSize(FGlyph.Width div FNumGlyphs, FGlyph.Height);
try
FImageList.AddMasked(tempGlyph, FTransColor);
FImageList.Draw(canvas, glyphpos.x, glyphpos.y, 0);
finally
FImageList.Free;
end;
tempGlyph.free;
end;
// DrawText
Canvas.Brush.Style := bsClear;
if FState = bsDisabled then
begin
OffsetRect(TextBounds, 1, 1);
Canvas.Font.Color := clBtnHighlight;
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
OffsetRect(TextBounds, -1, -1);
Canvas.Font.Color := clBtnShadow;
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
end
else
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
end;
procedure TDefineSpeed.UpdateTracking;
var
P: TPoint;
begin
if Enabled then
begin
GetCursorPos(P);
FMouseIn := not (FindDragTarget(P, True) = Self);
if FMouseIn then
MouseLeave
else
MouseEnter;
end;
end;
procedure TDefineSpeed.Loaded;
begin
inherited Loaded;
Invalidate;
end;
procedure TDefineSpeed.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if(Button = mbLeft) and Enabled then
begin
if not FDown then
begin
FState := bsDown;
Invalidate;
end;
FDragging := True;
end;
end;
procedure TDefineSpeed.MouseMove (Shift: TShiftState; X, Y: Integer);
var
NewState: TButtonState;
begin
inherited;
if FDragging then
begin
if not FDown then
NewState := bsUp
else
NewState := bsExclusive;
if (X >= 0) and (X < ClientWidth) and (Y >= 0) and (Y <= ClientHeight) then
if FDown then
NewState := bsExclusive
else
NewState := bsDown;
if NewState <> FState then
begin
FState := NewState;
Invalidate;
end;
end;
end;
procedure TDefineSpeed.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
DoClick: Boolean;
begin
inherited MouseUp(Button, Shift, X, Y);
if FDragging then
begin
FDragging := False;
DoClick := (X >= 0) and (X < ClientWidth) and (Y >= 0) and (Y <= ClientHeight);
if FGroupIndex = 0 then
begin
// Redraw face in-case mouse is captured
FState := bsUp;
FMouseIn := False;
if DoClick and not (FState in [bsExclusive, bsDown]) then
Invalidate;
end
else
if DoClick then
begin
SetDown(not FDown);
if FDown then Repaint;
end
else
begin
if FDown then FState := bsExclusive;
Repaint;
end;
if DoClick then Click else MouseLeave;
UpdateTracking;
end;
end;
procedure TDefineSpeed.Click;
begin
if Parent <> nil then
GetParentForm(self).ModalResult := FModalResult;
if Assigned(PopupMenu) then
PopupMenu.PopUp(ClientToScreen(Point(0, Height)).X,
ClientToScreen(Point(0, Height)).Y);
inherited Click;
end;
function TDefineSpeed.GetPalette: HPALETTE;
begin
Result := FGlyph.Palette;
end;
procedure TDefineSpeed.SetColors(Index: Integer; Value: TColor);
begin
case Index of
0: fColorFocused := Value;
1: fColorDown := Value;
2: FColorBorder := Value;
3: FColorShadow := Value;
4: FColorFlat := Value;
end;
Invalidate;
end;
procedure TDefineSpeed.SetGlyph(Value: TBitmap);
begin
if value <> FGlyph then
begin
FGlyph.Assign(value);
if not FGlyph.Empty then
begin
if FGlyph.Width mod FGlyph.Height = 0 then
begin
FNumGlyphs := FGlyph.Width div FGlyph.Height;
if FNumGlyphs > 4 then FNumGlyphs := 1;
end;
end;
Invalidate;
end;
end;
procedure TDefineSpeed.SetNumGlyphs(Value: TNumGlyphs);
begin
if value <> FNumGlyphs then
begin
FNumGlyphs := value;
Invalidate;
end;
end;
procedure TDefineSpeed.UpdateExclusive;
var
Msg: TMessage;
begin
if (FGroupIndex <> 0) and (Parent <> nil) then
begin
Msg.Msg := CM_BUTTONPRESSED;
Msg.WParam := FGroupIndex;
Msg.LParam := Longint(Self);
Msg.Result := 0;
Parent.Broadcast(Msg);
end;
end;
procedure TDefineSpeed.SetDown(Value: Boolean);
begin
if FGroupIndex = 0 then Value := False;
if Value <> FDown then
begin
if FDown and (not FAllowAllUp) then Exit;
FDown := Value;
if Value then
begin
if FState = bsUp then Invalidate;
FState := bsExclusive
end
else
begin
FState := bsUp;
Repaint;
end;
if Value then UpdateExclusive;
end;
end;
procedure TDefineSpeed.SetGroupIndex(Value: Integer);
begin
if FGroupIndex <> Value then
begin
FGroupIndex := Value;
UpdateExclusive;
end;
end;
procedure TDefineSpeed.SetLayout(Value: TButtonLayout);
begin
if FLayout <> Value then
begin
FLayout := Value;
Invalidate;
end;
end;
procedure TDefineSpeed.SetMargin(Value: Integer);
begin
if(Value <> FMargin) and(Value >= -1) then
begin
FMargin := Value;
Invalidate;
end;
end;
procedure TDefineSpeed.SetSpacing(Value: Integer);
begin
if Value <> FSpacing then
begin
FSpacing := Value;
Invalidate;
end;
end;
procedure TDefineSpeed.SetAllowAllUp(Value: Boolean);
begin
if FAllowAllUp <> Value then
begin
FAllowAllUp := Value;
UpdateExclusive;
end;
end;
procedure TDefineSpeed.WMLButtonDblClk(var Message: TWMLButtonDown);
begin
inherited;
if FDown then DblClick;
end;
procedure TDefineSpeed.CMEnabledChanged(var Message: TMessage);
begin
inherited;
if not Enabled then
begin
FMouseIn := False;
FState := bsDisabled;
//RemoveMouseTimer;
end;
UpdateTracking;
Invalidate;
end;
procedure TDefineSpeed.CMButtonPressed(var Message: TMessage);
var
Sender: TDefineSpeed;
begin
if Message.WParam = FGroupIndex then
begin
Sender := TDefineSpeed(Message.LParam);
if Sender <> Self then
begin
if Sender.Down and FDown then
begin
FDown := False;
FState := bsUp;
Invalidate;
end;
FAllowAllUp := Sender.AllowAllUp;
end;
end;
end;
procedure TDefineSpeed.CMDialogChar(var Message: TCMDialogChar);
begin
with Message do
if IsAccel(CharCode, Caption) and Enabled then
begin
Click;
Result := 1;
end else
inherited;
end;
procedure TDefineSpeed.CMFontChanged(var Message: TMessage);
begin
Invalidate;
end;
procedure TDefineSpeed.CMTextChanged(var Message: TMessage);
begin
Invalidate;
end;
procedure TDefineSpeed.CMSysColorChange(var Message: TMessage);
begin
inherited;
if (Parent <> nil)and(ParentColor) then
Color := TDefineSpeed(Parent).Color;
Invalidate;
end;
procedure TDefineSpeed.CMParentColorChanged(var Message: TWMNoParams);
begin
inherited;
if (Parent <> nil)and(not ParentColor) then
Color := TDefineSpeed(Parent).Color;
Invalidate;
end;
procedure TDefineSpeed.MouseEnter;
begin
if Enabled and not FMouseIn then
begin
FMouseIn := True;
Invalidate;
end;
end;
procedure TDefineSpeed.MouseLeave;
begin
if Enabled and FMouseIn and not FDragging then
begin
FMouseIn := False;
Invalidate;
end;
end;
{$IFDEF DFS_DELPHI_4_UP}
procedure TDefineSpeed.ActionChange(Sender: TObject; CheckDefaults: Boolean);
procedure CopyImage(ImageList: TCustomImageList; Index: Integer);
begin
with Glyph do
begin
Width := ImageList.Width;
Height := ImageList.Height;
Canvas.Brush.Color := clFuchsia;//! for lack of a better color
Canvas.FillRect(Rect(0,0, Width, Height));
ImageList.Draw(Canvas, 0, 0, Index);
end;
end;
begin
inherited ActionChange(Sender, CheckDefaults);
if Sender is TCustomAction then
with TCustomAction(Sender) do
begin
{ Copy image from action's imagelist }
if (Glyph.Empty) and (ActionList <> nil) and (ActionList.Images <> nil) and
(ImageIndex >= 0) and (ImageIndex < ActionList.Images.Count) then
CopyImage(ActionList.Images, ImageIndex);
end;
end;
{$ENDIF}
procedure TDefineSpeed.SetTransparent(const Value: TTransparentMode);
begin
FTransparent := Value;
Invalidate;
end;
procedure TDefineSpeed.CMMouseEnter(var Message: TMessage);
begin
inherited;
if Assigned(FOnMouseEnter) then
FOnMouseEnter(Self)
else if not(csDesigning in ComponentState) then
MouseEnter;
end;
procedure TDefineSpeed.CMMouseLeave(var Message: TMessage);
begin
inherited;
if Assigned(FOnMouseLeave) then
FOnMouseLeave(Self)
else if not(csDesigning in ComponentState) then
MouseLeave;
end;
{ TDefineButton }
constructor TDefineButton.Create(AOwner: TComponent);
begin
FGlyph := TBitmap.Create;
inherited Create(AOwner);
ControlStyle := [csCaptureMouse, csOpaque, csDoubleClicks];
TabStop := true;
ParentFont := True;
ParentColor := True;
fColorFocused := DefaultFocusedColor;
fColorDown := DefaultDownColor;
FColorBorder := DefaultBorderColor;
FColorShadow := DefaultShadowColor;
FState := bsUp;
fColorFlat := DefaultFlatColor;
FSpacing := 4;
FMargin := -1;
FNumGlyphs := 1;
FLayout := blGlyphTop;
FModalResult := mrNone;
FTransparent := tmNone;
fHasFocusFrame:= true;
SetBounds(0, 0, 80, 25);
end;
destructor TDefineButton.Destroy;
begin
FGlyph.Free;
inherited Destroy;
end;
procedure TDefineButton.Paint;
var
FTransColor: TColor;
FImageList: TImageList;
sourceRect, destRect, FocusRect: TRect;
tempGlyph, memBitmap: TBitmap;
Offset: TPoint;
begin
// get the transparent color
FTransColor := FGlyph.Canvas.Pixels[0, FGlyph.Height - 1];
memBitmap := TBitmap.Create; // create memory-bitmap to draw flicker-free
try
memBitmap.Height := ClientRect.Bottom;
memBitmap.Width := ClientRect.Right;
memBitmap.Canvas.Font := Self.Font;
if FState in [bsDown, bsExclusive] then
Offset := Point(1, 1)
else
Offset := Point(0, 0);
CalcButtonLayout(memBitmap.Canvas, ClientRect, Offset, FLayout, FSpacing,
FMargin, FGlyph, FNumGlyphs, Caption, TextBounds, GlyphPos);
if not Enabled then
begin
FState := bsDisabled;
FDragging := False;
end
else
begin
if FState = bsDisabled then
begin
if FDown and (GroupIndex <> 0) then
FState := bsExclusive
else
FState := bsUp;
end;
end;
// DrawBackground
case FTransparent of
tmAlways:
DrawParentImage(Self, memBitmap.Canvas);
tmNone:
begin
case FState of
bsUp:
if FMouseIn then
memBitmap.Canvas.Brush.Color := fColorFocused
else
memBitmap.Canvas.Brush.Color := fColorFlat;
bsDown:
memBitmap.Canvas.Brush.Color := fColorDown;
bsExclusive:
if FMouseIn then
memBitmap.Canvas.Brush.Color := fColorFocused
else
memBitmap.Canvas.Brush.Color := fColorDown;
bsDisabled:
memBitmap.Canvas.Brush.Color := fColorFlat;
end;
memBitmap.Canvas.FillRect(ClientRect);
end;
tmNotFocused:
if FMouseIn then
begin
case FState of
bsUp:
if FMouseIn then
memBitmap.Canvas.Brush.Color := fColorFocused
else
memBitmap.Canvas.Brush.Color := fColorFlat;
bsDown:
memBitmap.Canvas.Brush.Color := fColorDown;
bsExclusive:
if FMouseIn then
memBitmap.Canvas.Brush.Color := fColorFocused
else
memBitmap.Canvas.Brush.Color := fColorDown;
bsDisabled:
memBitmap.Canvas.Brush.Color := fColorFlat;
end;
memBitmap.Canvas.FillRect(ClientRect);
end
else
DrawParentImage(Self, memBitmap.Canvas);
end;
// DrawBorder
case FState of
bsUp:
if FMouseIn then
DrawButtonBorder(memBitmap.canvas, ClientRect, FColorShadow, 1)
else
if FDefault then
DrawButtonBorder(memBitmap.canvas, ClientRect, FColorBorder, 2)
else
DrawButtonBorder(memBitmap.canvas, ClientRect, FColorBorder, 1);
bsDown, bsExclusive:
DrawButtonBorder(memBitmap.canvas, ClientRect, FColorShadow, 1);
bsDisabled:
DrawButtonBorder(memBitmap.canvas, ClientRect, FColorBorder, 1);
end;
if (FMouseIn)and(fHasFocusFrame) then begin
with ClientRect do
begin
FocusRect := Rect(Left+2,Top+2,Right-2,Bottom-2);
end;
memBitmap.Canvas.DrawFocusRect(FocusRect);
end;
// DrawGlyph
if not FGlyph.Empty then
begin
tempGlyph := TBitmap.Create;
case FNumGlyphs of
1: case FState of
bsUp: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
bsDisabled: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
bsDown: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
bsExclusive: sourceRect := Rect(0, 0, FGlyph.Width, FGlyph.Height);
end;
2: case FState of
bsUp: sourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsDisabled: sourceRect := Rect(FGlyph.Width div FNumGlyphs, 0, FGlyph.Width, FGlyph.Height);
bsDown: sourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsExclusive: sourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
end;
3: case FState of
bsUp: SourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsDisabled: SourceRect := Rect(FGlyph.width div FNumGlyphs, 0, (FGlyph.Width div FNumGlyphs) * 2, FGlyph.Height);
bsDown: SourceRect := Rect((FGlyph.Width div FNumGlyphs) * 2, 0, FGlyph.Width, FGlyph.Height);
bsExclusive: SourceRect := Rect((FGlyph.Width div FNumGlyphs) * 2, 0, FGlyph.Width, FGlyph.Height);
end;
4: case FState of
bsUp: SourceRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
bsDisabled: SourceRect := Rect(FGlyph.width div FNumGlyphs, 0, (FGlyph.Width div FNumGlyphs) * 2, FGlyph.Height);
bsDown: SourceRect := Rect((FGlyph.Width div FNumGlyphs) * 2, 0, (FGlyph.Width div FNumGlyphs) * 3, FGlyph.Height);
bsExclusive: SourceRect := Rect((FGlyph.width div FNumGlyphs) * 3, 0, FGlyph.Width, FGlyph.Height);
end;
end;
destRect := Rect(0, 0, FGlyph.Width div FNumGlyphs, FGlyph.Height);
tempGlyph.Width := FGlyph.Width div FNumGlyphs;
tempGlyph.Height := FGlyph.Height;
tempGlyph.canvas.copyRect(destRect, FGlyph.canvas, sourcerect);
if (FNumGlyphs = 1) and (FState = bsDisabled) then
begin
tempGlyph := CreateDisabledBitmap(tempGlyph, clBlack, clBtnFace, clBtnHighlight, clBtnShadow, True);
FTransColor := tempGlyph.Canvas.Pixels[0, tempGlyph.Height - 1];
end;
FImageList := TImageList.CreateSize(FGlyph.Width div FNumGlyphs, FGlyph.Height);
try
FImageList.AddMasked(tempGlyph, FTransColor);
FImageList.Draw(memBitmap.canvas, glyphpos.x, glyphpos.y, 0);
finally
FImageList.Free;
end;
tempGlyph.free;
end;
// DrawText
memBitmap.Canvas.Brush.Style := bsClear;
if FState = bsDisabled then
begin
OffsetRect(TextBounds, 1, 1);
memBitmap.Canvas.Font.Color := clBtnHighlight;
DrawText(memBitmap.Canvas.Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
OffsetRect(TextBounds, -1, -1);
memBitmap.Canvas.Font.Color := clBtnShadow;
DrawText(memBitmap.Canvas.Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
end
else
DrawText(memBitmap.Canvas.Handle, PChar(Caption), Length(Caption), TextBounds, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
// Copy memBitmap to screen
canvas.CopyRect(ClientRect, memBitmap.canvas, ClientRect);
finally
memBitmap.free; // delete the bitmap
end;
end;
procedure TDefineButton.UpdateTracking;
var
P: TPoint;
begin
if Enabled then
begin
GetCursorPos(P);
FMouseIn := not (FindDragTarget(P, True) = Self);
if FMouseIn then
MouseLeave
else
MouseEnter;
end;
end;
procedure TDefineButton.Loaded;
begin
inherited Loaded;
Invalidate;
end;
procedure TDefineButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if(Button = mbLeft) and Enabled then
begin
if not FDown then
begin
FState := bsDown;
Invalidate;
end;
FDragging := True;
SetFocus;
end;
end;
procedure TDefineButton.MouseMove (Shift: TShiftState; X, Y: Integer);
var
NewState: TButtonState;
begin
inherited;
if FDragging then
begin
if not FDown then
NewState := bsUp
else
NewState := bsExclusive;
if (X >= 0) and (X < ClientWidth) and (Y >= 0) and (Y <= ClientHeight) then
if FDown then
NewState := bsExclusive
else
NewState := bsDown;
if NewState <> FState then
begin
FState := NewState;
Invalidate;
end;
end;
end;
procedure TDefineButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
DoClick: Boolean;
begin
inherited MouseUp(Button, Shift, X, Y);
if FDragging then
begin
FDragging := False;
DoClick := (X >= 0) and (X < ClientWidth) and (Y >= 0) and (Y <= ClientHeight);
if FGroupIndex = 0 then
begin
// Redraw face in-case mouse is captured
FState := bsUp;
FMouseIn := False;
if DoClick and not (FState in [bsExclusive, bsDown]) then
Invalidate;
end
else
if DoClick then
begin
SetDown(not FDown);
if FDown then Repaint;
end
else
begin
if FDown then FState := bsExclusive;
Repaint;
end;
if DoClick then Click else
MouseLeave;
UpdateTracking;
end;
end;
procedure TDefineButton.Click;
begin
if Parent <> nil then begin
GetParentForm(self).ModalResult := FModalResult;
SetDown(False);
end;
if Assigned(PopupMenu) then
PopupMenu.PopUp(ClientToScreen(Point(0, Height)).X,
ClientToScreen(Point(0, Height)).Y);
inherited Click;
end;
function TDefineButton.GetPalette: HPALETTE;
begin
Result := FGlyph.Palette;
end;
procedure TDefineButton.SetColors(Index: Integer; Value: TColor);
begin
case Index of
0: fColorFocused := Value;
1: fColorDown := Value;
2: FColorBorder := Value;
3: FColorShadow := Value;
4: FColorFlat := Value;
end;
Invalidate;
end;
procedure TDefineButton.SetGlyph(Value: TBitmap);
begin
if value <> FGlyph then
begin
FGlyph.Assign(value);
if not FGlyph.Empty then
begin
if FGlyph.Width mod FGlyph.Height = 0 then
begin
FNumGlyphs := FGlyph.Width div FGlyph.Height;
if FNumGlyphs > 4 then FNumGlyphs := 1;
end;
end;
Invalidate;
end;
end;
procedure TDefineButton.SetNumGlyphs(Value: TNumGlyphs);
begin
if value <> FNumGlyphs then
begin
FNumGlyphs := value;
Invalidate;
end;
end;
procedure TDefineButton.UpdateExclusive;
var
Msg: TMessage;
begin
if (FGroupIndex <> 0) and (Parent <> nil) then
begin
Msg.Msg := CM_BUTTONPRESSED;
Msg.WParam := FGroupIndex;
Msg.LParam := Longint(Self);
Msg.Result := 0;
Parent.Broadcast(Msg);
end;
end;
procedure TDefineButton.SetDown(Value: Boolean);
begin
if FGroupIndex = 0 then Value := False;
if Value <> FDown then
begin
if FDown and (not FAllowAllUp) then Exit;
FDown := Value;
if Value then
begin
if FState = bsUp then Invalidate;
FState := bsExclusive
end
else
begin
FState := bsUp;
Repaint;
end;
if Value then UpdateExclusive;
end;
end;
procedure TDefineButton.SetGroupIndex(Value: Integer);
begin
if FGroupIndex <> Value then
begin
FGroupIndex := Value;
UpdateExclusive;
end;
end;
procedure TDefineButton.SetLayout(Value: TButtonLayout);
begin
if FLayout <> Value then
begin
FLayout := Value;
Invalidate;
end;
end;
procedure TDefineButton.SetMargin(Value: Integer);
begin
if(Value <> FMargin) and(Value >= -1) then
begin
FMargin := Value;
Invalidate;
end;
end;
procedure TDefineButton.SetSpacing(Value: Integer);
begin
if Value <> FSpacing then
begin
FSpacing := Value;
Invalidate;
end;
end;
procedure TDefineButton.SetAllowAllUp(Value: Boolean);
begin
if FAllowAllUp <> Value then
begin
FAllowAllUp := Value;
UpdateExclusive;
end;
end;
procedure TDefineButton.WMLButtonDblClk(var Message: TWMLButtonDown);
begin
inherited;
if FDown then DblClick;
end;
procedure TDefineButton.CMEnabledChanged(var Message: TMessage);
begin
inherited;
if not Enabled then begin
FMouseIn := False;
FState := bsDisabled;
// RemoveMouseTimer;
end;
UpdateTracking;
Invalidate;
end;
procedure TDefineButton.CMButtonPressed(var Message: TMessage);
var
Sender: TDefineButton;
begin
if Message.WParam = FGroupIndex then
begin
Sender := TDefineButton(Message.LParam);
if Sender <> Self then
begin
if Sender.Down and FDown then
begin
FDown := False;
FState := bsUp;
Invalidate;
end;
FAllowAllUp := Sender.AllowAllUp;
end;
end;
end;
procedure TDefineButton.CMDialogKey(var Message: TCMDialogKey);
begin
with Message do
if ((CharCode = VK_RETURN) and FMouseIn) and
(KeyDataToShiftState(Message.KeyData) = []) and Enabled then
begin
Click;
Result := 1;
end else
inherited;
end;
procedure TDefineButton.CMDialogChar(var Message: TCMDialogChar);
begin
with Message do
if IsAccel(CharCode, Caption) and Enabled then begin
if GroupIndex <> 0 then
SetDown(true);
Click;
Result := 1;
end;
end;
procedure TDefineButton.CMFontChanged(var Message: TMessage);
begin
Invalidate;
end;
procedure TDefineButton.CMTextChanged(var Message: TMessage);
begin
Invalidate;
end;
procedure TDefineButton.CMSysColorChange(var Message: TMessage);
begin
inherited;
if (Parent <> nil)and(ParentColor) then
Color := TDefineButton(Parent).Color;
Invalidate;
end;
procedure TDefineButton.CMParentColorChanged(var Message: TWMNoParams);
begin
inherited;
if (Parent <> nil)and(not ParentColor) then
Color := TDefineButton(Parent).Color;
Invalidate;
end;
procedure TDefineButton.MouseEnter;
begin
if Enabled and not FMouseIn then
begin
FMouseIn := True;
Invalidate;
end;
end;
procedure TDefineButton.MouseLeave;
begin
if Enabled and FMouseIn and not FDragging then
begin
FMouseIn := False;
Invalidate;
end;
end;
procedure TDefineButton.SetDefault(const Value: Boolean);
var
{$IFDEF DFS_COMPILER_2}
Form: TForm;
{$ELSE}
Form: TCustomForm;
{$ENDIF}
begin
FDefault := Value;
if HandleAllocated then
begin
Form := GetParentForm(Self);
if Form <> nil then
Form.Perform(CM_FOCUSCHANGED, 0, Longint(Form.ActiveControl));
end;
Invalidate;
end;
procedure TDefineButton.WMKillFocus(var Message: TWMKillFocus);
begin
inherited;
MouseLeave;
end;
procedure TDefineButton.WMSetFocus(var Message: TWMSetFocus);
begin
inherited;
if Enabled then
begin
FMouseIn := True;
Invalidate;
end;
end;
procedure TDefineButton.WMKeyDown(var Message: TWMKeyDown);
var CharCode:Word;
begin
CharCode := Message.CharCode;
if CharCode = VK_SPACE then
begin
if GroupIndex = 0 then
FState := bsDown
else
SetDown(true);
Invalidate;
end;
end;
procedure TDefineButton.WMKeyUp(var Message: TWMKeyUp);
var CharCode:Word;
begin
CharCode := Message.CharCode;
if CharCode = VK_SPACE then begin
if GroupIndex = 0 then
FState := bsUp
else
SetDown(false);
Click;
Invalidate;
end;
end;
procedure TDefineButton.SetTransparent(const Value: TTransparentMode);
begin
FTransparent := Value;
Invalidate;
end;
procedure TDefineButton.WMMove(var Message: TWMMove);
begin
inherited;
if not (FTransparent = tmNone) then
Invalidate;
end;
procedure TDefineButton.WMSize(var Message: TWMSize);
begin
inherited;
if not (FTransparent = tmNone) then
Invalidate;
end;
procedure TDefineButton.CMMouseEnter(var Message: TMessage);
begin
inherited;
if Assigned(FOnMouseEnter) then
FOnMouseEnter(Self)
else if not(csDesigning in ComponentState) then
MouseEnter;
end;
procedure TDefineButton.CMMouseLeave(var Message: TMessage);
begin
inherited;
if Assigned(FOnMouseLeave) then
FOnMouseLeave(Self)
else if not(csDesigning in ComponentState) then
MouseLeave;
end;
procedure TDefineButton.SetName(const Value: TComponentName);
begin
inherited SetName(Value);
if (csDesigning in ComponentState)and((GetTextLen = 0)or
(CompareText(Caption, Name) = 0)) then
Caption := Value;
end;
{ TDefineSpin }
constructor TDefineSpin.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle - [csAcceptsControls, csSetCaption] + [csFramed, csOpaque];
FUpButton := CreateButton;
FDownButton := CreateButton;
UpGlyph := nil;
DownGlyph := nil;
FFocusedButton := FUpButton;
SetBounds(0,0,21,10);
end;
function TDefineSpin.CreateButton: TDefineTimer;
begin
Result := TDefineTimer.Create(Self);
Result.OnClick := BtnClick;
Result.OnMouseDown := BtnMouseDown;
Result.Visible := True;
Result.Enabled := True;
Result.TimeBtnState := [tbAllowTimer];
Result.Parent := Self;
end;
procedure TDefineSpin.Notification (AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FFocusControl) then
FFocusControl := nil;
end;
procedure TDefineSpin.AdjustSize(var W, H: Integer);
begin
if (FUpButton = nil) or (csLoading in ComponentState) then Exit;
FUpButton.SetBounds(0, 0, 15, H);
FDownButton.SetBounds(16, 0, 15, H);
end;
procedure TDefineSpin.SetBounds (ALeft, ATop, AWidth, AHeight: Integer);
var
W, H: Integer;
begin
W := AWidth;
H := AHeight;
AdjustSize (W, H);
inherited SetBounds (ALeft, ATop, W, H);
end;
procedure TDefineSpin.WMSize(var Message: TWMSize);
var
W, H: Integer;
begin
inherited;
// check for minimum size
W := Width;
H := Height;
AdjustSize (W, H);
if (W <> Width) or (H <> Height) then
inherited SetBounds(Left, Top, W, H);
Message.Result := 0;
end;
procedure TDefineSpin.WMSetFocus(var Message: TWMSetFocus);
begin
FFocusedButton.TimeBtnState := FFocusedButton.TimeBtnState + [tbFocusRect];
FFocusedButton.Invalidate;
end;
procedure TDefineSpin.WMKillFocus(var Message: TWMKillFocus);
begin
FFocusedButton.TimeBtnState := FFocusedButton.TimeBtnState - [tbFocusRect];
FFocusedButton.Invalidate;
end;
procedure TDefineSpin.KeyDown(var Key: Word; Shift: TShiftState);
begin
case Key of
VK_UP:
begin
SetFocusBtn(FUpButton);
FUpButton.Click;
end;
VK_DOWN:
begin
SetFocusBtn(FDownButton);
FDownButton.Click;
end;
VK_SPACE:
FFocusedButton.Click;
end;
end;
procedure TDefineSpin.BtnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
SetFocusBtn (TDefineTimer(Sender));
if (FFocusControl <> nil) and FFocusControl.TabStop and
FFocusControl.CanFocus and (GetFocus <> FFocusControl.Handle) then
FFocusControl.SetFocus
else if TabStop and (GetFocus <> Handle) and CanFocus then
SetFocus;
end;
end;
procedure TDefineSpin.BtnClick(Sender: TObject);
begin
if Sender = FUpButton then
if Assigned(FOnUpClick) then
FOnUpClick(Self);
if Sender = FDownButton then
if Assigned(FOnDownClick) then
FOnDownClick(Self);
end;
procedure TDefineSpin.SetFocusBtn (Btn: TDefineTimer);
begin
if TabStop and CanFocus and (Btn <> FFocusedButton) then
begin
FFocusedButton.TimeBtnState := FFocusedButton.TimeBtnState - [tbFocusRect];
FFocusedButton := Btn;
if (GetFocus = Handle) then
begin
FFocusedButton.TimeBtnState := FFocusedButton.TimeBtnState + [tbFocusRect];
Invalidate;
end;
end;
end;
procedure TDefineSpin.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
Message.Result := DLGC_WANTARROWS;
end;
procedure TDefineSpin.Loaded;
var
W, H: Integer;
begin
inherited Loaded;
W := Width;
H := Height;
AdjustSize (W, H);
if (W <> Width) or (H <> Height) then
inherited SetBounds(Left, Top, Width, Height);
end;
function TDefineSpin.GetUpGlyph: TBitmap;
begin
Result := FUpButton.Glyph;
end;
procedure TDefineSpin.SetUpGlyph(Value: TBitmap);
begin
if Value <> nil then
FUpButton.Glyph := Value
else
begin
FUpButton.Glyph.Handle := LoadBitmap(HInstance, 'FlatUp');
FUpButton.NumGlyphs := 1;
FUpButton.Margin := 2;
FUpButton.Invalidate;
FUpButton.Layout := blGlyphTop;
end;
end;
function TDefineSpin.GetUpNumGlyphs: TNumGlyphs;
begin
Result := FUpButton.NumGlyphs;
end;
procedure TDefineSpin.SetUpNumGlyphs(Value: TNumGlyphs);
begin
FUpButton.NumGlyphs := Value;
end;
function TDefineSpin.GetDownGlyph: TBitmap;
begin
Result := FDownButton.Glyph;
end;
procedure TDefineSpin.SetDownGlyph(Value: TBitmap);
begin
if Value <> nil then
FDownButton.Glyph := Value
else
begin
FDownButton.Glyph.Handle := LoadBitmap(HInstance, 'FlatDown');
FDownButton.NumGlyphs := 1;
FDownButton.Margin := 2;
FDownButton.Invalidate;
FDownButton.Layout := blGlyphBottom;
end;
end;
function TDefineSpin.GetDownNumGlyphs: TNumGlyphs;
begin
Result := FDownButton.NumGlyphs;
end;
procedure TDefineSpin.SetDownNumGlyphs(Value: TNumGlyphs);
begin
FDownButton.NumGlyphs := Value;
end;
{TDefineTimer}
constructor TDefineTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Cursor := crHandPoint;
end;
destructor TDefineTimer.Destroy;
begin
if FRepeatTimer <> nil then
FRepeatTimer.Free;
inherited Destroy;
end;
procedure TDefineTimer.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if tbAllowTimer in FTimeBtnState then
begin
if FRepeatTimer = nil then
FRepeatTimer := TTimer.Create(Self);
FRepeatTimer.OnTimer := TimerExpired;
FRepeatTimer.Interval := InitRepeatPause;
FRepeatTimer.Enabled := True;
end;
end;
procedure TDefineTimer.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseUp(Button, Shift, X, Y);
if FRepeatTimer <> nil then
FRepeatTimer.Enabled := False;
end;
procedure TDefineTimer.TimerExpired(Sender: TObject);
begin
FRepeatTimer.Interval := RepeatPause;
if (FState = bsDown) and MouseCapture then
begin
try
Click;
except
FRepeatTimer.Enabled := False;
raise;
end;
end;
end;
end.
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Pascal
1
https://gitee.com/wyrover/FlatStyle.git
git@gitee.com:wyrover/FlatStyle.git
wyrover
FlatStyle
FlatStyle
master

搜索帮助