Solved

RichTextBox Border Color

Posted on 2004-10-10
14
1,933 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String manipulation 15 64
C# Connection String for Oracle database is not working 22 88
Data is not showing from images 15 37
Turn on intranet settings 1 36
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now