新手问:关于在contextMenu中加入Icon的问题

RnfShadow 2003-08-07 07:34:15
我做了一个contextMenu,又做了一个imageList,并在imageList中加入了Icon,但不知道如何将这些Icon加入到contextMenu中去,请各位高手指点迷津!
我是一个不折不扣的初学者,各位高手可否把主要的代码写上来,万分感谢!!
...全文
236 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
RnfShadow 2003-08-10
  • 打赏
  • 举报
回复
哦,原来ContextMenu是不行的,一定要用mainMenu,然后自己绘画上去。。。。。Ms真是麻烦!!谢谢你了袁飞大侠!!
y1g1y1 2003-08-09
  • 打赏
  • 举报
回复
把你的contextMenu 要设置为某一个控件的 ContextMenu 试试?
y1g1y1 2003-08-09
  • 打赏
  • 举报
回复
选了 menuicon 之后,设计状态是不会显示图标,你需要运行才行看到效果.
y1g1y1 2003-08-09
  • 打赏
  • 举报
回复
to RnfShadow():
不用你编写编写代码的.你在设计状态,vs.net 自动会完成的.
angelior 2003-08-09
  • 打赏
  • 举报
回复
學一點
RnfShadow 2003-08-09
  • 打赏
  • 举报
回复
我按照你的方法做了之后,在mainMenu中可以看到自己加的Icon了,但是在contextMenu中还是不行。我看了你的例子程序,好像和我的一样,但为什么你的contextMenu里面可以显示Icon但我的不行呢??麻烦你再为我解答一下。呵呵...真不好意思,麻烦你了,袁飞大侠....
RnfShadow 2003-08-08
  • 打赏
  • 举报
回复
袁飞大侠,我看了你的实例,真的很不错。我在自己的程序里面引用了你的YuanFeiXPControls.dll,但我如何才能在菜单项的属性里面有 外观->MenuIcon 选项呢??
我试过加入一句private YuanFeiSoftware.YuanFeiXPControls.DNetMenu MyMenu;
跟着加入this.MyMenu = new YuanFeiSoftware.YuanFeiXPControls.DNetMenu();
就有了MenuIcon的选项,但是选了之后菜单还是没有变化,我应该怎么做呢???
y1g1y1 2003-08-08
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2093/2093334.xml?temp=.3760797
甴曱 2003-08-08
  • 打赏
  • 举报
回复
我机子上的.NET不有装上,用不了,给你一个网址,你自己转换一下代码不就行了?
http://expert.csdn.net/Expert/topic/2087/2087217.xml?temp=9.482974E-02
RnfShadow 2003-08-08
  • 打赏
  • 举报
回复
请问可以给我c#的代码吗??VC和VB的我不是太懂啊!!拜托拜托!!
孟子E章 2003-08-07
  • 打赏
  • 举报
回复
改一下

Adding icons to menus in VB.NET

'IconsMenuMain.vb
'Module for adding icons to menus...

Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Windows.Forms


Module IconsMenuMain

Dim m_Font As New Font("Arial", 8)

Sub MeasureItems(ByVal EvMeasureItem As System.Windows.Forms.MeasureItemEventArgs, _
ByVal Mi As MenuItem)
Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(60, New Single() {0})
EvMeasureItem.ItemHeight = 22
EvMeasureItem.ItemWidth = CInt(EvMeasureItem.Graphics.MeasureString(GetRealText(Mi), _
m_Font, 10000, sf).Width) + 10
sf.Dispose()
sf = Nothing
End Sub

Sub DrawItems(ByVal EvDrawItems As System.Windows.Forms.DrawItemEventArgs, _
ByVal Mi As MenuItem, ByVal m_Icon As Icon)
Dim br As Brush
Dim fDisposeBrush As Boolean
If Not m_Icon Is Nothing Then
If Not Mi.Checked Then
EvDrawItems.Graphics.DrawIcon(m_Icon, EvDrawItems.Bounds.Left + 2, _
EvDrawItems.Bounds.Top + 2)
Else
EvDrawItems.Graphics.DrawIcon(m_Icon, EvDrawItems.Bounds.Left + 2, _
EvDrawItems.Bounds.Top + 2)
Dim nPen As System.Drawing.Pen
If Not Mi.Enabled Then
NPEN = New Pen(Color.DarkGray)
Else
nPen = New Pen(Color.Gray)
End If
EvDrawItems.Graphics.DrawRectangle(nPen, 1, EvDrawItems.Bounds.Top, 20, 20)
EvDrawItems.Graphics.DrawRectangle(nPen, 3, EvDrawItems.Bounds.Top + 2,
16, 16)
End If
Else
If Mi.Checked Then
Dim nPen As System.Drawing.Pen
If Not Mi.Enabled Then
NPEN = New Pen(Color.DarkGray)
Else
nPen = New Pen(Color.Gray)
End If
EvDrawItems.Graphics.DrawRectangle(nPen, 1, EvDrawItems.Bounds.Top, 20, 20)
Dim Pnts() As Point
ReDim Pnts(2)
Pnts(0) = New Point(15, EvDrawItems.Bounds.Top + 6)
Pnts(1) = New Point(8, EvDrawItems.Bounds.Top + 13)
Pnts(2) = New Point(5, EvDrawItems.Bounds.Top + 10)
If Mi.Enabled Then
EvDrawItems.Graphics.DrawLines(New Pen(Color.Black), Pnts)
Else
EvDrawItems.Graphics.DrawLines(New Pen(Color.Gray), Pnts)
End If
End If
End If
Dim rcBk As Rectangle = EvDrawItems.Bounds
rcBk.X += 24

If CBool(EvDrawItems.State And DrawItemState.Selected) Then
br = New LinearGradientBrush(rcBk, Color.MidnightBlue, Color.LightBlue, 0)
fDisposeBrush = True
Else
br = SystemBrushes.Control
End If

EvDrawItems.Graphics.FillRectangle(br, rcBk)
If fDisposeBrush Then br.Dispose()
br = Nothing

Dim sf As StringFormat = New StringFormat()
sf.HotkeyPrefix = HotkeyPrefix.Show
sf.SetTabStops(60, New Single() {0})
If Mi.Enabled Then
br = New SolidBrush(EvDrawItems.ForeColor)
Else
br = New SolidBrush(Color.Gray)
End If

EvDrawItems.Graphics.DrawString(GetRealText(Mi), m_Font, br, _
EvDrawItems.Bounds.Left + 25, _
EvDrawItems.Bounds.Top + 2, sf)
br.Dispose()
br = Nothing
sf.Dispose()
sf = Nothing
End Sub

Function GetRealText(ByVal Mi As MenuItem) As String
Dim s As String = Mi.Text
If Mi.ShowShortcut And Mi.Shortcut <> Shortcut.None Then
Dim k As Keys = CType(Mi.Shortcut, Keys)
s = s & Convert.ToChar(9) & _
TypeDescriptor.GetConverter(GetType(Keys)).ConvertToString(k)
End If
Return s
End Function

End Module


'**************
'In the items of menu which you want add icon modify the property OwnerDraw to TRUE
'For use this code only add the next references in the form...

Private Sub MenuItem3_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles MenuItem3.DrawItem

Dim Ic As New Icon("C:\Documents and Settings\Yo\Escritorio\iconmenu\Save.ico")
DrawItems(e, MenuItem3, Nothing)
End Sub

Private Sub MenuItem3_MeasureItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
Handles MenuItem3.MeasureItem

MeasureItems(e, MenuItem3)
End Sub
RnfShadow 2003-08-07
  • 打赏
  • 举报
回复
那个例子是VC的,有C#的例子吗??
saucer 2003-08-07
  • 打赏
  • 举报
回复
10.11 How can I add an icon to my menu items?
http://www.syncfusion.com/FAQ/WinForms/FAQ_c49c.asp#q864q
RnfShadow 2003-08-07
  • 打赏
  • 举报
回复
可以写代码给我吗??我真的是初学者,不会怎么做!!谢了。。。
panyee 2003-08-07
  • 打赏
  • 举报
回复
要用ownerDraw,把图标画上去

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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