.net中的菜单控件中如何在菜单项前面加上图片? You need to implement an owner drawn menu to add icons to it. Take a look at the sample on http://www.msjogren.net/dotnet/eng/samples/dotnet_iconmenu.asp 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你自己参考吧using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Drawing.Drawing2D;namespace WindowsApplication2{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ImageList imageList1; private System.Windows.Forms.Label label1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MenuItem menuItem5; private System.Windows.Forms.MenuItem menuItem6; private System.Windows.Forms.MainMenu mids; private System.Windows.Forms.MenuItem mi; private System.ComponentModel.IContainer components; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.label1 = new System.Windows.Forms.Label(); this.mids = new System.Windows.Forms.MainMenu(); this.mi = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.menuItem4 = new System.Windows.Forms.MenuItem(); this.menuItem5 = new System.Windows.Forms.MenuItem(); this.menuItem6 = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // imageList1 // this.imageList1.ImageSize = new System.Drawing.Size(16, 16); this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); this.imageList1.TransparentColor = System.Drawing.Color.Transparent; // // label1 // this.label1.BackColor = System.Drawing.SystemColors.ActiveCaption; this.label1.Dock = System.Windows.Forms.DockStyle.Top; this.label1.Location = new System.Drawing.Point(0, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(292, 23); this.label1.TabIndex = 0; this.label1.Text = "label1"; // // mids // this.mids.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mi, this.menuItem4}); // // mi // this.mi.Index = 0; this.mi.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3}); this.mi.OwnerDraw = true; this.mi.Text = "我的菜单"; this.mi.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem1_DrawItem); this.mi.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem1_MeasureItem); // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Text = "我的菜单1"; // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.Text = "我的菜单2"; // // menuItem4 // this.menuItem4.Index = 1; this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem5, this.menuItem6}); this.menuItem4.OwnerDraw = true; this.menuItem4.Text = "不是我的菜单"; this.menuItem4.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem1_DrawItem); this.menuItem4.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem1_MeasureItem); // // menuItem5 // this.menuItem5.Index = 0; this.menuItem5.Text = "不是我的菜单1"; // // menuItem6 // this.menuItem6.Index = 1; this.menuItem6.Text = "不是我的菜单2"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.label1); this.Menu = this.mids; this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { } private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // Check the state of the menuItem if ( (e.State & DrawItemState.HotLight) == DrawItemState.HotLight ) { // Draw Hover rectangle DrawHoverRect(e, mi); } else if ( (e.State & DrawItemState.Selected) == DrawItemState.Selected ) { // Draw selection rectangle DrawSelectionRect(e, mi); } else { // if no selection, just draw space // Create the hover rectangle Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height - 1); // Create the hover brush Brush b = new LinearGradientBrush(rect, Color.White, Globals.UnCheckBoxColor, 90f, false); // Fill the rectangle e.Graphics.FillRectangle(b, rect); e.Graphics.DrawRectangle(new Pen(Globals.MainColor), rect); } // Create stringFormat object StringFormat sf = new StringFormat(); // set the Alignment to center sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; // Draw the text e.Graphics.DrawString(mi.Text, Globals.menuFont, new SolidBrush(Globals.TextColor), e.Bounds, sf); Rectangle rect1 = new Rectangle(e.Bounds.X, e.Bounds.Y + 1, 12, e.Bounds.Height - 1); e.Graphics.DrawImage(this.imageList1.Images[1], rect1) ;// MenuItem mi = ((MenuItem)sender);// Graphics g = e.Graphics;// Brush br = new SolidBrush(Color.Black);//// Brush br = new SolidBrush(Color.Black);// Font fnt = new Font(mi.Text, 8, FontStyle.Regular); //12是字体的大小// Rectangle rectCheck = e.Bounds; //这样做是把选钮图片的高度设为文本的高度// rectCheck.Width = rectCheck.Height / SystemInformation.MenuCheckSize.Height;//// rectCheck.Width = SystemInformation.MenuCheckSize.Width * rectCheck.Height / SystemInformation.MenuCheckSize.Height;// Rectangle rectRext = e.Bounds;// rectRext.X += rectCheck.Width;//item的宽度=图片的宽度+文字的宽度// e.DrawBackground();//重写这个函数,可以实现选中项的XP效果// //绘制表示选中的圆点。如果想加入图片,请在这里考虑一下// //设置选项的前景色// // br = SystemBrushes.Menu;//选中的会变成蓝底白字// System.Diagnostics.Debug.WriteLine(e.BackColor.ToString());// Image newImage = Image.FromFile("top1.bmp");// e.Graphics.DrawImage(newImage, rectRext);//// g.DrawString(mi.Text, fnt, br, RectangleF.op_Implicit(rectRext));// //加上边框// e.Graphics.DrawRectangle(Pens.Black, rectRext);// //// e.Graphics.DrawImage(//加上图像 } private void menuItem1_Click(object sender, System.EventArgs e) { } private void menuItem1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { MenuItem mi = (MenuItem) sender; // if the item is a seperator if ( mi.Text == "-" ) { e.ItemHeight = 7; } else { // get the item's text size SizeF miSize = e.Graphics.MeasureString(mi.Text, Globals.menuFont); int scWidth = 0; // get the short cut width if ( mi.Shortcut != Shortcut.None ) { SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), Globals.menuFont); scWidth = Convert.ToInt32(scSize.Width); } // set the bounds int miHeight = Convert.ToInt32(miSize.Height) + 7; if (miHeight < 25) miHeight = Globals.MIN_MENU_HEIGHT; e.ItemHeight = miHeight; e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (Globals.PIC_AREA_SIZE * 2); }// MenuItem mi = ((MenuItem)sender);// Font fnt = new Font(mi.Text, 8, FontStyle.Regular);// SizeF szf = e.Graphics.MeasureString(mi.Text ,fnt,1000);// e.ItemWidth = Convert.ToInt32(Math.Ceiling(szf.Width));// e.ItemHeight = Convert.ToInt32(Math.Ceiling(szf.Height));//// e.ItemWidth += SystemInformation.MenuCheckSize.Width + e.ItemHeight / SystemInformation.MenuCheckSize.Height;// e.ItemWidth += e.ItemHeight / SystemInformation.MenuCheckSize.Height; } /// <summary> /// Draws the hover rectangle in case of a mouse is hovering /// </summary> private static void DrawHoverRect(System.Windows.Forms.DrawItemEventArgs e, MenuItem mi) { // Create the hover rectangle Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height - 2); // Create the hover brush Brush b = new LinearGradientBrush(rect, Color.White, Globals.CheckBoxColor, 90f, false); // Fill the rectangle e.Graphics.FillRectangle(b, rect); // Draw borders e.Graphics.DrawRectangle(new Pen(Color.Black), rect); } /// <summary> /// Draws a selection rectangle /// </summary> private static void DrawSelectionRect(System.Windows.Forms.DrawItemEventArgs e, MenuItem mi) { // Create the selection rectangle Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height - 2); // Create the selectino brush Brush b = new LinearGradientBrush(rect, Globals.MenuBgColor, Globals.MenuDarkColor2, 90f, false); // fill the rectangle e.Graphics.FillRectangle(b, rect); // Draw borders e.Graphics.DrawRectangle(new Pen(Color.Black), rect); } }} http://www.codeproject.com/cs/menu/MhOffice2003Menus.asp写的不错,还有源码,你看看吧 双网卡 UDP 通讯问题 decimal的有效位数 求基于C#的股票 webservice 免费的和收费的都可以 我想做一个远程下载的程序,不知该如何做,请高手帮忙? 限制文件下载速度(请高手帮忙) 关于随机问题 C#怎么实现让聊天工具自动回复~~~~ 关于用Syncfution控件画图最大化问题 如何将dataset赋值给一个数组?? 关于控件DOCK的问题,急!! 想制作安装程序,没有做过,帮忙(100分不够再加) 关于反射的问题
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MainMenu mids;
private System.Windows.Forms.MenuItem mi;
private System.ComponentModel.IContainer components; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.label1 = new System.Windows.Forms.Label();
this.mids = new System.Windows.Forms.MainMenu();
this.mi = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(292, 23);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// mids
//
this.mids.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mi,
this.menuItem4});
//
// mi
//
this.mi.Index = 0;
this.mi.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.mi.OwnerDraw = true;
this.mi.Text = "我的菜单";
this.mi.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem1_DrawItem);
this.mi.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem1_MeasureItem);
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "我的菜单1";
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "我的菜单2";
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem6});
this.menuItem4.OwnerDraw = true;
this.menuItem4.Text = "不是我的菜单";
this.menuItem4.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem1_DrawItem);
this.menuItem4.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem1_MeasureItem);
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = "不是我的菜单1";
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "不是我的菜单2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Menu = this.mids;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
} private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
// Check the state of the menuItem
if ( (e.State & DrawItemState.HotLight) == DrawItemState.HotLight )
{
// Draw Hover rectangle
DrawHoverRect(e, mi);
}
else if ( (e.State & DrawItemState.Selected) == DrawItemState.Selected )
{
// Draw selection rectangle
DrawSelectionRect(e, mi);
}
else
{
// if no selection, just draw space
// Create the hover rectangle
Rectangle rect = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
e.Bounds.Width,
e.Bounds.Height - 1); // Create the hover brush
Brush b = new LinearGradientBrush(rect,
Color.White,
Globals.UnCheckBoxColor,
90f, false); // Fill the rectangle
e.Graphics.FillRectangle(b, rect);
e.Graphics.DrawRectangle(new Pen(Globals.MainColor), rect);
}
// Create stringFormat object
StringFormat sf = new StringFormat(); // set the Alignment to center
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center; // Draw the text
e.Graphics.DrawString(mi.Text,
Globals.menuFont,
new SolidBrush(Globals.TextColor),
e.Bounds,
sf);
Rectangle rect1 = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
12,
e.Bounds.Height - 1);
e.Graphics.DrawImage(this.imageList1.Images[1], rect1) ;
// MenuItem mi = ((MenuItem)sender);
// Graphics g = e.Graphics;
// Brush br = new SolidBrush(Color.Black);
//// Brush br = new SolidBrush(Color.Black);
// Font fnt = new Font(mi.Text, 8, FontStyle.Regular); //12是字体的大小
// Rectangle rectCheck = e.Bounds; //这样做是把选钮图片的高度设为文本的高度
// rectCheck.Width = rectCheck.Height / SystemInformation.MenuCheckSize.Height;
//// rectCheck.Width = SystemInformation.MenuCheckSize.Width * rectCheck.Height / SystemInformation.MenuCheckSize.Height;
// Rectangle rectRext = e.Bounds;
// rectRext.X += rectCheck.Width;//item的宽度=图片的宽度+文字的宽度
// e.DrawBackground();//重写这个函数,可以实现选中项的XP效果
// //绘制表示选中的圆点。如果想加入图片,请在这里考虑一下
// //设置选项的前景色
// // br = SystemBrushes.Menu;//选中的会变成蓝底白字
// System.Diagnostics.Debug.WriteLine(e.BackColor.ToString());
// Image newImage = Image.FromFile("top1.bmp");
// e.Graphics.DrawImage(newImage, rectRext);
//
// g.DrawString(mi.Text, fnt, br, RectangleF.op_Implicit(rectRext));
// //加上边框
// e.Graphics.DrawRectangle(Pens.Black, rectRext);
//
//// e.Graphics.DrawImage(//加上图像
} private void menuItem1_Click(object sender, System.EventArgs e)
{
}
{
MenuItem mi = (MenuItem) sender;
// if the item is a seperator
if ( mi.Text == "-" )
{
e.ItemHeight = 7;
}
else
{
// get the item's text size
SizeF miSize = e.Graphics.MeasureString(mi.Text, Globals.menuFont);
int scWidth = 0;
// get the short cut width
if ( mi.Shortcut != Shortcut.None )
{
SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), Globals.menuFont);
scWidth = Convert.ToInt32(scSize.Width);
}
// set the bounds
int miHeight = Convert.ToInt32(miSize.Height) + 7;
if (miHeight < 25) miHeight = Globals.MIN_MENU_HEIGHT;
e.ItemHeight = miHeight;
e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (Globals.PIC_AREA_SIZE * 2);
}
// MenuItem mi = ((MenuItem)sender);
// Font fnt = new Font(mi.Text, 8, FontStyle.Regular);
// SizeF szf = e.Graphics.MeasureString(mi.Text ,fnt,1000);
// e.ItemWidth = Convert.ToInt32(Math.Ceiling(szf.Width));
// e.ItemHeight = Convert.ToInt32(Math.Ceiling(szf.Height));
//// e.ItemWidth += SystemInformation.MenuCheckSize.Width + e.ItemHeight / SystemInformation.MenuCheckSize.Height;
// e.ItemWidth += e.ItemHeight / SystemInformation.MenuCheckSize.Height;
}
/// <summary>
/// Draws the hover rectangle in case of a mouse is hovering
/// </summary>
private static void DrawHoverRect(System.Windows.Forms.DrawItemEventArgs e, MenuItem mi)
{
// Create the hover rectangle
Rectangle rect = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
e.Bounds.Width,
e.Bounds.Height - 2); // Create the hover brush
Brush b = new LinearGradientBrush(rect,
Color.White,
Globals.CheckBoxColor,
90f, false); // Fill the rectangle
e.Graphics.FillRectangle(b, rect); // Draw borders
e.Graphics.DrawRectangle(new Pen(Color.Black), rect);
} /// <summary>
/// Draws a selection rectangle
/// </summary>
private static void DrawSelectionRect(System.Windows.Forms.DrawItemEventArgs e, MenuItem mi)
{
// Create the selection rectangle
Rectangle rect = new Rectangle(e.Bounds.X,
e.Bounds.Y + 1,
e.Bounds.Width,
e.Bounds.Height - 2); // Create the selectino brush
Brush b = new LinearGradientBrush(rect,
Globals.MenuBgColor,
Globals.MenuDarkColor2,
90f, false); // fill the rectangle
e.Graphics.FillRectangle(b, rect); // Draw borders
e.Graphics.DrawRectangle(new Pen(Color.Black), rect);
}
}
}
写的不错,还有源码,你看看吧