FLabel.Pas
资源名称:FLabel.rar [点击查看]
上传用户:hnxhmj
上传日期:2020-10-13
资源大小:14k
文件大小:8k
源码类别:
Static控件
开发平台:
C++ Builder
- {*******************************************************}
- { }
- { Enhaned TLabel with SetTextCharacterExtra capable }
- { }
- { Copyright (c) 2001 冷路生 }
- { }
- {*******************************************************}
- unit FLabel;
- interface
- uses Messages, Windows, SysUtils, Classes, Controls, Forms, Menus, Graphics;
- type
- TCustomLabel = class(TGraphicControl)
- private
- FFocusControl: TWinControl;
- FAlignment: TAlignment;
- FAutoSize: Boolean;
- FLayout: TTextLayout;
- FWordWrap: Boolean;
- FShowAccelChar: Boolean;
- function GetTransparent: Boolean;
- procedure SetAlignment(Value: TAlignment);
- procedure SetFocusControl(Value: TWinControl);
- procedure SetShowAccelChar(Value: Boolean);
- procedure SetTransparent(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;
- 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;
- procedure SetAutoSize(Value: Boolean); virtual;
- 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 GetTransparent write SetTransparent default False;
- property Layout: TTextLayout read FLayout write SetLayout default tlTop;
- property WordWrap: Boolean read FWordWrap write SetWordWrap default False;
- public
- constructor Create(AOwner: TComponent); override;
- property Canvas;
- end;
- TLabel = class(TCustomLabel)
- 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;
- 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
- constructor TCustomLabel.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- ControlStyle := ControlStyle + [csOpaque, csReplicatable];
- Width := 65;
- Height := 17;
- FAutoSize := True;
- FShowAccelChar := True;
- end;
- function TCustomLabel.GetLabelText: string;
- begin
- Result := Caption;
- end;
- procedure TCustomLabel.DoDrawText(var Rect: TRect; Flags: Longint);
- var
- Text: string;
- begin
- 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);
- Canvas.Font := Font;
- 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
- DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
- end;
- procedure TCustomLabel.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;
- begin
- with Canvas do
- begin
- if not Transparent then
- begin
- Brush.Color := Self.Color;
- Brush.Style := bsSolid;
- FillRect(ClientRect);
- end;
- Brush.Style := bsClear;
- Rect := ClientRect;
- { DoDrawText takes care of BiDi alignments }
- DrawStyle := DT_EXPANDTABS or WordWraps[FWordWrap] or Alignments[FAlignment];
- { Calculate vertical layout }
- 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;
- end;
- procedure TCustomLabel.Loaded;
- begin
- inherited Loaded;
- AdjustBounds;
- end;
- procedure TCustomLabel.AdjustBounds;
- const
- WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
- var
- DC: HDC;
- X: Integer;
- Rect: TRect;
- AAlignment: TAlignment;
- begin
- if not (csReading in ComponentState) and FAutoSize then
- begin
- Rect := ClientRect;
- DC := GetDC(0);
- Canvas.Handle := DC;
- DoDrawText(Rect, (DT_EXPANDTABS or DT_CALCRECT) or WordWraps[FWordWrap]);
- Canvas.Handle := 0;
- ReleaseDC(0, DC);
- X := Left;
- AAlignment := FAlignment;
- if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment);
- if AAlignment = taRightJustify then Inc(X, Width - Rect.Right);
- SetBounds(X, Top, Rect.Right, Rect.Bottom);
- end;
- end;
- procedure TCustomLabel.SetAlignment(Value: TAlignment);
- begin
- if FAlignment <> Value then
- begin
- FAlignment := Value;
- Invalidate;
- end;
- end;
- procedure TCustomLabel.SetAutoSize(Value: Boolean);
- begin
- if FAutoSize <> Value then
- begin
- FAutoSize := Value;
- AdjustBounds;
- end;
- end;
- function TCustomLabel.GetTransparent: Boolean;
- begin
- Result := not (csOpaque in ControlStyle);
- end;
- procedure TCustomLabel.SetFocusControl(Value: TWinControl);
- begin
- FFocusControl := Value;
- if Value <> nil then Value.FreeNotification(Self);
- end;
- procedure TCustomLabel.SetShowAccelChar(Value: Boolean);
- begin
- if FShowAccelChar <> Value then
- begin
- FShowAccelChar := Value;
- Invalidate;
- end;
- end;
- procedure TCustomLabel.SetTransparent(Value: Boolean);
- begin
- if Transparent <> Value then
- begin
- if Value then
- ControlStyle := ControlStyle - [csOpaque] else
- ControlStyle := ControlStyle + [csOpaque];
- Invalidate;
- end;
- end;
- procedure TCustomLabel.SetLayout(Value: TTextLayout);
- begin
- if FLayout <> Value then
- begin
- FLayout := Value;
- Invalidate;
- end;
- end;
- procedure TCustomLabel.SetWordWrap(Value: Boolean);
- begin
- if FWordWrap <> Value then
- begin
- FWordWrap := Value;
- AdjustBounds;
- Invalidate;
- end;
- end;
- procedure TCustomLabel.Notification(AComponent: TComponent;
- Operation: TOperation);
- begin
- inherited Notification(AComponent, Operation);
- if (Operation = opRemove) and (AComponent = FFocusControl) then
- FFocusControl := nil;
- end;
- procedure TCustomLabel.CMTextChanged(var Message: TMessage);
- begin
- Invalidate;
- AdjustBounds;
- end;
- procedure TCustomLabel.CMFontChanged(var Message: TMessage);
- begin
- inherited;
- AdjustBounds;
- end;
- procedure TCustomLabel.CMDialogChar(var Message: TCMDialogChar);
- begin
- if (FFocusControl <> nil) and Enabled and ShowAccelChar and
- IsAccel(Message.CharCode, Caption) then
- with FFocusControl do
- if CanFocus then
- begin
- SetFocus;
- Message.Result := 1;
- end;
- end;
- end.