Solved

RichTextBox Border Color

Posted on 2004-10-10
14
1,953 Views
Last Modified: 2011-10-03
How can I change the color of a RichTextBox's Border? I've tried overriding paint, but this causes a lot of problems when the TextBox is displayed inside of other controls ...
0
Comment
Question by:derekthornton
  • 6
  • 6
  • 2
14 Comments
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12271407
Usually the border is painted by the Message handler of WM_NCPAINT. They are not avaible as methods in the .net Framework. But you can override the WndProc to handle it. You will need to mess a bit around with the Win32 API (GDI).
0
 
LVL 2

Author Comment

by:derekthornton
ID: 12271424
Okay ..That doesn't do me much good, I know nothing of the Win32 API ...
0
 
LVL 8

Accepted Solution

by:
Razzie_ earned 500 total points
ID: 12271561
Hi derek,

This slightly modified code comes from http://dotnet.meetholland.com/message/143621.aspx - it will create a new control inherited from RichTextBox and will uselike ptmcomp said the WM_NCPAINT message:

CODE BELOW:
--------------------------------------------------------------------------------------------------------------
class CustomRichTextBox : System.Windows.Forms.RichTextBox
{
      [DllImport("user32.dll")]
      static extern IntPtr GetWindowDC(IntPtr hWnd);

      [DllImport("user32.dll")]
      static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

      const int WM_NCCALCSIZE = 0x0083;
      const int WM_NCHITTEST = 0x0084;
      const int WM_NCPAINT = 0x0085;

      struct RECT
      {
            public Int32 left;
            public Int32 top;
            public Int32 right;
            public Int32 bottom;
      };

      protected override void OnPaint(PaintEventArgs e)
      {
            base.OnPaint (e);
            IntPtr hDC = GetWindowDC(Handle);

            if (hDC.ToInt32() == 0)
            {
                  Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());
                  return;
            }

            Graphics g = Graphics.FromHdc(hDC);
            Brush b = Brushes.AliceBlue;
            Pen p = new Pen(b,5);
            Rectangle r = new Rectangle(0,0,this.Size.Width,this.Size.Height);
            g.DrawRectangle(p,r);
            ReleaseDC(Handle,hDC);
      }

      protected override void WndProc(ref Message m)
      {
            switch(m.Msg)
            {
                  case WM_NCPAINT:
                  {
                        base.WndProc(ref m);
                        IntPtr hDC = GetWindowDC(m.HWnd);

                        if (hDC.ToInt32() == 0)
                        {
                              Debug.WriteLine("Couldn't get hDC:"+Marshal.GetLastWin32Error().ToString());
                              break;
                        }

                        Graphics g = Graphics.FromHdc(hDC);
                        Brush b = Brushes.Beige;
                        Pen p = new Pen(b,5);
                        Rectangle r = new Rectangle(0,0,this.Size.Width,this.Size.Height);

                        
                        g.DrawRectangle(p,r);
                        m.Result = IntPtr.Zero;
                        ReleaseDC(m.HWnd,hDC);

                        return;
                  }
            }

            base.WndProc(ref m);
      }
}
---------------------------------------------------------------------------------------------------

You will need a reference to System.Runtime.InteropServices.
You can modify the border color by changing Brushes.Beige in the WndProc method.

HTH,

Razzie
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Author Comment

by:derekthornton
ID: 12271589
Hrnm.. That's almost what I'm lookign for ..is there any way to eliminate the 3D-esque look to the border? Setting it to "FixedSingle" sure doesn't do that...
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12271877
Hmmm I noticed. Strange. I don't know that - I'll get back to you about that.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 12271885
Alright. Thanks. I'll keep trying tinkering with it in the meantime. If you can't figure it out, tell me and I'll go ahead and give the points to you. But I'd like to get rid of the 3D Border thing if it's possible.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12272451
I got this from the MSDN:

'The derived class, RichTextBox, does not support the BorderStyle.FixedSingle style. This style will cause the borderstyle to use the BorderStyle.Fixed3D style instead.' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformstextboxbaseclassborderstyletopic.asp)


However, you might try the following. In the WndProc method, instead of:

Rectangle r = new Rectangle(0,0,this.Size.Width,this.Size.Height);

use:

Rectangle r = new Rectangle(1,1,this.Size.Width-2,this.Size.Height-2);
------------------------------

This is very dirty code of course, but at least the border looks solid now :)







0
 
LVL 2

Author Comment

by:derekthornton
ID: 12272468
Yeah ..but it still looks tacky. Now the border is all thick and ugly, I was hoping to get a 1pt size border. I guess it's not possible with a RichTextBox control....
0
 
LVL 2

Author Comment

by:derekthornton
ID: 12272502
Why is the 'OnPaint" method in that example, btw? It's never called.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12272509
Hmm well if you change the Pen size to 1 ( Pen p = new Pen(b,1); ) it doesn't look awesome, but still decent imho (especially with darker colors)

Well if I'll ever stumble across a good solution sometime I'll let you know, thanks and take care.
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12272534
I think you have to implement a message handler for the WM_NCCALCSIZE message. But I don't know if it will work.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 12272541
Where are you supposed to get the constant values for the 'messages' that WndProc passes? Seriously. Do you just make them up or something?
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12272574
You can leave the OnPaint method - forgot to get rid of it, just like the struct.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12272586
You can find constant values in the windows.h header file (found in the SDK) and I'm sure there are some internetsites listing them.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question