📄 scustomlabel.pas
字号:
unit sCustomLabel;
{$I sDEFS.INC}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
sConst, ShellApi, sGraphUtils, sVclUtils, sUtils, sStyleSimply;
type
TsCustomLabel = class(TCustomControl)
private
FFocusControl: TWinControl;
FAlignment: TAlignment;
FAutoSize: Boolean;
FLayout: TTextLayout;
FWordWrap: Boolean;
FShowAccelChar: Boolean;
FTransparent: Boolean;
procedure SetAlignment(Value: TAlignment);
procedure SetFocusControl(Value: TWinControl);
procedure SetShowAccelChar(Value: Boolean);
procedure SetLayout(Value: TTextLayout);
procedure SetWordWrap(Value: Boolean);
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
procedure WMNclButtonDown(var Message: TMessage); message WM_LBUTTONDOWN;
procedure WMEraseBkGND (var Message: TWMPaint); message WM_ERASEBKGND;
procedure SetTransparent(const Value: Boolean);
protected
procedure AdjustBounds; dynamic;
procedure DoDrawText(var Rect: TRect; Flags: Longint); dynamic;
function GetLabelText: string; virtual;
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure Paint; override;
{$IFDEF DELPHI6UP}
procedure SetAutoSize(Value: Boolean); override;
{$ELSE}
procedure SetAutoSize(Value: Boolean); virtual;
{$ENDIF}
function GetCurrentFont: TFont; dynamic;
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
property FocusControl: TWinControl read FFocusControl write SetFocusControl;
property ShowAccelChar: Boolean read FShowAccelChar write SetShowAccelChar default True;
property Transparent: Boolean read FTransparent write SetTransparent default True;
property Layout: TTextLayout read FLayout write SetLayout default tlTop;
property WordWrap: Boolean read FWordWrap write SetWordWrap default False;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
function GetParentCache : TCacheInfo;
public
MouseAbove: boolean;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Canvas;
end;
TsEditLabel = class(TsCustomLabel)
private
function GetTop: Integer;
function GetLeft: Integer;
function GetWidth: Integer;
function GetHeight: Integer;
procedure SetHeight(const Value: Integer);
procedure SetWidth(const Value: Integer);
protected
procedure AdjustBounds; override;
public
constructor Create(AOwner: TComponent); override;
published
property BiDiMode;
property Caption;
property Color;
property DragCursor;
property DragKind;
property DragMode;
property Font;
property Height: Integer read GetHeight write SetHeight;
property Left: Integer read GetLeft;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar;
property ShowHint;
property Top: Integer read GetTop;
property Transparent default True;
property Layout;
property WordWrap;
property Width: Integer read GetWidth write SetWidth;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
TsLabel = class(TsCustomLabel)
published
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BiDiMode;
property Caption;
property Color;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FocusControl;
property Font;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar;
property ShowHint;
property Transparent default True;
property Layout;
property Visible;
property WordWrap;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
TsWebLabel = class(TsLabel)
private
procedure SetHoverFont(const Value: TFont);
protected
FHoverFont : TFont;
FURL : string;
FShowMode: TsWindowShowMode;
// FHoverCursor : TCursor;
procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
function GetCurrentFont: TFont; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property HoverFont: TFont read FHoverFont write SetHoverFont;
property ShowMode : TsWindowShowMode read FShowMode write FShowMode default soDefault;
property URL : string read FURL write FURL;
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BiDiMode;
property Caption;
property Color;
property Constraints;
property Cursor default crHandPoint;
// property HoverCursor : TCursor read FHoverCursor write FHoverCursor default crHandPoint;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property FocusControl;
property Font;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar;
property ShowHint;
property Transparent default True;
property Layout;
property Visible;
property WordWrap;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
implementation
uses sMessages
{$IFNDEF ALITE}
, sPageControl
{$ENDIF};
{ TsCustomLabel }
constructor TsCustomLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Parent := TWinControl(AOwner);
Width := 65;
Height := 17; // TLabel
FAutoSize := True;
FShowAccelChar := True;
// FDoubleBuffered := True;
FTransparent := True;
ControlStyle := ControlStyle - [csFramed, csReplicatable] + [csSetCaption, csOpaque];
end;
function TsCustomLabel.GetLabelText: string;
begin
if not Assigned(Parent) then Exit;
Result := Caption;
end;
procedure TsCustomLabel.DoDrawText(var Rect: TRect; Flags: Longint);
var
Text: string;
cFont : TFont;
begin
if not Assigned(Parent) then Exit;
cFont := TFont.Create;
try
Text := GetLabelText;
if (Flags and DT_CALCRECT <> 0) and ((Text = '') or FShowAccelChar and
(Text[1] = '&') and (Text[2] = #0)) then Text := Text + ' ';
if not FShowAccelChar then Flags := Flags or DT_NOPREFIX;
Flags := DrawTextBiDiModeFlags(Flags);
cFont.Assign(GetCurrentFont);
Canvas.Font.Assign(cFont);// := GetCurrentFont;
if not Enabled then begin
OffsetRect(Rect, 1, 1);
Canvas.Font.Color := clBtnHighlight;
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
OffsetRect(Rect, -1, -1);
Canvas.Font.Color := clBtnShadow;
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
end
else begin
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
end;
finally
FreeAndNil(cFont);
end;
end;
procedure TsCustomLabel.Paint;
const
Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
Rect, CalcRect: TRect;
DrawStyle: Longint;
ci : TCacheInfo;
begin
// inherited;
if not Assigned(Parent) then Exit;
if (csDestroying in ComponentState) or (csLoading in ComponentState) then Exit;
ci := GetParentCache;
if Transparent and ci.Ready then begin
CalcRect := ClientRect;
OffsetRect(CalcRect, Left + ci.X, Top + ci.Y);
FadeRect(ci.Bmp.Canvas,
CalcRect,
Canvas.Handle,
Point(0, 0),
100,
Color, 0, ssRectangle);
end
else begin
Canvas.Brush.Color := Color;
Canvas.Brush.Style := bsSolid;
Canvas.FillRect(ClientRect);
end;
Canvas.Brush.Style := bsClear;
Rect := ClientRect;
DrawStyle := DT_EXPANDTABS or WordWraps[FWordWrap] or Alignments[FAlignment];
if FLayout <> tlTop then begin
CalcRect := Rect;
DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
if FLayout = tlBottom then OffsetRect(Rect, 0, Height - CalcRect.Bottom)
else OffsetRect(Rect, 0, (Height - CalcRect.Bottom) div 2);
end;
DoDrawText(Rect, DrawStyle);
end;
procedure TsCustomLabel.Loaded;
begin
if not Assigned(Parent) then Exit;
inherited Loaded;
AdjustBounds;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -