Solved

RichTextBox Border Color

Posted on 2004-10-10
14
1,906 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

17 Experts available now in Live!

Get 1:1 Help Now