HotDog's Blog

Hotdog (Robert Verpalen) about C# and vb.net

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AugSeptember 2004Oct
SMTWTFS
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

Articles

Archives

Topics

CONTACT

Fun but useful linkies

General

VS 2005

Wolfenstein ET

Thursday, September 09, 2004 #

 

This is only a very small addition to
http://blogs.vbcity.com/hotdog/archive/2004/09/09/272.aspx , but I figured this is something that will come in handy often enough to put in a seperate post:

You want your control to behave as if a button is clicked? All you have to do is have a raised 3d border in normal circumstances and set it to sunken while the mouse is down (restoring it afterwards) eg:

 

private Border3DStyle borderstyle = Border3DStyle.Raised;

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint (e);

ControlPaint.DrawBorder3D(

e.Graphics,ClientRectangle,borderstyle);

}

protected override void OnMouseDown(MouseEventArgs e)

{

borderstyle=Border3DStyle.Sunken;

Invalidate();

base.OnMouseDown (e);

}

protected override void OnMouseUp(MouseEventArgs e)

{

borderstyle=Border3DStyle.Raised;

Invalidate();

base.OnMouseUp (e);

}

posted @ 8:18 AM

A usercontrol does not have a default borderstyle property. Still creating different sets of borders is very easy in the might .net.

Inside the System.Windows.Forms namespace (which is referenced by default in  a windowsapplication) there's a class named ControlPaint, which has some very interesting static (shared in vb.net) methods. One of those methods is the DrawBorder3D method, which does just that. You have the choice of several borders (defined in the Border3DStyle enumeration).

All you have to do is override the paint event of your custom control (or add a paint event catcher, but when overriding is possible, it's the preferred method) and do some drawing. For example:

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint (e);

ControlPaint.DrawBorder3D(

e.Graphics,ClientRectangle,Border3DStyle.Raised);

}

 

That's not too bad is it? The ControlPaint class has a lot of other very usefull methods. For example one paints a comboboxbutton. (all button drawing methods use the ButtonState enumaration so all states can be painted). Others a checkbox, a grid, a grabhandle, you name it. Even a way to draw a disabled image windows style.

Have fun exploring :D

posted @ 7:53 AM