Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RichTextBox Border Color

Posted on 2004-10-10
14
Medium Priority
?
2,113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

704 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