📄 newstatictext.pas
字号:
unit NewStaticText;
{
TNewStaticText - similar to TStaticText on D3+ but with multi-line AutoSize
support and a WordWrap property
$jrsoftware: issrc/Components/NewStaticText.pas,v 1.3 2003/05/20 03:20:50 jr Exp $
}
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms;
type
TNewStaticText = class(TWinControl)
private
FAutoSize: Boolean;
FFocusControl: TWinControl;
FShowAccelChar: Boolean;
FWordWrap: Boolean;
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 AdjustBounds;
function GetDrawTextFlags: UINT;
procedure SetAutoSize(Value: Boolean);
procedure SetFocusControl(Value: TWinControl);
procedure SetShowAccelChar(Value: Boolean);
procedure SetWordWrap(Value: Boolean);
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
published
property Align;
property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
property Caption;
property Color;
property DragCursor;
property DragMode;
property Enabled;
property FocusControl: TWinControl read FFocusControl write SetFocusControl;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowAccelChar: Boolean read FShowAccelChar write SetShowAccelChar
default True;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property WordWrap: Boolean read FWordWrap write SetWordWrap default False;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('JR', [TNewStaticText]);
end;
{ TNewStaticText }
constructor TNewStaticText.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csCaptureMouse, csClickEvents, csSetCaption,
csOpaque, csReplicatable, csDoubleClicks];
Width := 65;
Height := 17;
FAutoSize := True;
FShowAccelChar := True;
AdjustBounds;
end;
procedure TNewStaticText.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
CreateSubClass(Params, 'STATIC');
with Params do
begin
Style := Style or SS_NOTIFY;
if not FWordWrap then Style := Style or SS_LEFTNOWORDWRAP;
if not FShowAccelChar then Style := Style or SS_NOPREFIX;
end;
end;
procedure TNewStaticText.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;
procedure TNewStaticText.CMFontChanged(var Message: TMessage);
begin
inherited;
AdjustBounds;
end;
procedure TNewStaticText.CMTextChanged(var Message: TMessage);
begin
inherited;
Invalidate;
AdjustBounds;
end;
procedure TNewStaticText.Loaded;
begin
inherited Loaded;
AdjustBounds;
end;
function TNewStaticText.GetDrawTextFlags: UINT;
begin
Result := DT_EXPANDTABS or DT_NOCLIP;
if FWordWrap then Result := Result or DT_WORDBREAK;
if not FShowAccelChar then Result := Result or DT_NOPREFIX;
end;
procedure TNewStaticText.AdjustBounds;
var
R: TRect;
S: String;
DC: HDC;
begin
if not (csReading in ComponentState) and FAutoSize then
begin
{ Note: The calculated width/height is actually one pixel wider/taller
than the size of the text, so that when Enabled=False the white shadow
does not get clipped }
R := Rect(0, 0, Width, 0);
if R.Right > 0 then Dec(R.Right);
S := Caption;
if (S = '') or (FShowAccelChar and (S[1] = '&') and (S[2] = #0)) then
S := S + ' ';
DC := GetDC(0);
try
SelectObject(DC, Font.Handle);
DrawText(DC, PChar(S), Length(S), R, DT_CALCRECT or GetDrawTextFlags);
finally
ReleaseDC(0, DC);
end;
SetBounds(Left, Top, R.Right + 1, R.Bottom + 1);
end;
end;
procedure TNewStaticText.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FFocusControl) then
FFocusControl := nil;
end;
procedure TNewStaticText.SetAutoSize(Value: Boolean);
begin
if FAutoSize <> Value then
begin
FAutoSize := Value;
if Value then AdjustBounds;
end;
end;
procedure TNewStaticText.SetFocusControl(Value: TWinControl);
begin
FFocusControl := Value;
if Value <> nil then Value.FreeNotification(Self);
end;
procedure TNewStaticText.SetShowAccelChar(Value: Boolean);
begin
if FShowAccelChar <> Value then
begin
FShowAccelChar := Value;
RecreateWnd;
AdjustBounds;
end;
end;
procedure TNewStaticText.SetWordWrap(Value: Boolean);
begin
if FWordWrap <> Value then
begin
FWordWrap := Value;
RecreateWnd;
AdjustBounds;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -