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

📄 mymenu.vb

📁 The Management Information System of Library using .NET
💻 VB
字号:
Imports System.ComponentModel
Public Class mymenu
    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
#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化
        InitBaseValue()

    End Sub

    'UserControl 重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

#End Region

    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
    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 * Me.cintBuffer
        e.ItemWidth += IconSize

        If Me.Shortcut <> Shortcut.None And Me.Shortcut 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 + IconSize, _
        e.Bounds.Top, e.Bounds.Width - IconSize, IconSize)
        Dim mRectText As New RectangleF(e.Bounds.Width + 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)
        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)
                Else
                    e.Graphics.DrawImage(Me.Bitmap, e.Bounds.Left + _
                    cintBuffer, e.Bounds.Top + cintBuffer)
                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 + -