Imports System.Reflection
Imports System.Drawing.Imaging
Public Class PictureButtonToolbar
Inherits System.Windows.Forms.UserControl
Public Enum Layout
Left = 0
right = 1
up = 2
down = 3
middle = 4
End Enum
'字段
Private backgroundImgColor As Color = Me.BackColor
Private pressedImgColor As Color = Color.SteelBlue
Private pressed As Boolean = False
Private TxtImgLayout As Layout = 1
'属性
Private Ctext As String = "按钮文本按钮文本"
Private Imagelist1 As New ImageList
Public ImageHeight As Integer = 32
Public ImageWidth As Integer = 32
'当作为工具栏最终重新设置其大小时的变量
Public BtnWidth As Integer = 0
Public BtnHeight As Integer = 0
Public Property setImageHeight() As Integer
Get
Return Me.ImageHeight
End Get
Set(ByVal Value As Integer)
Me.ImageHeight = Value
End Set
End Property
Public Property SetTag() As Integer
Get
Return MyBase.Tag
End Get
Set(ByVal value As Integer)
MyBase.Tag = value
End Set
End Property
'单击按钮后启动的外接程序(平台可以任意添加EXE了)
Public BtnAppName As String = ""
Public Property SetAppName() As String
Get
Return Me.BtnAppName
End Get
Set(ByVal value As String)
Me.BtnAppName = value
End Set
End Property
Public Property setImageWidth() As Integer
Get
Return Me.ImageWidth
End Get
Set(ByVal Value As Integer)
Me.ImageWidth = Value
End Set
End Property
'平时的背景色
Public Property backgroundImgColorValue() As Color
Get
Return Me.backgroundImgColor
End Get
Set(ByVal Value As Color)
Me.backgroundImgColor = Value
End Set
End Property
'鼠标按下后的背景色
Public Property PressedImageColorValue() As Color
Get
Return Me.pressedImgColor
End Get
Set(ByVal Value As Color)
Me.pressedImgColor = Value
End Set
End Property
'显示的文本
Public Property Caption() As String
Get
Return Me.Ctext
End Get
Set(ByVal Value As String)
Me.Ctext = Value
End Set
End Property
'文字的布局
Public Property TextImageLayout() As Layout
Get
Return Me.TxtImgLayout
End Get
Set(ByVal Value As Layout)
Me.TxtImgLayout = Value
End Set
End Property
'控件大小是否自动调整
Private ControlSizeSet As Boolean = False
Public Property ControlSizeSetF() As Boolean
Get
Return ControlSizeSet
End Get
Set(ByVal value As Boolean)
Me.ControlSizeSet = value
End Set
End Property
Public Sub AddBmp(ByVal bmp As Image)
Imagelist1.ImageSize = New System.Drawing.Size(ImageWidth, ImageHeight)
Me.Imagelist1.Images.Add(bmp)
End Sub
'绘制表面
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim gp As Graphics = e.Graphics
Imagelist1.ImageSize = New System.Drawing.Size(ImageWidth, ImageHeight)
'控件大小不自动调整
If ControlSizeSet = False Then
Select Case Imagelist1.Images.Count
Case 0 '画背景
Dim backgroundImg As Image = MakeBitmap(backgroundImgColor, Me.Width, Me.Height)
Dim pressedImg As Image = MakeBitmap(pressedImgColor, Me.Width, Me.Height)
If Me.pressed AndAlso (pressedImg IsNot Nothing) Then
gp.DrawImage(pressedImg, 0, 0)
Else
gp.DrawImage(backgroundImg, 0, 0)
End If
backgroundImg.Dispose()
pressedImg.Dispose()
If Me.Ctext.Length > 0 Then
Dim size As SizeF = e.Graphics.MeasureString(Me.Ctext, Me.Font)
e.Graphics.DrawString(Me.Ctext, Me.Font, New SolidBrush(Me.ForeColor), (Me.ClientSize.Width - size.Width) / 2, (Me.ClientSize.Height - size.Height) / 2)
End If
Case 1 '画图像
Dim vsizef As SizeF
If Me.pressed Then
'文本颜色取按下的颜色
Dim cbrush As New System.Drawing.SolidBrush(Me.pressedImgColor)
Select Case Me.TxtImgLayout
Case Layout.Left
vsizef = gp.MeasureString(Ctext, Me.Font)
'虽然预留了8个像素 但是只平均8个像素
gp.DrawString(Ctext, Me.Font, cbrush, 4, (Me.ClientSize.Height - vsizef.Height) / 2)
'图像的X坐标是vsizef.Width + 8
Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
Case Layout.right
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的Y坐标是(cHeight - ImageHeight) / 2
Dim imgRect As Rectangle = New Rectangle(0, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'虽然预留了8个像素 但是只平均6个像素
gp.DrawString(Ctext, Me.Font, cbrush, Me.ImageWidth + 3, (Me.ClientSize.Height - vsizef.Height) / 2)
Case Layout.up
vsizef = gp.MeasureString(Ctext, Me.Font)
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2, 2)
'图像的Y坐标是vsizef.Height + 4
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2 - 1, vsizef.Height + 4, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
Case Layout.down
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2 - 1, 0, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2 - 1, ImageHeight + 2)
Case Layout.middle
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2, (Me.ClientSize.Height - vsizef.Height) / 2)
End Select
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 2), 0, 0, Me.Width, 0)
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 2), 0, 0, 0, Me.Height)
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 4), 1, Me.Height + 1, Me.Width + 1, Me.Height + 1)
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 4), Me.Width + 1, 1, Me.Width + 1, Me.Height + 1)
cbrush.Dispose()
cbrush = Nothing
gp.Dispose()
gp = Nothing
Else
'文本颜色取前景色
Dim cbrush As New System.Drawing.SolidBrush(Me.ForeColor)
Select Case Me.TxtImgLayout
Case Layout.Left
vsizef = gp.MeasureString(Ctext, Me.Font)
'虽然预留了8个像素 但是只平均8个像素
gp.DrawString(Ctext, Me.Font, cbrush, 4, (Me.ClientSize.Height - vsizef.Height) / 2)
'图像的X坐标是vsizef.Width + 8
Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
Case Layout.right
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的Y坐标是(cHeight - ImageHeight) / 2
Dim imgRect As Rectangle = New Rectangle(0, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'虽然预留了8个像素 但是只平均6个像素
gp.DrawString(Ctext, Me.Font, cbrush, Me.ImageWidth + 3, (Me.ClientSize.Height - vsizef.Height) / 2)
Case Layout.up
vsizef = gp.MeasureString(Ctext, Me.Font)
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2, 2)
'图像的Y坐标是vsizef.Height + 4
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2 - 1, vsizef.Height + 4, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Ite
Imports System.Drawing.Imaging
Public Class PictureButtonToolbar
Inherits System.Windows.Forms.UserControl
Public Enum Layout
Left = 0
right = 1
up = 2
down = 3
middle = 4
End Enum
'字段
Private backgroundImgColor As Color = Me.BackColor
Private pressedImgColor As Color = Color.SteelBlue
Private pressed As Boolean = False
Private TxtImgLayout As Layout = 1
'属性
Private Ctext As String = "按钮文本按钮文本"
Private Imagelist1 As New ImageList
Public ImageHeight As Integer = 32
Public ImageWidth As Integer = 32
'当作为工具栏最终重新设置其大小时的变量
Public BtnWidth As Integer = 0
Public BtnHeight As Integer = 0
Public Property setImageHeight() As Integer
Get
Return Me.ImageHeight
End Get
Set(ByVal Value As Integer)
Me.ImageHeight = Value
End Set
End Property
Public Property SetTag() As Integer
Get
Return MyBase.Tag
End Get
Set(ByVal value As Integer)
MyBase.Tag = value
End Set
End Property
'单击按钮后启动的外接程序(平台可以任意添加EXE了)
Public BtnAppName As String = ""
Public Property SetAppName() As String
Get
Return Me.BtnAppName
End Get
Set(ByVal value As String)
Me.BtnAppName = value
End Set
End Property
Public Property setImageWidth() As Integer
Get
Return Me.ImageWidth
End Get
Set(ByVal Value As Integer)
Me.ImageWidth = Value
End Set
End Property
'平时的背景色
Public Property backgroundImgColorValue() As Color
Get
Return Me.backgroundImgColor
End Get
Set(ByVal Value As Color)
Me.backgroundImgColor = Value
End Set
End Property
'鼠标按下后的背景色
Public Property PressedImageColorValue() As Color
Get
Return Me.pressedImgColor
End Get
Set(ByVal Value As Color)
Me.pressedImgColor = Value
End Set
End Property
'显示的文本
Public Property Caption() As String
Get
Return Me.Ctext
End Get
Set(ByVal Value As String)
Me.Ctext = Value
End Set
End Property
'文字的布局
Public Property TextImageLayout() As Layout
Get
Return Me.TxtImgLayout
End Get
Set(ByVal Value As Layout)
Me.TxtImgLayout = Value
End Set
End Property
'控件大小是否自动调整
Private ControlSizeSet As Boolean = False
Public Property ControlSizeSetF() As Boolean
Get
Return ControlSizeSet
End Get
Set(ByVal value As Boolean)
Me.ControlSizeSet = value
End Set
End Property
Public Sub AddBmp(ByVal bmp As Image)
Imagelist1.ImageSize = New System.Drawing.Size(ImageWidth, ImageHeight)
Me.Imagelist1.Images.Add(bmp)
End Sub
'绘制表面
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim gp As Graphics = e.Graphics
Imagelist1.ImageSize = New System.Drawing.Size(ImageWidth, ImageHeight)
'控件大小不自动调整
If ControlSizeSet = False Then
Select Case Imagelist1.Images.Count
Case 0 '画背景
Dim backgroundImg As Image = MakeBitmap(backgroundImgColor, Me.Width, Me.Height)
Dim pressedImg As Image = MakeBitmap(pressedImgColor, Me.Width, Me.Height)
If Me.pressed AndAlso (pressedImg IsNot Nothing) Then
gp.DrawImage(pressedImg, 0, 0)
Else
gp.DrawImage(backgroundImg, 0, 0)
End If
backgroundImg.Dispose()
pressedImg.Dispose()
If Me.Ctext.Length > 0 Then
Dim size As SizeF = e.Graphics.MeasureString(Me.Ctext, Me.Font)
e.Graphics.DrawString(Me.Ctext, Me.Font, New SolidBrush(Me.ForeColor), (Me.ClientSize.Width - size.Width) / 2, (Me.ClientSize.Height - size.Height) / 2)
End If
Case 1 '画图像
Dim vsizef As SizeF
If Me.pressed Then
'文本颜色取按下的颜色
Dim cbrush As New System.Drawing.SolidBrush(Me.pressedImgColor)
Select Case Me.TxtImgLayout
Case Layout.Left
vsizef = gp.MeasureString(Ctext, Me.Font)
'虽然预留了8个像素 但是只平均8个像素
gp.DrawString(Ctext, Me.Font, cbrush, 4, (Me.ClientSize.Height - vsizef.Height) / 2)
'图像的X坐标是vsizef.Width + 8
Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
Case Layout.right
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的Y坐标是(cHeight - ImageHeight) / 2
Dim imgRect As Rectangle = New Rectangle(0, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'虽然预留了8个像素 但是只平均6个像素
gp.DrawString(Ctext, Me.Font, cbrush, Me.ImageWidth + 3, (Me.ClientSize.Height - vsizef.Height) / 2)
Case Layout.up
vsizef = gp.MeasureString(Ctext, Me.Font)
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2, 2)
'图像的Y坐标是vsizef.Height + 4
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2 - 1, vsizef.Height + 4, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
Case Layout.down
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2 - 1, 0, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2 - 1, ImageHeight + 2)
Case Layout.middle
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2, (Me.ClientSize.Height - vsizef.Height) / 2)
End Select
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 2), 0, 0, Me.Width, 0)
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 2), 0, 0, 0, Me.Height)
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 4), 1, Me.Height + 1, Me.Width + 1, Me.Height + 1)
gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 4), Me.Width + 1, 1, Me.Width + 1, Me.Height + 1)
cbrush.Dispose()
cbrush = Nothing
gp.Dispose()
gp = Nothing
Else
'文本颜色取前景色
Dim cbrush As New System.Drawing.SolidBrush(Me.ForeColor)
Select Case Me.TxtImgLayout
Case Layout.Left
vsizef = gp.MeasureString(Ctext, Me.Font)
'虽然预留了8个像素 但是只平均8个像素
gp.DrawString(Ctext, Me.Font, cbrush, 4, (Me.ClientSize.Height - vsizef.Height) / 2)
'图像的X坐标是vsizef.Width + 8
Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
Case Layout.right
vsizef = gp.MeasureString(Ctext, Me.Font)
'图像的Y坐标是(cHeight - ImageHeight) / 2
Dim imgRect As Rectangle = New Rectangle(0, (Me.ClientSize.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Item(0), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
imgRect = Nothing
imageAttr = Nothing
'虽然预留了8个像素 但是只平均6个像素
gp.DrawString(Ctext, Me.Font, cbrush, Me.ImageWidth + 3, (Me.ClientSize.Height - vsizef.Height) / 2)
Case Layout.up
vsizef = gp.MeasureString(Ctext, Me.Font)
'文本起始坐标(3,2)
gp.DrawString(Ctext, Me.Font, cbrush, (Me.ClientSize.Width - vsizef.Width) / 2, 2)
'图像的Y坐标是vsizef.Height + 4
Dim imgRect As Rectangle = New Rectangle((Me.ClientSize.Width - ImageWidth) / 2 - 1, vsizef.Height + 4, ImageWidth, ImageHeight)
Dim imageAttr As New ImageAttributes
imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(0)), BackgroundImageColor(Imagelist1.Images.Item(0)))
gp.DrawImage(Imagelist1.Images.Ite