如何将图像添加到ToolStripMenuItem [英] How to add image to ToolStripMenuItem

查看:659
本文介绍了如何将图像添加到ToolStripMenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ContextMenuStrip的C#winForm项目。我根据使用交互动态地将ToolStripMenuItems添加到ContextMenuStrip。当我添加一个新的ToolStripMenuItem时,我设置了它的Text属性和Image属性。我不知道如何设置Image属性而不从它所在的位置获取图像。如何将想象添加到我的项目中?这是我的代码正在做的一个例子



I have a C# winForm project that uses a ContextMenuStrip. I dynamically add ToolStripMenuItems to the ContextMenuStrip based on use interaction. When I add a new ToolStripMenuItem I set it's Text property and Image property. I don't know how to the set the Image property without getting the image from the location where it's at. How do I add the imagine to my project? Here's an example of what my code is doing

ContextMenuStrip cxtMnuStrp = new ContextMenuStrip;

private void Button_Click(object sender, EventArgs e)
{
   // some filtering and logic
   // to determine weather to 
   // create and add a ToolStripMenuItem
   // blah, blah, blah...
   
   ToolStripMenuItem item = new ToolStripMenuItem("uniqueName");

   item.Image = Image.FromFile(@"C:\MyFolder\MyIcon.ico");

   if (cxtMnuStrp.Items.ContainsKey(item) == false)
       cxtMnuStrp.Items.Add(item);
}





使用item.Image = Image.FromFile(@C:\ MyyFolder \ Myycon.ico )当我分发我的每台机器时,必须有C:\ MyFoler目录,并且在C:\ MyFoler目录中的计算机上也有MyIcon.ico。



另外,每次我想在我的ToolStripMenuItem上添加一个图标时,我都没有点击硬盘驱动器



With "item.Image = Image.FromFile(@"C:\MyFolder\MyIcon.ico")" When I distribute my each machine would have to have the "C:\MyFoler" directory and also have the "MyIcon.ico" on their computer in the "C:\MyFoler" directory.

Plus it doesn't seem right that I have hit the hard drive each time I want to add an icon to my ToolStripMenuItem

推荐答案

将图像作为嵌入资源添加到项目中,然后可以使用Image.FromStream和Assembly.GetManifestResourceStream来加载图像。您还可以添加设置文件并将图像添加到设置文件中,然后只需使用设置文件的属性即可获取图像。
Add the image as an embedded resource in your project, then you can use Image.FromStream and Assembly.GetManifestResourceStream to load your image. You can also add a settings file and add the images to the settings file then just use the properties of the settings file to get the image.


这篇关于如何将图像添加到ToolStripMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆