作了一个测试程序,在form上单击右键可以弹出重画后的菜单,但是右击托盘图标可以弹出菜单,但是无法完成重画功能,连菜单上的字都不见了。请问怎么回事?代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace ttttttttttt
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.NotifyIcon notifyIcon1;
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.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
// 
// contextMenu1
// 
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
 this.menuItem1,
 this.menuItem2,
 this.menuItem3});
// 
// menuItem1
// 
this.menuItem1.Index = 0;
this.menuItem1.OwnerDraw = true;
this.menuItem1.Text = "copy";
this.menuItem1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem1_DrawItem);
this.menuItem1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem1_MeasureItem);
// 
// menuItem2
// 
this.menuItem2.Index = 1;
this.menuItem2.OwnerDraw = true;
this.menuItem2.Text = "cut";
this.menuItem2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem2_DrawItem);
this.menuItem2.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem2_MeasureItem);
// 
// menuItem3
// 
this.menuItem3.Index = 2;
this.menuItem3.OwnerDraw = true;
this.menuItem3.Text = "delete";
this.menuItem3.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.menuItem3_DrawItem);
this.menuItem3.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.menuItem3_MeasureItem);
// 
// notifyIcon1
// 
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Visible = true;
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 272);
this.ContextMenu = this.contextMenu1;
this.Name = "Form1";
this.Text = "Form1"; }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g=e.Graphics; 
if(e.State==DrawItemState.NoAccelerator)//一开始右键单击出现菜单,但是鼠标并没有移上去 
{ //用白色的底色 
g.FillRectangle(new SolidBrush(Color.FromName("Control")),e.Bounds.X,e.Bounds.Y,150,20); 

//鼠标移上去,但是并没有单击 
if ((e.State & DrawItemState.Selected)==DrawItemState.Selected) 

//花边框和底色 
g.FillRectangle(new SolidBrush(Color.FromArgb(255,049,150,231)),e.Bounds.X,e.Bounds.Y,150,20);  } 

//显示文字 
MenuItem   mi   =   (MenuItem)   sender;
e.Graphics.DrawString(mi.Text,new Font("宋体",12), new   SolidBrush(Color.Red),   e.Bounds.X+25,e.Bounds.Y);  e.Graphics.DrawImage(System.Drawing.Image.FromFile(@"D:\5000个ICO图标文件\Dragdrop\DROPFLDR.ICO"),e.Bounds.X+5,e.Bounds.Y+2)   ;
} private void menuItem1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemHeight=20; 
e.ItemWidth=100; 
} private void menuItem3_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g=e.Graphics; 
if(e.State==DrawItemState.NoAccelerator)//一开始右键单击出现菜单,但是鼠标并没有移上去 
{ //用白色的底色 
g.FillRectangle(new SolidBrush(Color.FromName("Control")),e.Bounds.X,e.Bounds.Y,150,20); 

//鼠标移上去,但是并没有单击 
if ((e.State & DrawItemState.Selected)==DrawItemState.Selected) 

//花边框和底色 
g.FillRectangle(new SolidBrush(Color.FromArgb(255,049,150,231)),e.Bounds.X,e.Bounds.Y,150,20);  } 

//显示文字 
MenuItem   mi   =   (MenuItem)   sender;
//   Draw   the   text
e.Graphics.DrawString(mi.Text,new Font("宋体",12), new   SolidBrush(Color.Red),   e.Bounds.X+25,e.Bounds.Y);
e.Graphics.DrawImage(System.Drawing.Image.FromFile(@"D:\5000个ICO图标文件\Dragdrop\DRAG3PG.ICO"),e.Bounds.X+5,e.Bounds.Y+2)   ;
}
private void menuItem3_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemHeight=20; 
e.ItemWidth=100; 
}
private void menuItem2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g=e.Graphics; 
if(e.State==DrawItemState.NoAccelerator)//一开始右键单击出现菜单,但是鼠标并没有移上去 
{ //用白色的底色 
g.FillRectangle(new SolidBrush(Color.FromName("Control")),e.Bounds.X,e.Bounds.Y,150,20); 

//鼠标移上去,但是并没有单击 
if ((e.State & DrawItemState.Selected)==DrawItemState.Selected) 

//花边框和底色 
g.FillRectangle(new SolidBrush(Color.FromArgb(255,049,150,231)),e.Bounds.X,e.Bounds.Y,150,20);  } 

//显示文字  MenuItem   mi   =   (MenuItem)   sender;
//   Draw   the   text
e.Graphics.DrawString(mi.Text,new Font("宋体",12), new   SolidBrush(Color.Red),   e.Bounds.X+25,e.Bounds.Y);
e.Graphics.DrawImage(System.Drawing.Image.FromFile(@"D:\5000个ICO图标文件\Dragdrop\DROP1PG.ICO"),e.Bounds.X+5,e.Bounds.Y+2)   ;
} private void menuItem2_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemHeight=20; 
e.ItemWidth=100; 
}
}
}

解决方案 »

  1.   

    不是菜单的问题,是托盘的问题,
    用这个试试.
    http://www.codeproject.com/cs/miscctrl/notifyiconex.asp
      

  2.   

    我要的是单击托盘的图标生成的菜单的重画代码。如果不重画的话,菜单上的选项列表可以显示,但是我现在要的是菜单的选项列表前面带有小图片。所以这就涉及到了重画问题。当把菜单的 OwnerDraw 设置为true的时候,就无法显示选项列表了
      

  3.   

    唉,,http://www.codeproject.com/cs/miscctrl/notifyiconex.asp
    这上面的是托盘的例子,
    你用这个托盘,,再随便弄个重画的contextmenu(搜一下,到处都有),
    或者就用你自己的.
    试试!
      

  4.   

    http://briefcase.tom.com/folder.php?directory_id=4376733到这里下载menu.rar,看是否符合你要求