⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iconmenuitem.vb

📁 The Management Information System of Library using .NET
💻 VB
字号:
Imports System.ComponentModel
Public Class IconMenuItem
    Inherits System.Windows.Forms.MenuItem
    Private mFont As Font
    Private mBitmap As Bitmap
    Private mColor As Color
    Private minIconSize As Int32
    Private Const cintBuffer As Int32 = 4


    Public Property Font() As Font
        Get
            Return mFont
        End Get
        Set(ByVal Value As Font)
            mFont = Value
        End Set
    End Property
    Public Property Bitmap() As Bitmap
        Get
            Return mBitmap
        End Get
        Set(ByVal Value As Bitmap)
            mBitmap = Value
            Me.mColor = Me.Bitmap.GetPixel(0, 0)
        End Set
    End Property
    Public Property Tcolor() As Color
        Get
            Return mColor
        End Get
        Set(ByVal Value As Color)
            mColor = Value
            mBitmap.MakeTransparent(mColor)
        End Set
    End Property
    Public Property IconSize() As Int16
        Get
            Return minIconSize
        End Get
        Set(ByVal Value As Int16)
            minIconSize = Value
        End Set
    End Property

    Protected Sub InitBaseValue()
        Me.Font = SystemInformation.MenuFont
        Me.IconSize = 24
        Me.OwnerDraw = True
        Me.Bitmap = New Bitmap(16, 16)
    End Sub
    Public Sub New()
        MyBase.New()
        InitBaseValue()
    End Sub
    Public Sub New(ByVal text As String)
        MyBase.New(text)
        InitBaseValue()
    End Sub
    Public Sub New(ByVal text As String, ByVal bitmapA As Bitmap)
        MyBase.New(text)
        Me.Bitmap = bitmapA
        If Not Me.Bitmap Is Nothing Then
            Me.Tcolor = Me.Bitmap.GetPixel(0, 0)
        End If
        InitBaseValue()
    End Sub
    Protected ReadOnly Property IsSelected(ByVal state As DrawItemState) As Boolean
        Get
            Return CType(state And DrawItemState.Selected, Boolean)
        End Get
    End Property

    Protected ReadOnly Property IsDisabled(ByVal state As DrawItemState) As Boolean
        Get
            Return CType(state And DrawItemState.Disabled, Boolean)
        End Get
    End Property
    Protected ReadOnly Property ShortCutText() As String
        Get
            If Me.Shortcut = Shortcut.None Or Me.ShowShortcut = False Then
                Return ""
            End If
            Dim k As Keys = CType(Shortcut, Keys)
            Return TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
        End Get
    End Property

    Protected Function getCurrentTextColor(ByVal State As DrawItemState) As Brush
        If IsDisabled(State) Then
            Return New SolidBrush(SystemColors.GrayText)
        Else
            Return New SolidBrush(SystemColors.WindowText)
        End If
    End Function

    Protected Function getCurrentBackColor(ByVal state As DrawItemState) As SolidBrush
        If IsDisabled(state) Then
            Return New SolidBrush(SystemColors.ControlLightLight)
        End If
        If IsSelected(state) Then
            Return New SolidBrush(Color.LightSteelBlue)
        Else
            Return New SolidBrush(SystemColors.ControlLightLight)
        End If
    End Function
    Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
        MyBase.OnMeasureItem(e)
        If Me.Text = "-" Then
            e.ItemHeight = 4
            Exit Sub
        End If
        Dim mstringFormat As New StringFormat
        mstringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show
        e.ItemWidth = CType(e.Graphics.MeasureString(Me.Text, Me.Font, _
         1000, mstringFormat).Width, Int32) + 2 * cintBuffer
        e.ItemWidth += IconSize

        If Me.Shortcut <> Shortcut.None And Me.ShowShortcut Then
            e.ItemWidth += cintBuffer + CInt(e.Graphics.MeasureString(" " _
            & Me.ShortCutText, Me.Font).Width)
        End If
        e.ItemHeight = IconSize
    End Sub

    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)

        MyBase.OnDrawItem(e)
        Dim mRectBitmap As New RectangleF(e.Bounds.Left, _
        e.Bounds.Top, IconSize, IconSize)
        Dim mRectText As New RectangleF(e.Bounds.Left + IconSize, e.Bounds.Top, e.Bounds.Width - IconSize, IconSize)
        Dim mClientRect As New Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width - 2, e.Bounds.Height - 2)
        '处理分隔符
        If Me.Text = "-" Then
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), mRectBitmap)
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLightLight), mRectText)
            e.Graphics.DrawLine(SystemPens.ControlDark, e.Bounds.Left + IconSize + cintBuffer, e.Bounds.Top + CInt(e.Bounds.Height / 2), _
             e.Bounds.Left + e.Bounds.Width - cintBuffer, e.Bounds.Top + CInt(e.Bounds.Height / 2))
            Exit Sub
        End If

        '针对被选择的menuitem
        If IsSelected(e.State) And Not IsDisabled(e.State) Then
            e.Graphics.FillRectangle(getCurrentBackColor(e.State), mClientRect)
            e.Graphics.DrawRectangle(New Pen(SystemColors.ControlDarkDark), mClientRect)
        Else
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), mRectBitmap)
            e.Graphics.FillRectangle(getCurrentBackColor(e.State), mRectText)
            '.Graphics.FillRectangle(New Brushes, mRectText)
        End If
        '处理文本部分
        Dim mstringFormat As New StringFormat
        mstringFormat.Alignment = StringAlignment.Near
        mstringFormat.LineAlignment = StringAlignment.Center
        mstringFormat.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show
        Dim mRF As RectangleF = RectangleF.Empty
        With mRF
            .X = mRectText.X + cintBuffer
            .Y = mRectText.Y
            .Width = mRectText.Width - cintBuffer
            .Height = mRectText.Height
        End With
        e.Graphics.DrawString(Me.Text, Me.Font, getCurrentTextColor(e.State), mRF, mstringFormat)
        If Me.Shortcut <> Shortcut.None And Me.ShowShortcut Then
            mstringFormat.Alignment = StringAlignment.Far
            e.Graphics.DrawString(Me.ShortCutText, Me.Font, getCurrentTextColor(e.State), mRF, mstringFormat)
        End If

        '处理图像部分
        If Not Me.Bitmap Is Nothing Then
            If Not IsDisabled(e.State) Then
                If IsSelected(e.State) Then
                    ControlPaint.DrawImageDisabled(e.Graphics, Me.Bitmap, e.Bounds.Left + cintBuffer, e.Bounds.Top + cintBuffer, _
                    SystemColors.ControlLight)
                    e.Graphics.DrawImage(Me.Bitmap, e.Bounds.Left + cintBuffer - 1, e.Bounds.Top + cintBuffer - 1)
                    'e.Graphics.DrawImage(Me.Bitmap, e.Bounds.Left + cintBuffer - 1, e.Bounds.Top + cintBuffer - 1, 24, 16)
                Else
                    e.Graphics.DrawImage(Me.Bitmap, e.Bounds.Left + _
                    cintBuffer, e.Bounds.Top + cintBuffer)
                    ' e.Graphics.DrawImage(Me.Bitmap, e.Bounds.Left + _
                    'cintBuffer, e.Bounds.Top + cintBuffer, 24, 16)
                End If
            Else
                ControlPaint.DrawImageDisabled(e.Graphics, Me.Bitmap, _
                e.Bounds.Left + cintBuffer, e.Bounds.Top + _
                cintBuffer, SystemColors.ControlLight)
            End If

        End If

    End Sub
End Class


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -