怎样给菜单项加图标?

2zhk 2004-10-21 09:12:17
在VB.NET中怎样给菜单项加图标?
...全文
111 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
2zhk 2004-10-21
  • 打赏
  • 举报
回复
先谢过程序员,待我先试一下.
yikais 2004-10-21
  • 打赏
  • 举报
回复
mark
Rotaxe 2004-10-21
  • 打赏
  • 举报
回复
使用代码如下
Private Sub frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mnuMain.MenuItems.Add("&File")
Dim miFile As MenuItem = mnuMain.MenuItems(0)
Dim handlerFile As EventHandler = New EventHandler(AddressOf OpenFileClick)

' By using this constructor the menu items will show up in whatever system
' colors are chosen by the user in control panel or by their theme choice.
miFile.MenuItems.Add(New IconMenuItem("&Open", New Icon("..\open.ico"), _
handlerFile, Shortcut.CtrlO))
end sub
Private Sub OpenFileClick(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("You selected open")
End Sub
Rotaxe 2004-10-21
  • 打赏
  • 举报
回复
要用ownerdraw.微软的例子
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Reflection
Imports System.Resources
Imports System.Windows.Forms

Public Class IconMenuItem : Inherits MenuItem

Private m_Icon As Icon
Private m_Font As Font
' By default these are set to the SystemColors Highight and Control values.
' This allows the appropriate color to be displayed if the user changes
' themes or display settings.
' These can be overriden by calling the appropriate constructor for this
' class.
Private m_Gradient_Color1 As Color = SystemColors.Highlight
Private m_Gradient_Color2 As Color = SystemColors.Control

Public Sub New()
MyClass.New("", Nothing, Nothing, System.Windows.Forms.Shortcut.None)
End Sub

Public Sub New(ByVal text As String, ByVal icon As Icon, ByVal onClick As EventHandler, ByVal shortcut As Shortcut)
MyBase.New(text, onClick, shortcut)
' Owner Draw Property allows you to modify the menu item by handling
' OnMeasureItem and OnDrawItem
OwnerDraw = True
m_Font = New Font("Times New Roman", 8)
m_Icon = icon
End Sub

' Additional constructor allows the setting of custom colors for each part of the menu
' color gradient.
Public Sub New(ByVal text As String, ByVal GradientColor1 As System.Drawing.Color, ByVal GradientColor2 As System.Drawing.Color, ByVal icon As Icon, ByVal onClick As EventHandler, ByVal shortcut As Shortcut)
MyBase.New(text, onClick, shortcut)
' Key Property
OwnerDraw = True
m_Font = New Font("Times New Roman", 8)
m_Gradient_Color1 = GradientColor1
m_Gradient_Color2 = GradientColor2
m_Icon = icon
End Sub

Private Function GetRealText() As String
Dim s As String = Text

' Append shortcut if one is set and it should be visible
If ShowShortcut And Shortcut <> Shortcut.None Then
' To get a string representation of a Shortcut value, cast
' it into a Keys value and use the KeysConverter class (via TypeDescriptor).
Dim k As Keys = CType(Shortcut, Keys)
s = s & Convert.ToChar(9) & TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
End If

Return s
End Function


Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
' OnDrawItem perfoms the task of actually drawing the item after
' measurement is complete
MyBase.OnDrawItem(e)

Dim br As Brush

If Not m_Icon Is Nothing Then
e.Graphics.DrawIcon(m_Icon, e.Bounds.Left + 2, e.Bounds.Top + 2)
End If

Dim rcBk As Rectangle = e.Bounds
rcBk.X += 22

' Draw a background to the menu item with a linear gradient.
' This will use system defaults unless colors and have been
' passed on menu item instantiation
If CBool(e.State And DrawItemState.Selected) Then
br = New LinearGradientBrush(rcBk, m_Gradient_Color1, m_Gradient_Color2, 0)
Else
br = SystemBrushes.Control
End If

' Draw the main rectangle
e.Graphics.FillRectangle(br, rcBk)

' Leave room for accelerator key
Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show

' Draw the actual menu text
br = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(GetRealText(), m_Font, br, e.Bounds.Left + 25, e.Bounds.Top + 2, sf)

End Sub

Protected Overrides Sub OnMeasureItem(ByVal e As MeasureItemEventArgs)
' The MeasureItem event along with the OnDrawItem event are the two key events
' that need to be handled in order to create owner drawn menus.
' Measure the string that makes up a given menu item and use it to set the
' size of the menu item being drawn.

Dim sf As New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
MyBase.OnMeasureItem(e)
e.ItemHeight = 22
e.ItemWidth = CInt(e.Graphics.MeasureString(GetRealText(), m_Font, 10000, sf).Width) + 10

End Sub

End Class
haoztao 2004-10-21
  • 打赏
  • 举报
回复
找本入门书看看呗,好象首先要添加一个image控件,然后调用里边得图标

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧