我想使用不同于默认值的Font.Color绘制一段TEdit.Text。有没有例子怎么做?
我正试图做这样的事情:
注意:这个截图图片只是一个毛茸茸的草稿,但它让我确信可解决的问题。
答案 0 :(得分:14)
Edit
控件没有 owner-draw 支持,但您可以通过对其进行子类化并处理WM_PAINT
来自定义它(在许多其他消息中)。这是可行的,但实际100%正确实施将是一个痛苦的世界。来自文档:Developing Custom Draw Controls in Visual C++:
请注意,所有者绘制适用于大多数控件。但是,它不适用于编辑控件;并且对于列表控件,它仅适用于报表视图样式
我也有兴趣了解兔子洞有多深,所以,
下面是使用内插器类的代码示例(仍然需要实现选择,但是当插入符号在控件中时自定义绘图有效):
type
TEdit = class(StdCtrls.TEdit)
private
FCanvas: TCanvas;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
protected
procedure WndProc(var Message: TMessage); override;
procedure Paint; virtual;
procedure PaintWindow(DC: HDC); override;
property Canvas: TCanvas read FCanvas;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
...
constructor TEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Control := Self;
end;
destructor TEdit.Destroy;
begin
FCanvas.Free;
inherited Destroy;
end;
procedure TEdit.Paint;
var
R: TRect;
I: Integer;
S: String;
begin
R := ClientRect;
Inc(R.Left, 1);
Inc(R.Top, 1);
Canvas.Brush.Assign(Self.Brush);
Canvas.Font.Assign(Self.Font);
for I := 1 to Length(Text) do
begin
if Text[I] in ['0'..'9'] then
Canvas.Font.Color := clRed
else
Canvas.Font.Color := clGreen;
S := Text[I];
DrawText(Canvas.Handle, PChar(S), -1, R, DT_LEFT or DT_NOPREFIX or
DT_WORDBREAK or DrawTextBiDiModeFlagsReadingOnly);
Inc(R.Left,Canvas.TextWidth(S));
end;
end;
procedure TEdit.PaintWindow(DC: HDC);
begin
FCanvas.Lock;
try
FCanvas.Handle := DC;
try
TControlCanvas(FCanvas).UpdateTextFlags;
Paint;
finally
FCanvas.Handle := 0;
end;
finally
FCanvas.Unlock;
end;
end;
procedure TEdit.WMPaint(var Message: TWMPaint);
begin
ControlState := ControlState+[csCustomPaint];
inherited;
ControlState := ControlState-[csCustomPaint];
end;
procedure TEdit.WndProc(var Message: TMessage);
begin
inherited WndProc(Message);
with Message do
case Msg of
CM_MOUSEENTER, CM_MOUSELEAVE, WM_LBUTTONUP, WM_LBUTTONDOWN,
WM_KEYDOWN, WM_KEYUP,
WM_SETFOCUS, WM_KILLFOCUS,
CM_FONTCHANGED, CM_TEXTCHANGED:
begin
Invalidate;
end;
end;
end;
答案 1 :(得分:9)
没有。标准tEdit不支持自定义绘图或具有多种颜色的文本。作为替代方案,您可以使用带有WantReturns = False的tRichEdit。
答案 2 :(得分:3)
kobik solusion的一些改进:
procedure TMyEdit.Paint;
var
R: TRect;
I: Integer;
NewColor : TColor;
NewBackColor : TColor;
procedure DrawEx(S: String);
begin
if ((i-1)>=Self.SelStart) and ((i-1)<=(Self.SelStart+(Self.SelLength-1)))
and (Self.SelLength>0) and (Self.focused)
then begin
Canvas.Font.Color := clWhite;
Canvas.Brush.Color := NewColor;
end else begin
Canvas.Font.Color := NewColor;
Canvas.Brush.Color := NewBackColor;
end;
Canvas.Brush.Style := bsSolid;
DrawText(Canvas.Handle, PChar(S), -1, R, DT_LEFT or DT_NOPREFIX or
DT_WORDBREAK or DrawTextBiDiModeFlagsReadingOnly);
end;
begin
R := ClientRect;
Inc(R.Left, 1);
Inc(R.Top, 1);
Canvas.Brush.Assign(Self.Brush);
Canvas.Font.Assign(Self.Font);
if Self.Focused then begin
NewBackColor := clYellow;
Canvas.Brush.Color := NewBackColor;
Canvas.Brush.Style := bsSolid;
Canvas.FillRect(ClientRect);
Canvas.DrawFocusRect(ClientRect);
end else NewBackColor := clWhite;
for I:=1 to Length(Text) do begin
if PasswordChar=#0 then begin
if Text[I] in ['0'..'9'] then begin
NewColor := clRed;
DrawEx(Text[I]);
end else begin
NewColor := clGreen;
DrawEx(Text[I]);
end;
Inc(R.Left,Canvas.TextWidth(Text[I]));
end else begin //with passwordchar
NewColor := clBlack;
DrawEx(PasswordChar);
Inc(R.Left,Canvas.TextWidth(PasswordChar));
end;
end;
end;
答案 3 :(得分:0)
通过覆盖CreateParams过程的另一项小改进,该过程可修复文本选择期间的闪烁(鼠标左键按下时移动鼠标):
procedure TMyEdit.CreateParams(var Params: TCreateParams);
begin
inherited;
if csDesigning in ComponentState then
exit;
Params.ExStyle := Params.ExStyle or WS_EX_COMPOSITED;
end;