Display text in different colors in a textbox using WndProc


D

digitalshehan

Hi All,

I need to have a text box that displays text in different colors. I
tired overriding WndProc but I still couldn't. Below is the code
snippet I'm using.


Would appreciate if someone could help me out with this (wolud
appreciate if the solution is done using WndProc)


const int WM_PAINT = 0xF;
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);


protected override void WndProc(ref
System.Windows.Forms.Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_PAINT:
IntPtr hDC = GetWindowDC(m.HWnd);
Graphics graph = Graphics.FromHdc(hDC);
graph.DrawString(this.Text, this.Font, new
SolidBrush(Color.Red), 1.6F, 2.5F);


graph.Dispose();
ReleaseDC(m.HWnd, hDC);


m.Result = IntPtr.Zero;
break;


}
}


Thanks!
Shehan


p.s. I'm using .net2.0 (if its of any help)
 
Ad

Advertisements

A

Alexander Ubillus

Hi Shehan ,
Why don't you use a RichTextBox ? it behaves exactly as the TextBox but it
lets you apply several colors and font styles to text.

Overriding a core common control functionality will cost you much time. You
would also have to override the Key pressing because it also makes the
textbox draw its text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top