关于在窗体菜单中加图标! 响应MenuItem.DrawItem事件,DrawItemEventArgs参数提供你所需要的一切,就是用Graphics对象自己画菜单。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上的朋友,谢谢但DrawItem事件我用了的,但触发不了。不知是什么情况会触发它 Putting Images Next To MenuItems In A Menu in C#http://www.codeproject.com/cs/miscctrl/cs_menus.asp [System.Runtime.InteropServices.DllImport("user32")] private static extern int SetMenuItemBitmaps(IntPtr hMenu, int nPosition, int wFlags, IntPtr hBitmapUnchecked,IntPtr hBitmapChecked); const int MF_BYPOSITION = 0x0400; private void Form1_Load(object sender, System.EventArgs e) { IntPtr mHandle=this.menuItem1.Handle; Bitmap bp; bp=new Bitmap("a.bmp"); SetMenuItemBitmaps(mHandle,0,MF_BYPOSITION,bp.GetHbitmap(),bp.GetHbitmap()); bp=new Bitmap("b.bmp"); SetMenuItemBitmaps(mHandle,1,MF_BYPOSITION,bp.GetHbitmap(),bp.GetHbitmap()); } [email protected]楼上发给我一份好吗??谢谢 我有一个例子,比较简单,你参考一下:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace WindowsApplication2{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ImageList imageList1; private System.Windows.Forms.ContextMenu contextMenu1; private System.Windows.Forms.MenuItem one; private System.Windows.Forms.MenuItem two; private System.Windows.Forms.MenuItem three; 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.contextMenu1 = new System.Windows.Forms.ContextMenu(); this.one = new System.Windows.Forms.MenuItem(); this.two = new System.Windows.Forms.MenuItem(); this.three = new System.Windows.Forms.MenuItem(); // // 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; // // contextMenu1 // this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.one, this.two, this.three}); // // one // this.one.Index = 0; this.one.Text = "one"; this.one.Click += new System.EventHandler(this.one_Click); // // two // this.two.Index = 1; this.two.Text = "two"; // // three // this.three.Index = 2; this.three.Text = ""; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(520, 273); this.Name = "Form1"; this.Text = "菜单设计"; this.Load += new System.EventHandler(this.Form1_Load); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { MainMenu my1=new MainMenu(); this.Menu=my1; MenuItem file=my1.MenuItems.Add(" 文件"); MenuItem open=file.MenuItems.Add(""); MenuItem save=file.MenuItems.Add("保存"); open.OwnerDraw=true; open.MeasureItem+=new System.Windows.Forms.MeasureItemEventHandler(this.draw_measureitem); open.DrawItem+=new System.Windows.Forms.DrawItemEventHandler(this.draw_drawitem); MenuItem a=file.MenuItems.Add("-"); MenuItem close=file.MenuItems.Add("关闭"); close.Enabled=false; MenuItem open1=open.MenuItems.Add("独占打开",new EventHandler(this.open1_Click )); } private void one_Click(object sender, System.EventArgs e) { MessageBox.Show("one ok!"); } private void open1_Click(object sender, System.EventArgs e) { MessageBox.Show("file1_Click"); } private void draw_drawitem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Icon myicon=new Icon("f:\\1.ico"); Graphics g=e.Graphics; Color my =new Color(); my=System.Drawing.Color.Red; //设置字体 Font myfont=new Font("宋体",10,System.Drawing.FontStyle.Bold|FontStyle.Italic); Brush mybrush=System.Drawing.Brushes.Aquamarine; e.DrawFocusRectangle(); e.DrawBackground(); g.DrawIcon(myicon,0,0); g.DrawString("打开!!",myfont,mybrush,40,10); } private void draw_measureitem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { e.ItemWidth=100; e.ItemHeight=30; } }} 本人新手,急求一段串口通信高频采集代码 c# 流媒体传输 C#中调用Excel中的宏 关于winform 中 datagrid 的问题 怎样判断客户端是否在线 关于List<>.FindIndex的问题。 哪位大哥非常精通c#对PPT的操作 怎么加开隐藏的主窗口 请问高手c#在调试的时候,为什么界面zhogde菜单项不能显示呢 谢谢 帮我去除一下沟goto语句 如何简单的将 10.8.11.13 转换成去了.的四个字符串? 关于aspx中session的问题,请大家帮忙
但DrawItem事件我用了的,但触发不了。
不知是什么情况会触发它
http://www.codeproject.com/cs/miscctrl/cs_menus.asp
private static extern int SetMenuItemBitmaps(IntPtr hMenu, int nPosition, int wFlags, IntPtr hBitmapUnchecked,IntPtr hBitmapChecked);
const int MF_BYPOSITION = 0x0400;
private void Form1_Load(object sender, System.EventArgs e)
{
IntPtr mHandle=this.menuItem1.Handle;
Bitmap bp;
bp=new Bitmap("a.bmp");
SetMenuItemBitmaps(mHandle,0,MF_BYPOSITION,bp.GetHbitmap(),bp.GetHbitmap());
bp=new Bitmap("b.bmp");
SetMenuItemBitmaps(mHandle,1,MF_BYPOSITION,bp.GetHbitmap(),bp.GetHbitmap());
}
楼上发给我一份好吗??
谢谢
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem one;
private System.Windows.Forms.MenuItem two;
private System.Windows.Forms.MenuItem three;
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.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.one = new System.Windows.Forms.MenuItem();
this.two = new System.Windows.Forms.MenuItem();
this.three = new System.Windows.Forms.MenuItem();
//
// 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;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.one,
this.two,
this.three});
//
// one
//
this.one.Index = 0;
this.one.Text = "one";
this.one.Click += new System.EventHandler(this.one_Click);
//
// two
//
this.two.Index = 1;
this.two.Text = "two";
//
// three
//
this.three.Index = 2;
this.three.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 273);
this.Name = "Form1";
this.Text = "菜单设计";
this.Load += new System.EventHandler(this.Form1_Load); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
MainMenu my1=new MainMenu();
this.Menu=my1;
MenuItem file=my1.MenuItems.Add(" 文件");
MenuItem open=file.MenuItems.Add("");
MenuItem save=file.MenuItems.Add("保存"); open.OwnerDraw=true;
open.MeasureItem+=new System.Windows.Forms.MeasureItemEventHandler(this.draw_measureitem);
open.DrawItem+=new System.Windows.Forms.DrawItemEventHandler(this.draw_drawitem);
MenuItem a=file.MenuItems.Add("-");
MenuItem close=file.MenuItems.Add("关闭");
close.Enabled=false;
MenuItem open1=open.MenuItems.Add("独占打开",new EventHandler(this.open1_Click ));
}
private void one_Click(object sender, System.EventArgs e)
{
MessageBox.Show("one ok!");
}
private void open1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("file1_Click");
} private void draw_drawitem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Icon myicon=new Icon("f:\\1.ico");
Graphics g=e.Graphics;
Color my =new Color();
my=System.Drawing.Color.Red;
//设置字体
Font myfont=new Font("宋体",10,System.Drawing.FontStyle.Bold|FontStyle.Italic);
Brush mybrush=System.Drawing.Brushes.Aquamarine;
e.DrawFocusRectangle();
e.DrawBackground();
g.DrawIcon(myicon,0,0);
g.DrawString("打开!!",myfont,mybrush,40,10);
}
private void draw_measureitem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemWidth=100;
e.ItemHeight=30;
}
}
}