1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
Imports System.Windows.Forms
Imports System.Windows
Imports System.Drawing
Imports System
Imports System.ComponentModel
''' -----------------------------------------------------------------------------
''' Project : EllipticalButton
''' Class : EllipticalButton
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' This Control is a button that takes an elliptical shap
''' </summary>
''' <remarks>
''' The EllipticalButton Control is a sub class of Control
''' </remarks>
''' <history>
''' [Ahmed Al-Asaad] 3/27/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
<ToolboxBitmap(GetType(Button))> _
Public Class EllipticalButton
Inherits Control
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Name = "Elliptical Button"
Me.Size = New System.Drawing.Size(50, 50)
End Sub
#End Region
#Region "Private Veriables"
Private mousePressed As Boolean = False
Private MeGotFocus As Boolean = False
Private MouseOver As Boolean = False
Private ButtonStyle As Style = Style.Standard
Private ButtonImage As Image = Nothing
#End Region
#Region "Control Properties"
<System.ComponentModel.Category("Appearance"), System.ComponentModel.Browsable(True), _
DefaultValue(GetType(Image), Nothing)> _
Public Property Image() As System.Drawing.Image
Get
Return Me.ButtonImage
End Get
Set(ByVal Value As System.Drawing.Image)
ButtonImage = Value
Me.Invalidate()
End Set
End Property
<System.ComponentModel.Browsable(False)> _
Public Shadows ReadOnly Property BackgroundImage() As System.Drawing.Image
Get
Return MyBase.BackgroundImage()
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' Gets or sets the text associated with this control.
''' </summary>
''' <value>
''' String
''' </value>
''' -----------------------------------------------------------------------------
<System.ComponentModel.Description("Gets or sets the text associated with this control.")> _
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
MyBase.Text = Value
Me.Invalidate()
End Set
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' Gets or sets the flat style appearance of the button control.
''' </summary>
''' <value>enum Style</value>
''' -----------------------------------------------------------------------------
<System.ComponentModel.Category("Appearance"), System.ComponentModel.Browsable(True), _
System.ComponentModel.Description("Gets or sets the flat style appearance of the button control.")> _
Public Overridable Property FlatStyle() As Style
Get
Return ButtonStyle
End Get
Set(ByVal Value As Style)
ButtonStyle = Value
Me.Invalidate()
End Set
End Property
#End Region
#Region "Painting Methods"
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Try
Dim g As Graphics = e.Graphics
Dim a As New System.Drawing.Drawing2D.GraphicsPath
Dim b As New SolidBrush(Color.Transparent)
Dim p1 As Pen
Dim p2 As Pen
Dim stringBrush As New SolidBrush(Me.ForeColor)
Dim recRegion As New RectangleF
Dim recBorder As New RectangleF
'Check if the button style is standard or popup
If Me.ButtonStyle = Style.Standard Then
'Choose the pen colors to simulate the 3D effect
If Me.mousePressed Then
'if mouse is pressed dark colour at the top of the button and light at the buttom
p1 = New Pen(SystemColors.ControlLightLight, 1)
p2 = New Pen(SystemColors.ControlDarkDark, 1)
Else
'if mouse is NOT pressed light colour at the top of the button and dark at the buttom
p1 = New Pen(SystemColors.ControlDarkDark, 1)
p2 = New Pen(SystemColors.ControlLightLight, 1)
End If
recRegion = New RectangleF(0, 0, Me.Width, Me.Height)
recBorder = New RectangleF(1, 1, Me.Width - 2, Me.Height - 2)
'This line is very important to have smooth cruves. Otherwise,
'the button will look so ugly.
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
' I was using the g.FillEllipse(b, rec) method before. Using that
' method however makes the button respond to clicking within the
' rectangle and outsize of the ellipse.
' Using the GraphicPath object and Add ellipse did the job. It sets the shape
' shape/region of the button to be an ellipse. It sure did the trick :)
a.AddEllipse(recRegion)
Me.Region = New Region(a)
' Now we need to draw the 3D effect. Remember we can only draw on
' the ellipse now.
g.DrawArc(p1, recBorder, 180, -180)
g.DrawArc(p2, recBorder, 180, 180)
' Measure the size of the String to centralize it.
' If an image is present, the text is drawn next to the icon.
Dim textSize As SizeF = g.MeasureString(Me.Text, Me.Font)
'If the Mouse is pressed draw the text and the image, if available,
' shifted 2 pixels to the right to simulate the 3D effect
If Me.mousePressed Then
If Me.ButtonImage Is Nothing Then
g.DrawString(Me.Text, Me.Font, stringBrush, (((Me.Width + 3) - textSize.Width) / 2) + 2, (((Me.Height + 2) - textSize.Height) / 2) + 2)
Else
Dim pt As New Point(((Me.Width + 3) / 12) + 1, ((Me.Height + 2 - 16) / 2) + 1)
Dim recString As New Rectangle(pt, New Size(16, 16))
g.DrawImage(ButtonImage, recString)
g.DrawString(Me.Text, Me.Font, stringBrush, recString.X + recString.Width + 3, (((Me.Height + 2) - textSize.Height) / 2) + 2)
End If
Else
If Me.ButtonImage Is Nothing Then
g.DrawString(Me.Text, Me.Font, stringBrush, (((Me.Width + 3) - textSize.Width) / 2), (((Me.Height + 2) - textSize.Height) / 2))
Else
Dim pt As New Point((Me.Width + 3) / 12, (Me.Height + 2 - 16) / 2)
Dim recString As New Rectangle(pt, New Size(16, 16))
g.DrawImage(ButtonImage, recString)
g.DrawString(Me.Text, Me.Font, stringBrush, recString.X + recString.Width + 3, (((Me.Height + 2) - textSize.Height) / 2))
End If
End If
Else ' if the buttonStyle is Popup
' if the mouse is over the button, button should look like the standard one.
If Me.MouseOver = True Then
If Me.mousePressed Then
p1 = New Pen(SystemColors.ControlDarkDark, 1)
p2 = New Pen(SystemColors.ControlDarkDark, 1)
Else
p1 = New Pen(SystemColors.ControlDarkDark, 1)
p2 = New Pen(SystemColors.ControlLightLight, 1)
End If
Else ' if mouse is not over the button, the button looks flat
p1 = New Pen(SystemColors.ControlDark, 1.5)
p2 = New Pen(SystemColors.ControlDark, 1.5)
End If
recRegion = New RectangleF(0, 0, Me.Width, Me.Height)
recBorder = New RectangleF(1, 1, Me.Width - 2, Me.Height - 2)
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
a.AddEllipse(recRegion)
Me.Region = New Region(a)
g.DrawArc(p1, recBorder, 180, -180)
g.DrawArc(p2, recBorder, 180, 180)
Dim textSize As SizeF = g.MeasureString(Me.Text, Me.Font)
If Me.mousePressed Then
If Me.ButtonImage Is Nothing Then
g.DrawString(Me.Text, Me.Font, stringBrush, (((Me.Width + 3) - textSize.Width) / 2) + 1, (((Me.Height + 2) - textSize.Height) / 2) + 1)
Else
Dim pt As New Point(((Me.Width + 3) / 12) + 2, ((Me.Height + 2 - 16) / 2) + 2)
Dim rect As New Rectangle(pt, New Size(16, 16))
g.DrawImage(ButtonImage, rect)
g.DrawString(Me.Text, Me.Font, stringBrush, rect.X + rect.Width + 3, (((Me.Height + 2) - textSize.Height) / 2) + 2)
End If
Else
If Me.ButtonImage Is Nothing Then
g.DrawString(Me.Text, Me.Font, stringBrush, (((Me.Width + 3) - textSize.Width) / 2), (((Me.Height + 2) - (textSize.Height)) / 2))
Else
Dim pt As New Point((Me.Width + 3) / 12, (Me.Height + 2 - 16) / 2)
Dim rect As New Rectangle(pt, New Size(16, 16))
g.DrawImage(ButtonImage, rect)
g.DrawString(Me.Text, Me.Font, stringBrush, rect.X + rect.Width + 3, (((Me.Height + 2) - textSize.Height) / 2))
End If
End If
End If
'If the control has focus, an elliptical dotted line is drawn to
'show that the button has focus
If Me.MeGotFocus Then
Dim gotFocusPen As New Pen(Color.Black, 1.3)
Dim recFocus As Rectangle
gotfocuspen.DashStyle = Drawing2D.DashStyle.Dot
gotfocuspen.DashCap = Drawing2D.DashCap.Round
recFocus = New Rectangle(4, 4, Me.Width - 8, Me.Height - 8)
g.DrawEllipse(gotFocusPen, recFocus)
End If
'Dispose pens, brushes and the graphicpath object.
b.Dispose()
p1.Dispose()
p2.Dispose()
stringBrush.Dispose()
a.Dispose()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
#End Region
#Region "Events"
'Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
' 'Redraw the control if it gets resized (in design time)
' Me.Invalidate()
'End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
'if left click, setfocus and draw a pressed button.
If e.Button = MouseButtons.Left Then
mousePressed = True
Me.Focus()
Me.Invalidate()
End If
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
'if left button is up, draw an unpressed button. Don't lose focus yet.
If e.Button = MouseButtons.Left Then
mousePressed = False
Me.Invalidate()
End If
End Sub
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
MyBase.OnGotFocus(e)
' if the button gets focus, draw a button with dotted ellipse
Me.MeGotFocus = True
Me.Invalidate()
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
MyBase.OnLostFocus(e)
Me.MeGotFocus = False
Me.Invalidate()
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
' if the button has focus and the spacebar is pushed, draw a pressed button.
If e.KeyCode = Keys.Space Then
Me.mousePressed = True
Me.Invalidate()
End If
End Sub
Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyUp(e)
' if the button has focus and the spacebar is released, draw an unpressed button.
If e.KeyCode = Keys.Space Then
Me.mousePressed = False
Me.Invalidate()
MyBase.OnClick(e)
End If
End Sub
Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
MyBase.OnMouseEnter(e)
' Overridding this method is important in case buttonstyle is set to popup
Me.MouseOver = True
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
'Overridding this method is important in case buttonstyle is set to popup
Me.MouseOver = False
Me.Invalidate()
End Sub
'Public Shadows Event click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
MyBase.OnClick(e)
mousePressed = False
Me.Invalidate()
End Sub
#End Region
End Class
''' -----------------------------------------------------------------------------
''' <summary>
''' Specify the Flat Style for the EllipticalButton Control
''' </summary>
''' <history>
''' [Ahmed Al-Asaad] 3/27/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Enum Style
''' -----------------------------------------------------------------------------
''' <summary>
''' Specifies the style as standard button
''' </summary>
''' -----------------------------------------------------------------------------
Standard
''' -----------------------------------------------------------------------------
''' <summary>
''' Specifies the style as Popup Button
''' </summary>
''' -----------------------------------------------------------------------------
Popup
End Enum |
Partager