向面板添加图标 [英] adding icons to panel

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

问题描述

先生!
我们可以在面板上添加图标吗?应该使用哪个模板来创建面板控件.
请我不知道....
在此先感谢...

sir!
can we add icons to panel ! which template should be use to create panel control.
please I''ve not idea of that....
Thanks in advance...

推荐答案

图标不是控件,因此无法像添加控件一样将其添加到面板中.您只能在任何控件的客户区中绘制图标.请参阅:
http://msdn.microsoft.com/en-us/library/system.drawing. icon.aspx [^ ].

也许最接近您的需求解决方案的是:创建一些自定义控件,在其中呈现一些图标.在您的组框中插入此类控件的实例.要在控件上绘制包括图标在内的东西,您需要处理其事件Paint,或者更好的是,重写其虚拟方法OnPaint.无论哪种方式,都不要创建System.Drawing.Graphics的实例(这是一个非常常见的错误),而是将传递给您的实例用作重写方法的事件处理程序的事件参数参数.使用方法System.Drawing.Graphics.DrawIcon:
http://msdn.microsoft.com/en-us/library/system. drawing.graphics.drawicon.aspx [ ^ ].

但是,我看不到在客户区域中绘制图标有用的情况.图标具有特定的功能(具有不同大小和颜色空间的多个图像),使它们成为应用程序图标和窗口图标时必须具备这些功能,才能由Windows UI(例如Shell)正确处理.在其他所有情况下,您确实需要使用Bitmap或其他ImageSystem.Drawing.Graphics.DrawImage:
http://msdn.microsoft.com/en-us/library/system.drawing. image.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system. drawing.graphics.drawimage.aspx [ ^ ].

由于这种情况下使用图像非常简单,因此可以使用专门为在简单情况下显示图像而设计的即用型控件.这是PictureBox:
http://msdn.microsoft.com/en-us/library/system. windows.forms.picturebox.aspx [ ^ ].

请记住,这个简单的类经常被滥用.仅当您需要真正简单的东西(例如您的情况)时,这才是好.我在过去的答案中对此进行了解释:
如何从旧图纸中清除面板 [ ^ ],
在C#中绘制矩形 [在图片框内添加图片 [
Icon is not a control, so you cannot add it to the panel the same way you would add a control. You can only draw an icon in any control''s client area. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.icon.aspx[^].

Perhaps the closest to your requirements solution would be this: create some custom control which renders some icon in it. Insert instanced of such control in your group box. To draw something, including an icon, on a control, you need to handle its event Paint, or, better yet, override its virtual method OnPaint. In either way, do not create an instance of System.Drawing.Graphics (which is a very common mistake), but use the instance passed to you as an event arguments parameter of your event handler of overridden method. Use the method System.Drawing.Graphics.DrawIcon:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawicon.aspx[^].

However, I don''t see cases where drawing icons in the client area would be useful. Icons carry specific functionality (multiple images with different size and color spaces) which are required to makes them application icons and windows icons, to be properly treated by the Windows UI, such as Shell. In all other cases you really need to use Bitmap or other Image and System.Drawing.Graphics.DrawImage:
http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage.aspx[^].

As this case with using image is very simple, you can use the ready-to-use control specially designed for showing images in simple cases. This is PictureBox:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx[^].

Please remember that this simple class is often abused. It is only good if you need something really simple (such as in your case). I explained it in my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

—SA


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

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