臧
臧马
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个从TCustomLabel继承下来的标签控件,想实现通过设置控件网格数让文字等距排列。最初我只是通过重载DoDrawText函数,在里面加上了
CharSpac := (Width - TextOut(GetLabelText)) div FGridCount;
SetTextCharacterExtra(Handle,CharSpac);
两句话,这样在taLeftJustify状态下文字左对齐没有问题,但是字符间的间距不对,明显偏大。
之后我放弃了重载DoDrawText函数,重载了Paint函数。下面是代码。但是效果也还是这样。taLeftJustify状态下文字左对齐没有问题,但是字间距有问题。而在其他模式下对齐都有问题,字符间距更是一塌糊涂。那位高手帮我看看,错误在什么地方。谢谢
procedure TMyLabel.Paint;
const
WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
Rect, CalcRect: TRect;
DrawStyle: Longint;
CharSpac: Integer;
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 }
[red]DrawStyle := DT_EXPANDTABS or WordWraps[WordWrap];
Font := self.Font;
CharSpac := TextWidth('W') * Length(GetLabelText);
// CharSpac := TextWidth(GetLabelText);
CharSpac := (Width - CharSpac) div FGridCount;
SetTextCharacterExtra(Handle,CharSpac);[/red]
{ Calculate vertical layout }
if Layout <> tlTop then
begin
CalcRect := Rect;
DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
if Layout = tlBottom then OffsetRect(Rect, 0, Height - CalcRect.Bottom)
else OffsetRect(Rect, 0, (Height - CalcRect.Bottom) div 2);
end;
[red]if Alignment <> taLeftJustify then
begin
CalcRect := Rect;
DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
if Alignment = taRightJustify then
OffsetRect(Rect, Width - CalcRect.Right - CharSpac, 0)
else
OffsetRect(Rect, (Width - CalcRect.Right - CharSpac) div 2, 0);
end;[/red]
DoDrawText(Rect, DrawStyle);
Pen.Color := clBlack;
Pen.Width := 1;
Pen.Style := psSolid;
Rectangle(ClipRect);
end;
end;
CharSpac := (Width - TextOut(GetLabelText)) div FGridCount;
SetTextCharacterExtra(Handle,CharSpac);
两句话,这样在taLeftJustify状态下文字左对齐没有问题,但是字符间的间距不对,明显偏大。
之后我放弃了重载DoDrawText函数,重载了Paint函数。下面是代码。但是效果也还是这样。taLeftJustify状态下文字左对齐没有问题,但是字间距有问题。而在其他模式下对齐都有问题,字符间距更是一塌糊涂。那位高手帮我看看,错误在什么地方。谢谢
procedure TMyLabel.Paint;
const
WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
Rect, CalcRect: TRect;
DrawStyle: Longint;
CharSpac: Integer;
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 }
[red]DrawStyle := DT_EXPANDTABS or WordWraps[WordWrap];
Font := self.Font;
CharSpac := TextWidth('W') * Length(GetLabelText);
// CharSpac := TextWidth(GetLabelText);
CharSpac := (Width - CharSpac) div FGridCount;
SetTextCharacterExtra(Handle,CharSpac);[/red]
{ Calculate vertical layout }
if Layout <> tlTop then
begin
CalcRect := Rect;
DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
if Layout = tlBottom then OffsetRect(Rect, 0, Height - CalcRect.Bottom)
else OffsetRect(Rect, 0, (Height - CalcRect.Bottom) div 2);
end;
[red]if Alignment <> taLeftJustify then
begin
CalcRect := Rect;
DoDrawText(CalcRect, DrawStyle or DT_CALCRECT);
if Alignment = taRightJustify then
OffsetRect(Rect, Width - CalcRect.Right - CharSpac, 0)
else
OffsetRect(Rect, (Width - CalcRect.Right - CharSpac) div 2, 0);
end;[/red]
DoDrawText(Rect, DrawStyle);
Pen.Color := clBlack;
Pen.Width := 1;
Pen.Style := psSolid;
Rectangle(ClipRect);
end;
end;