Visual Basic - Create Shapely Button Controls Through GDI+ (VB .NET)


Perhaps you're aware that you can shape Microsoft Windows Forms into circles or other shapes using the Form's Region property and a GraphicsPath object. However, did you know that the Button control also has a Region property that you can use to shape the buttons on your form? To test it out, drop a Button onto a Windows Form and add the following code to the Click() event of the Button.


  • If Button1.Tag <> "clicked" Then
  • Button1.Size = New Size(114,90)
  • Dim gPath As GraphicsPath = New GraphicsPath
  • gPath.AddLines(New Point()
  • {New Point(55, 0), New Point(125, 150), _
  • New Point(-15, 150)})
  •  
  • gPath.CloseFigure()
  • Button1.BackColor = Color.MediumBlue
  • Button1.ForeColor = Color.White
  • Button1.Region = New Region(gPath)
  • Button1.Tag = "clicked"
  • Else
  • MsgBox("I'm not square!")
  • End If

This code uses GDI+ to transform the button when you click it the first time and returns a message when you click it the second time. Be sure to Import the System.Drawing.Drawing2D Namespace or include it in the gPath declaration.

Go back