VB.net编程实现Wince下的图片按钮自定义控件

来源:互联网 发布:c语言如何求最小公倍数 编辑:程序博客网 时间:2024/04/29 05:10
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.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.ControlLight, 2), 0, 0, Me.Width, 0)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 2), 0, 0, 0, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), 0, Me.Height, Me.Width, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), Me.Width, 0, Me.Width, Me.Height)
                        cbrush.Dispose()
                        cbrush = Nothing
                        gp.Dispose()
                        gp = Nothing


                    End If
                Case 2
                    '画图像


                    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(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), 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(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), 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(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), 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(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), 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(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), 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.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.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.ControlLight, 2), 0, 0, Me.Width, 0)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 2), 0, 0, 0, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), 0, Me.Height, Me.Width, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), Me.Width, 0, Me.Width, Me.Height)
                        cbrush.Dispose()
                        cbrush = Nothing
                        gp.Dispose()
                        gp = Nothing
                    End If
            End Select
        End If
        '画一条边框
        'e.Graphics.DrawRectangle(New Pen(System.Drawing.SystemColors.ControlDark), 0, 0, Me.ClientSize.Width + 1, Me.ClientSize.Height + 1)
        '============================================================================================================================================
        '大小自动设置时
        If ControlSizeSet = True 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 cHeight As Integer = 0
                    Dim cWidth As Integer = 0
                    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)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 8
                                '虽然预留了8个像素 但是只平均8个像素
                                gp.DrawString(Ctext, Me.Font, cbrush, 4, (cHeight - vsizef.Height) / 2)
                                '图像的X坐标是vsizef.Width + 8


                                Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (cHeight - 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)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight + 2
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 12
                                '图像的Y坐标是(cHeight - ImageHeight) / 2
                                Dim imgRect As Rectangle = New Rectangle(0, (cHeight - 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, (cHeight - vsizef.Height) / 2)
                            Case Layout.up
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '文本起始坐标(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.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)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.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.Width - vsizef.Width) / 2 - 1, ImageHeight + 2)




                            Case Layout.middle
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If
                                '高度预留4个像素
                                Me.Height = cHeight
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.Width - ImageWidth) / 2, (Me.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.Width - vsizef.Width) / 2, (Me.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)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 8
                                '虽然预留了8个像素 但是只平均8个像素
                                gp.DrawString(Ctext, Me.Font, cbrush, 4, (cHeight - vsizef.Height) / 2)
                                '图像的X坐标是vsizef.Width + 8
                                Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (cHeight - 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)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight + 2
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 12
                                '图像的Y坐标是(cHeight - ImageHeight) / 2
                                Dim imgRect As Rectangle = New Rectangle(0, (cHeight - 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, (cHeight - vsizef.Height) / 2)
                            Case Layout.up
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '文本起始坐标(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.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)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.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.Width - vsizef.Width) / 2 - 1, ImageHeight + 2)
                            Case Layout.middle
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If
                                '高度预留4个像素
                                Me.Height = cHeight
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.Width - ImageWidth) / 2, (Me.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.Width - vsizef.Width) / 2, (Me.Height - vsizef.Height) / 2)


                        End Select
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 2), 0, 0, Me.Width, 0)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 2), 0, 0, 0, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), 0, Me.Height, Me.Width, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), Me.Width, 0, Me.Width, Me.Height)
                        cbrush.Dispose()
                        cbrush = Nothing
                        gp.Dispose()
                        gp = Nothing


                    End If
                Case 2
                    '画图像
                    Dim cHeight As Integer = 0
                    Dim cWidth As Integer = 0
                    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)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 8
                                '虽然预留了8个像素 但是只平均8个像素
                                gp.DrawString(Ctext, Me.Font, cbrush, 4, (cHeight - vsizef.Height) / 2)
                                '图像的X坐标是vsizef.Width + 8
                                Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (cHeight - ImageHeight) / 2, ImageWidth, ImageHeight)
                                Dim imageAttr As New ImageAttributes
                                imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
                                imgRect = Nothing
                                imageAttr = Nothing


                            Case Layout.right


                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 8
                                '图像的Y坐标是(cHeight - ImageHeight) / 2
                                Dim imgRect As Rectangle = New Rectangle(0, (cHeight - ImageHeight) / 2, ImageWidth, ImageHeight)
                                Dim imageAttr As New ImageAttributes
                                imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
                                imgRect = Nothing
                                imageAttr = Nothing
                                '虽然预留了8个像素 但是只平均6个像素
                                gp.DrawString(Ctext, Me.Font, cbrush, Me.ImageWidth + 3, (cHeight - vsizef.Height) / 2)
                            Case Layout.up
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '文本起始坐标(3,2)
                                gp.DrawString(Ctext, Me.Font, cbrush, 3, 2)


                                '图像的Y坐标是vsizef.Height + 4
                                Dim imgRect As Rectangle = New Rectangle((Me.Width - ImageWidth) / 2 - 1, vsizef.Height + 4, ImageWidth, ImageHeight)
                                Dim imageAttr As New ImageAttributes
                                imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
                                imgRect = Nothing
                                imageAttr = Nothing


                            Case Layout.down


                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.Width - ImageWidth) / 2 - 1, 0, ImageWidth, ImageHeight)
                                Dim imageAttr As New ImageAttributes
                                imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
                                imgRect = Nothing
                                imageAttr = Nothing
                                '文本起始坐标(3,2)
                                gp.DrawString(Ctext, Me.Font, cbrush, (Me.Width - vsizef.Width) / 2 - 1, ImageHeight + 2)
                            Case Layout.middle
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If
                                '高度预留4个像素
                                Me.Height = cHeight
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.Width - ImageWidth) / 2, (Me.Height - ImageHeight) / 2, ImageWidth, ImageHeight)
                                Dim imageAttr As New ImageAttributes
                                imageAttr.SetColorKey(BackgroundImageColor(Imagelist1.Images.Item(1)), BackgroundImageColor(Imagelist1.Images.Item(1)))
                                gp.DrawImage(Imagelist1.Images.Item(1), imgRect, 0, 0, ImageWidth, ImageHeight, GraphicsUnit.Pixel, imageAttr)
                                imgRect = Nothing
                                imageAttr = Nothing
                                '文本起始坐标(3,2)
                                gp.DrawString(Ctext, Me.Font, cbrush, (Me.Width - vsizef.Width) / 2, (Me.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)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 8
                                '虽然预留了8个像素 但是只平均8个像素
                                gp.DrawString(Ctext, Me.Font, cbrush, 4, (cHeight - vsizef.Height) / 2)
                                '图像的X坐标是vsizef.Width + 8
                                Dim imgRect As Rectangle = New Rectangle(vsizef.Width + 8, (cHeight - 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)
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If


                                '高度取大的
                                Me.Height = cHeight
                                ' 预留8个像素
                                Me.Width = ImageWidth + vsizef.Width + 8
                                '图像的Y坐标是(cHeight - ImageHeight) / 2
                                Dim imgRect As Rectangle = New Rectangle(0, (cHeight - 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, (cHeight - vsizef.Height) / 2)
                            Case Layout.up
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '文本起始坐标(3,2)
                                gp.DrawString(Ctext, Me.Font, cbrush, 3, 2)


                                '图像的Y坐标是vsizef.Height + 4
                                Dim imgRect As Rectangle = New Rectangle((Me.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)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                '高度预留4个像素
                                Me.Height = ImageHeight + vsizef.Height + 4
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth + 8
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.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.Width - vsizef.Width) / 2 - 1, ImageHeight + 2)
                            Case Layout.middle
                                vsizef = gp.MeasureString(Ctext, Me.Font)
                                If Me.ImageWidth > vsizef.Width Then
                                    cWidth = Me.ImageWidth
                                Else
                                    cWidth = vsizef.Width
                                End If
                                If Me.ImageHeight > vsizef.Height Then
                                    cHeight = Me.ImageHeight
                                Else
                                    cHeight = vsizef.Height
                                End If
                                '高度预留4个像素
                                Me.Height = cHeight
                                ' 宽度取大的 预留8个像素
                                Me.Width = cWidth
                                '图像的X坐标是(Me.Width - ImageWidth) / 2 - 1
                                Dim imgRect As Rectangle = New Rectangle((Me.Width - ImageWidth) / 2, (Me.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.Width - vsizef.Width) / 2, (Me.Height - vsizef.Height) / 2)


                        End Select
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 2), 0, 0, Me.Width, 0)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlLight, 2), 0, 0, 0, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), 0, Me.Height, Me.Width, Me.Height)
                        gp.DrawLine(New Pen(System.Drawing.SystemColors.ControlDark, 4), Me.Width, 0, Me.Width, Me.Height)
                        cbrush.Dispose()
                        cbrush = Nothing
                        gp.Dispose()
                        gp = Nothing
                    End If
            End Select
        End If
        '画一条边框
        'e.Graphics.DrawRectangle(New Pen(System.Drawing.SystemColors.ControlDark), 0, 0, Me.ClientSize.Width + 1, Me.ClientSize.Height + 1)
        MyBase.OnPaint(e)
    End Sub
    '确定按钮的宽度高度(工具栏专用)
    Public Sub RefreshPicBtn()
        Dim gp As Graphics = Me.CreateGraphics
        Dim cHeight As Integer = 0
        Dim cWidth As Integer = 0
        Dim vsizef As SizeF


        Ctext = Caption


        Select Case Me.TxtImgLayout
            Case Layout.Left
                vsizef = gp.MeasureString(Ctext, Me.Font)
                If Me.ImageHeight > vsizef.Height Then
                    cHeight = Me.ImageHeight
                Else
                    cHeight = vsizef.Height
                End If


                '高度取大的
                Me.Height = cHeight
                ' 预留8个像素
                Me.Width = ImageWidth + vsizef.Width + 8
            Case Layout.right


                vsizef = gp.MeasureString(Ctext, Me.Font)
                If Me.ImageHeight > vsizef.Height Then
                    cHeight = Me.ImageHeight
                Else
                    cHeight = vsizef.Height
                End If


                '高度取大的
                Me.Height = cHeight
                ' 预留8个像素
                Me.Width = ImageWidth + vsizef.Width + 8
            Case Layout.up
                vsizef = gp.MeasureString(Ctext, Me.Font)
                If Me.ImageWidth > vsizef.Width Then
                    cWidth = Me.ImageWidth
                Else
                    cWidth = vsizef.Width
                End If
                '高度预留4个像素
                Me.Height = ImageHeight + vsizef.Height + 6
                ' 宽度取大的 预留8个像素
                Me.Width = cWidth + 8
            Case Layout.down


                vsizef = gp.MeasureString(Ctext, Me.Font)
                If Me.ImageWidth > vsizef.Width Then
                    cWidth = Me.ImageWidth
                Else
                    cWidth = vsizef.Width
                End If
                '高度预留4个像素
                Me.Height = ImageHeight + vsizef.Height + 6
                ' 宽度取大的 预留8个像素
                Me.Width = cWidth + 8
            Case Layout.middle
                vsizef = gp.MeasureString(Ctext, Me.Font)
                If Me.ImageWidth > vsizef.Width Then
                    cWidth = Me.ImageWidth
                Else
                    cWidth = vsizef.Width
                End If
                If Me.ImageHeight > vsizef.Height Then
                    cHeight = Me.ImageHeight
                Else
                    cHeight = vsizef.Height
                End If
                '高度预留4个像素
                Me.Height = cHeight
                ' 宽度取大的 预留8个像素
                Me.Width = cWidth
        End Select
    End Sub
    '鼠标按下
    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        Me.pressed = True
        Me.Invalidate()
        MyBase.OnMouseDown(e)


    End Sub
    '鼠标抬起
    Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
        Me.pressed = False
        Me.Invalidate()
        MyBase.OnMouseUp(e)
    End Sub


    '颜色变为图片画背景用
    Function MakeBitmap(ByVal ButtonColor As Color, ByVal width As Integer, ByVal height As Integer) As Bitmap
        Dim bmpThis As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(bmpThis)
        g.FillRectangle(New SolidBrush(ButtonColor), 0, 0, bmpThis.Width, bmpThis.Height)
        g.Dispose()
        Return bmpThis


    End Function
    '销毁时清空
    Public Sub DisposePictureButton()
        Me.Imagelist1.Images.Clear()
        Me.Imagelist1.Dispose()
    End Sub
    '获取图片的背景色
    Private Function BackgroundImageColor(ByVal image As Image) As System.Drawing.Color
        Dim bmp As Bitmap = New Bitmap(image)
        Return bmp.GetPixel(0, 0)
    End Function




End Class
原创粉丝点击