📄 jvstatictext.pas
字号:
end;
{$IFDEF VCL}
procedure TJvCustomStaticText.CNDrawItem(var Msg: TWMDrawItem);
begin
DrawItem(Msg.DrawItemStruct^);
end;
{$ENDIF VCL}
procedure TJvCustomStaticText.DrawItem(const DrawItemStruct: TDrawItemStruct);
const
cBorders: array [TStaticBorderStyle] of DWORD = (0, BF_MONO, BF_SOFT);
var
R: TRect;
DrawStyle: Cardinal;
B: HBRUSH;
begin
B := CreateSolidBrush(ColorToRGB(Color));
try
with DrawItemStruct do
begin
R := rcItem;
{$IFDEF VCL}
DrawThemedBackground(Self, hDC, R, B);
{$ENDIF VCL}
if BorderStyle <> sbsNone then
DrawEdge(hDC, R, BDR_SUNKENOUTER, BF_ADJUST or BF_RECT or cBorders[BorderStyle]);
DrawStyle := GetTextDisplayInfo(hDC, R);
case Layout of
tlTop:
OffsetRect(R, 0, FTextMargins.Y);
tlBottom:
OffsetRect(R, 0, (ClientHeight - R.Bottom) - FTextMargins.Y);
tlCenter:
OffsetRect(R, 0, (ClientHeight - R.Bottom) div 2);
end;
case Alignment of
taLeftJustify:
OffsetRect(R, FTextMargins.X, 0);
taRightJustify:
OffsetRect(R, (Width - R.Right) - FTextMargins.X, 0);
taCenter:
OffsetRect(R, (Width - R.Right) div 2, 0);
end;
SetBkMode(hDC, Windows.TRANSPARENT);
DrawText(hDC, Caption, Length(Caption), R, DrawStyle);
// DrawText(hDC, Caption, Length(Caption), R, DrawStyle);
end;
finally
DeleteObject(B);
end;
end;
procedure TJvCustomStaticText.SetAutoSize(Value: Boolean);
begin
if FAutoSize <> Value then
begin
FAutoSize := Value;
if Value then
AdjustBounds;
end;
end;
procedure TJvCustomStaticText.AdjustBounds;
var
DC: HDC;
R: TRect;
SaveFont: HFont;
TextSize: TSize;
begin
if not (csReading in ComponentState) and AutoSize and HandleAllocated then
begin
DC := GetDC(HWND_DESKTOP);
if not WordWrap then
begin
SaveFont := SelectObject(DC, Font.Handle);
{$IFDEF VCL}
GetTextExtentPoint32(DC, PChar(Caption), Length(Caption), TextSize);
{$ENDIF VCL}
{$IFDEF VisualCLX}
GetTextExtentPoint32(DC, PWideChar(Caption), Length(Caption), TextSize);
{$ENDIF VisualCLX}
SelectObject(DC, SaveFont);
SetBounds(Left, Top,
TextSize.cx + (GetSystemMetrics(SM_CXBORDER) * 4),
TextSize.cy + (GetSystemMetrics(SM_CYBORDER) * 4));
end
else
begin
R := ClientRect;
GetTextDisplayInfo(DC, R);
SetBounds(Left, Top, R.Right, R.Bottom);
end;
ReleaseDC(HWND_DESKTOP, DC);
end;
end;
function TJvCustomStaticText.WantKey(Key: Integer; Shift: TShiftState;
const KeyText: WideString): Boolean;
begin
Result := (FFocusControl <> nil) and Enabled and ShowAccelChar and
IsAccel(Key, Caption) and (ssAlt in Shift);
if Result then
if FFocusControl.CanFocus then
FFocusControl.SetFocus;
end;
procedure TJvCustomStaticText.FontChanged;
begin
inherited FontChanged;
AdjustBounds;
UpdateTrackFont(HotTrackFont, Font, HotTrackFontOptions);
end;
procedure TJvCustomStaticText.TextChanged;
begin
inherited TextChanged;
AdjustBounds;
end;
procedure TJvCustomStaticText.Loaded;
begin
inherited Loaded;
AdjustBounds;
end;
procedure TJvCustomStaticText.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FFocusControl) then
FFocusControl := nil;
end;
procedure TJvCustomStaticText.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
Invalidate;
end;
end;
procedure TJvCustomStaticText.SetBorderStyle(Value: TStaticBorderStyle);
begin
if FBorderStyle <> Value then
begin
FBorderStyle := Value;
Invalidate;
end;
end;
procedure TJvCustomStaticText.SetFocusControl(Value: TWinControl);
begin
FFocusControl := Value;
if Value <> nil then
Value.FreeNotification(Self);
end;
procedure TJvCustomStaticText.SetShowAccelChar(Value: Boolean);
begin
if FShowAccelChar <> Value then
begin
FShowAccelChar := Value;
Invalidate;
end;
end;
procedure TJvCustomStaticText.SetTextMargins(const Value: TJvTextMargins);
begin
if Value = nil then
begin
FTextMargins.X := 0;
FTextMargins.Y := 0
end
else
begin
FTextMargins.X := Value.X;
FTextMargins.Y := Value.Y;
end;
end;
procedure TJvCustomStaticText.DoMarginsChange(Sender: TObject);
begin
Invalidate;
end;
procedure TJvCustomStaticText.SetWordWrap(const Value: Boolean);
begin
if FWordWrap <> Value then
begin
FWordWrap := Value;
AdjustBounds;
Invalidate;
end;
end;
function TJvCustomStaticText.GetTextDisplayInfo(ADC: HDC; var ARect: TRect): Cardinal;
const
cAlignment: array [Boolean, TAlignment] of DWORD =
((DT_LEFT, DT_RIGHT, DT_CENTER), (DT_RIGHT, DT_LEFT, DT_CENTER));
cLayout: array [TTextLayout] of DWORD = (DT_TOP, DT_VCENTER, DT_BOTTOM);
cDrawAccel: array [Boolean] of DWORD = (DT_NOPREFIX, 0);
cWordWrap: array [Boolean] of DWORD = (0{DT_SINGLELINE}, DT_WORDBREAK);
begin
Result := DT_EXPANDTABS or cAlignment[UseRightToLeftAlignment, Alignment] or
cLayout[Layout] or cDrawAccel[ShowAccelChar] or cWordWrap[WordWrap];
DrawText(ADC, Caption, Length(Caption), ARect, Result or DT_CALCRECT);
end;
procedure TJvCustomStaticText.Resize;
begin
inherited Resize;
Invalidate;
end;
procedure TJvCustomStaticText.SetHotTrackFontOptions(const Value: TJvTrackFOntOptions);
begin
if FHotTrackFontOptions <> Value then
begin
FHotTrackFontOptions := Value;
UpdateTrackFont(HotTrackFont, Font,FHotTrackFontOptions);
end;
end;
{$IFDEF VisualCLX}
procedure TJvCustomStaticText.Paint;
var
FDrawItemStruct: TDrawItemStruct;
begin
with FDrawItemStruct do
begin
rcItem := Bounds(0, 0, Width, Height);
hDC := Canvas.Handle;
hWndItem := Handle;
end;
DrawItem(FDrawItemStruct)
end;
{$ENDIF VisualCLX}
//=== { TJvTextMargins } =====================================================
procedure TJvTextMargins.Change;
begin
if Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TJvTextMargins.SetX(const Value: Word);
begin
if FX <> Value then
begin
FX := Value;
Change;
end;
end;
procedure TJvTextMargins.SetY(const Value: Word);
begin
if FY <> Value then
begin
FY := Value;
Change;
end;
end;
{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);
finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -