Home All Groups Group Topic Archive Search About

Display text in different colors in a textbox using WndProc

Author
29 Jun 2006 4:19 PM
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)

Author
29 Jun 2006 5:50 PM
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.

Show quote
"digitalshehan" <digitalshe***@gmail.com> escribió en el mensaje
news:1151597981.597619.193890@b68g2000cwa.googlegroups.com...
> 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)
>

AddThis Social Bookmark Button