Border for UserControl

  • Thread starter Thread starter Guest
  • Start date Start date
If you want to add a BorderStyle property you can use pinvokes, within the
BorderStyle property "setter", to modify the appearance of the non-client
area using the Win32 API functions GetWindowLong and SetWindowLong.
http://www.pinvoke.net/
 
I overrode the OnPaint event and drew my own border, in my case a simple
line. But you could do anything you wanted.
 
There are several ways to add a border to a control, these depend on the
type of border and more specifically the relationship between the border and
the client area.

The simplest way to add a border to a control is by overriding the
CreateParams property and adding the correct border style to the window
creation parameters. This is shown in detail in Windows Forms Tips and
Tricks.

The most comprehensive way to add a border is to override the WndProc of the
control and add message handlers for the WM_NCCREATE and WM_NCPAINT
messages. Using these you can add a border of any thickness and style you
choose because you have full control over the relationship between the
window and its client area and ful control over the drawing of the border
itself.

You should not simply use ControlPaint or any other graphics API methods to
just draw a border onto the client area of the control. Well, you can if you
wish but you can also re-paint a Rolls Roys using pebble-dash paint and a
paint roller. This isn't the correct way to do it however.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Thanks Bob and all. Yes, CreateParams does seem ideal. I found a sample on
Code Project and it worked fine. I was able to do 3D using ExStyle or flat
using Style.

By the way, is it possible to change the flat line from black to a gray look.

Thank you!

--
Greg McPherran
www.McPherran.com


Bob Powell said:
There are several ways to add a border to a control, these depend on the
type of border and more specifically the relationship between the border and
the client area.

The simplest way to add a border to a control is by overriding the
CreateParams property and adding the correct border style to the window
creation parameters. This is shown in detail in Windows Forms Tips and
Tricks.

The most comprehensive way to add a border is to override the WndProc of the
control and add message handlers for the WM_NCCREATE and WM_NCPAINT
messages. Using these you can add a border of any thickness and style you
choose because you have full control over the relationship between the
window and its client area and ful control over the drawing of the border
itself.

You should not simply use ControlPaint or any other graphics API methods to
just draw a border onto the client area of the control. Well, you can if you
wish but you can also re-paint a Rolls Roys using pebble-dash paint and a
paint roller. This isn't the correct way to do it however.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
To change the border style in that way you'll definitely need to handle
WM_NCPAINT.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Greg said:
Thanks Bob and all. Yes, CreateParams does seem ideal. I found a sample on
Code Project and it worked fine. I was able to do 3D using ExStyle or flat
using Style.

By the way, is it possible to change the flat line from black to a gray
look.

Thank you!
 
Back
Top