Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:24:45+00:00 2026-05-31T15:24:45+00:00

I’d like to draw a piece of TEdit.Text using Font.Color different from the default.

  • 0

I’d like to draw a piece of TEdit.Text using Font.Color different from the default. Are there any examples how to do that?

I’m attempting to do something like this:

NOTE: what this screenshot pictures is merely a hairy draft, but it convinces me what problem solvable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:24:47+00:00Added an answer on May 31, 2026 at 3:24 pm

    Edit controls do not have owner-draw support, but you can custom-draw it by sub-classing it and handling WM_PAINT (among many other messages). It’s doable, but it would be a world of pain to actually implement 100% correctly. From the docs: Developing Custom Draw Controls in Visual C++:

    Note that owner-draw will work for most controls. However, it doesn’t work for edit controls; and with regards to the list control, it works only for report-view style

    I was also interested to find out how deep the rabbit hole goes, so,
    Here is a code sample using an interposer class (still needs to implement selection but the custom drawing works when the caret is in the control):

    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;
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.