Monday, October 11, 2010

Creating Shaped buttons in .NET

Shapes for controls can be defined by the Region property.Use it to specify the shape of choice for the control.

Lets see how buttons can be shaped:


'Oval Shaped button

Dim path1 As New Drawing2D.GraphicsPath() 'define a graphics path
Dim rect As New Rectangle(30, 30, Button1.Width - 60, Button1.Height - 60)
path1.AddEllipse(rect) 'create an ellipse with the rectangle as boundary
Button1.Region = New Region(path1) 'set the region property of the button

'Triangular Button

Dim path2 As New Drawing2D.GraphicsPath()
Dim pt(2) As Point
pt(0) = New Point(Button2.Width / 2, 1)
pt(1) = New Point(Button2.Width - 10, Button2.Height - 10)
pt(2) = New Point(10, Button2.Height - 10)
path2.AddPolygon(pt)
Button2.Region = New Region(path2)

Buttons can also be shaped in the form of Text.


'Text Shaped

Dim path3 As New Drawing2D.GraphicsPath()
path3.AddString("Text Button", New FontFamily("Arial"), 1, 30, New Point(20, 20), _
                            New StringFormat(StringFormatFlags.FitBlackBox))
Button3.Region = New Region(path3)


This is what it would look like


Hope this helps :)

No comments:

Post a Comment