作了一个测试程序,在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;
}
}
}
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;
}
}
}
解决方案 »
- C#如何将其他类型转换成Image类型?
- c# 向oracle 插入date 类型
- 如何查看WebService的soap header
- CSDN的版面设计是不是要改一下了?
- 在 c#中把自定义的结构消息体通过udp协议发送到服务器端,如何写这段代码呀? 急!!!
- 读取RSS问题这方面的高手近来看看
- 很急的一个问题,希望高手帮个忙!!!在线等
- 极度郁闷中,,,,,,,,高人进来~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 表达式树不能包含动态操作
- [都攒了200分]难道没有人要吗????放了这么长时间了,为什么你们不要呢?????
- 高分求助:请教高手如何在AxWebBrowser控件加载页面后让指定的按钮执行自定义的函数?
- 主窗体有一控件A..在另外一个类中如何调用该控件呢。。?
用这个试试.
http://www.codeproject.com/cs/miscctrl/notifyiconex.asp
这上面的是托盘的例子,
你用这个托盘,,再随便弄个重画的contextmenu(搜一下,到处都有),
或者就用你自己的.
试试!