给你看下delphi里label的源代码,是怎么刷新的,参考一下吧 Label1: TLabel;TLabel = class(TCustomLabel)TCustomLabel = class(TGraphicControl) private ... procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED; // 捕获自定义消息 ...procedure TCustomLabel.CMTextChanged(var Message: TMessage); begin Invalidate; AdjustBounds; end;procedure TCustomLabel.AdjustBounds; begin ... DoDrawText(Rect, (DT_EXPANDTABS or DT_CALCRECT) or WordWraps[FWordWrap]); ... 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;function DrawText; external user32 name 'DrawTextA'; // 调用windows api
还有TControl = class(TComponent) ... property Caption: TCaption read GetText write SetText stored IsCaptionStored; // 做成属性 ...procedure TControl.SetText(const Value: TCaption); begin if GetText <> Value then SetTextBuf(PChar(Value)); end;procedure TControl.SetTextBuf(Buffer: PChar); begin Perform(WM_SETTEXT, 0, Longint(Buffer)); Perform(CM_TEXTCHANGED, 0, 0); // 在这里发送这个自定义消息然后去触发上面那个CMTextChanged去刷新文字的绘图 end;再相应WM_Paint消息去保持住结果,每次刷新都去显示
public virtual string Text
{ set
{
//...
this.OnTextChanged(EventArgs.Empty);
}
}Label的OnTextChanged:
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
this.prefWidthCache = -1;
this.AdjustSize();
base.Invalidate(); // 导致重绘,因此你就看到了更新后的Text
}
你可以反射其代码,当然你也可以在Browser里面查看源文件,label控件在客户端最后的结果是什么
Label1: TLabel;TLabel = class(TCustomLabel)TCustomLabel = class(TGraphicControl)
private
...
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED; // 捕获自定义消息
...procedure TCustomLabel.CMTextChanged(var Message: TMessage);
begin
Invalidate;
AdjustBounds;
end;procedure TCustomLabel.AdjustBounds;
begin
...
DoDrawText(Rect, (DT_EXPANDTABS or DT_CALCRECT) or WordWraps[FWordWrap]);
...
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;function DrawText; external user32 name 'DrawTextA'; // 调用windows api
...
property Caption: TCaption read GetText write SetText stored IsCaptionStored; // 做成属性
...procedure TControl.SetText(const Value: TCaption);
begin
if GetText <> Value then SetTextBuf(PChar(Value));
end;procedure TControl.SetTextBuf(Buffer: PChar);
begin
Perform(WM_SETTEXT, 0, Longint(Buffer));
Perform(CM_TEXTCHANGED, 0, 0); // 在这里发送这个自定义消息然后去触发上面那个CMTextChanged去刷新文字的绘图
end;再相应WM_Paint消息去保持住结果,每次刷新都去显示