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
|
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class PictureButtonDemo
Inherits System.Windows.Forms.Form
Private mainMenu1 As System.Windows.Forms.MainMenu
Private clickCount As Integer = 0
' Create a bitmap object, fill it with the specified color.
' To make it look like a custom image, draw an ellipse in it.
Function MakeBitmap(ByVal color As Color, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim bmp As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillRectangle(New SolidBrush(color), 0, 0, bmp.Width, bmp.Height)
g.DrawEllipse(New Pen(color.DarkGray), 3, 3, width - 6, height - 6)
g.Dispose()
Return bmp
End Function
' Create a new PictureButton control and hook up it's properties.
Public Sub New()
'
' Required for Windows Form Designer support
'
InitializeComponent()
Dim button As New PictureButton
button.Parent = Me
button.Bounds = New Rectangle(10, 30, 150, 30)
button.ForeColor = Color.White
button.BackgroundImage = MakeBitmap(Color.Blue, button.Width, button.Height)
button.PressedImage = MakeBitmap(Color.LightBlue, button.Width, button.Height)
button.Text = "click me"
AddHandler button.Click, AddressOf button_Click
End Sub
' Clean up any resources being used.
'
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.mainMenu1 = New System.Windows.Forms.MainMenu
'
' PictureButtonDemo
'
Me.Menu = Me.mainMenu1
Me.Text = "Picture Button Demo"
End Sub 'InitializeComponent
#End Region
'
' The main entry point for the application.
'
Shared Sub Main()
Application.Run(New PictureButtonDemo)
End Sub
' Because PictureButton inherits from Control,
' you can use the default Click event.
Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.clickCount = Me.clickCount + 1
Me.Text = "Click Count = " & Me.clickCount
End Sub
End Class
' This code shows a simle way to have a button-like control
' that has a background image.
Public Class PictureButton
Inherits Control
Private backgroundImg, pressedImg As Image
Private pressed As Boolean = False
' Property for the background image to be drawn behind the button text.
Public Property BackgroundImage() As Image
Get
Return Me.backgroundImg
End Get
Set(ByVal Value As Image)
Me.backgroundImg = Value
End Set
End Property
' Property for the background image to be drawn behind the button text when
' the button is pressed.
Public Property PressedImage() As Image
Get
Return Me.pressedImg
End Get
Set(ByVal Value As Image)
Me.pressedImg = Value
End Set
End Property
'
' When the mouse is pressed, set the "pressed" flag = true and invalidate
' to cause a repaint. The .NET Compact Framework sets the mouse capture automatically.
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
Me.pressed = True
Me.Invalidate()
MyBase.OnMouseDown(e)
End Sub
' When the mouse is released, reset the "pressed" flag and invalidate to redraw the
' button in the un-pressed state.
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
Me.pressed = False
Me.Invalidate()
MyBase.OnMouseUp(e)
End Sub 'OnMouseUp
' Override the OnPaint method so we can draw the background image and the text.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
If Me.pressed AndAlso Not (Me.pressedImg Is Nothing) Then
e.Graphics.DrawImage(Me.pressedImg, 0, 0)
Else
e.Graphics.DrawImage(Me.backgroundImg, 0, 0)
End If
' Draw the text if there is any.
If Me.Text.Length > 0 Then
Dim size As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font)
' Center the text inside the client area of the PictureButton.
e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), (Me.ClientSize.Width - size.Width) / 2, (Me.ClientSize.Height - size.Height) / 2)
End If
' Draw a border around the outside of the control to
' look like Pocket PC buttons.
e.Graphics.DrawRectangle(New Pen(Color.Black), 0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
MyBase.OnPaint(e)
End Sub
End Class |
Partager