tongko liew
November 17th, 2002, 08:30 AM
Hi there,
I tried to create an owner draw menu, and trying to build it as User control which you can add it in from the Toolbox window. I have sucessfully create a custom class which inherits from MenuItem class, but the problem is that I can't add this to the Toolbox window. I can only use it in coding. Can anybody help me on this?
HotMenuItem Class:
Option Explicit On
Option Strict On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Namespace Menu.MenuItem
<ToolboxBitmap("HotMenu.ico")> _
Public Class HotMenuItem
Inherits System.Windows.Forms.MenuItem
#Region "Initialization"
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal caption As String)
MyBase.Text = caption
End Sub
#End Region
#Region "Overrides Method"
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim MyBrush As Brush = Brushes.Black
Dim MySizeF As SizeF = e.Graphics.MeasureString(MyBase.Text, e.Font)
Dim b As New SolidBrush(Color.FromKnownColor(KnownColor.Control))
' Draw the item, and then draw a Rectangle around it.
e.Graphics.FillRectangle(b, e.Bounds)
If (e.State And DrawItemState.HotLight) = DrawItemState.HotLight Then
b.Color = Color.FromArgb(&H7F, &HC0, &HC0, &HFF)
e.Graphics.FillRectangle(b, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
e.Graphics.DrawRectangle(Drawing.Pens.Black, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
ElseIf (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
b.Color = Color.FromArgb(&H7D, Color.FromKnownColor(KnownColor.ControlLight))
e.Graphics.FillRectangle(b, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
e.Graphics.DrawRectangle(Drawing.Pens.Black, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
End If
Dim style As New StringFormat()
style.Alignment = StringAlignment.Center
style.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(MyBase.Text, e.Font, MyBrush, _
New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), style)
End Sub
Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
Dim MyFont As New Font("Tahoma", 8)
Dim MySizeF As SizeF = e.Graphics.MeasureString(MyBase.Text, MyFont)
e.ItemHeight = CType(MySizeF.Height, Integer) + 6
e.ItemWidth = CType(MySizeF.Width, Integer) + 6
End Sub
#End Region
End Class
End Namespace
regards,
Terry.
I tried to create an owner draw menu, and trying to build it as User control which you can add it in from the Toolbox window. I have sucessfully create a custom class which inherits from MenuItem class, but the problem is that I can't add this to the Toolbox window. I can only use it in coding. Can anybody help me on this?
HotMenuItem Class:
Option Explicit On
Option Strict On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Namespace Menu.MenuItem
<ToolboxBitmap("HotMenu.ico")> _
Public Class HotMenuItem
Inherits System.Windows.Forms.MenuItem
#Region "Initialization"
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal caption As String)
MyBase.Text = caption
End Sub
#End Region
#Region "Overrides Method"
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim MyBrush As Brush = Brushes.Black
Dim MySizeF As SizeF = e.Graphics.MeasureString(MyBase.Text, e.Font)
Dim b As New SolidBrush(Color.FromKnownColor(KnownColor.Control))
' Draw the item, and then draw a Rectangle around it.
e.Graphics.FillRectangle(b, e.Bounds)
If (e.State And DrawItemState.HotLight) = DrawItemState.HotLight Then
b.Color = Color.FromArgb(&H7F, &HC0, &HC0, &HFF)
e.Graphics.FillRectangle(b, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
e.Graphics.DrawRectangle(Drawing.Pens.Black, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
ElseIf (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
b.Color = Color.FromArgb(&H7D, Color.FromKnownColor(KnownColor.ControlLight))
e.Graphics.FillRectangle(b, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
e.Graphics.DrawRectangle(Drawing.Pens.Black, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1))
End If
Dim style As New StringFormat()
style.Alignment = StringAlignment.Center
style.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(MyBase.Text, e.Font, MyBrush, _
New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), style)
End Sub
Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
Dim MyFont As New Font("Tahoma", 8)
Dim MySizeF As SizeF = e.Graphics.MeasureString(MyBase.Text, MyFont)
e.ItemHeight = CType(MySizeF.Height, Integer) + 6
e.ItemWidth = CType(MySizeF.Width, Integer) + 6
End Sub
#End Region
End Class
End Namespace
regards,
Terry.