Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2256
  • Last Modified:

RichTextBox Border Color

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
derekthornton
Asked:
derekthornton
  • 6
  • 6
  • 2
1 Solution
 
ptmcompCommented:
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
 
derekthorntonAuthor Commented:
Okay ..That doesn't do me much good, I know nothing of the Win32 API ...
0
 
Razzie_Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
derekthorntonAuthor Commented:
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
 
Razzie_Commented:
Hmmm I noticed. Strange. I don't know that - I'll get back to you about that.
0
 
derekthorntonAuthor Commented:
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
 
Razzie_Commented:
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
 
derekthorntonAuthor Commented:
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
 
derekthorntonAuthor Commented:
Why is the 'OnPaint" method in that example, btw? It's never called.
0
 
Razzie_Commented:
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
 
ptmcompCommented:
I think you have to implement a message handler for the WM_NCCALCSIZE message. But I don't know if it will work.
0
 
derekthorntonAuthor Commented:
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
 
Razzie_Commented:
You can leave the OnPaint method - forgot to get rid of it, just like the struct.
0
 
Razzie_Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now