如何创建具有与按钮相同属性的用户控件? [英] How to create a usercontrol that has the same properties of the buttons?

查看:102
本文介绍了如何创建具有与按钮相同属性的用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个用户控件,该控件只有一个带有图像的picturbox,并且具有与按钮相同的功能.因此,当我将此用户控件放在窗体上并访问其属性窗口时,该窗口应具有与按钮相同的属性,并且当我单击某些属性(例如文本"属性)时,文本应显示在此图片框的顶部,或者当我访问BackColor属性时,应该更改背景色,所有其他属性都应更改.
有人知道我怎么做吗?在此先感谢

I want to create a usercontrol, which will have only a picturbox with an image, that has the same functionality of the buttons. So when I put this usercontrol on the form and access its properties window, this window should have the same properties of the buttons and when I click on some property, such as the Text property, the text should appear on top of this picturbox, or when I access the BackColor property the background color should be changed, the same for all other properties.
Does anyone know how I do it? Thanks in advance

推荐答案

创建一个从ControlButton派生的自定义控件.出于善意的考虑,甚至不用考虑使用PictireBox -它是为非常不同的目的而设计的,并且不会比在控件的表面上呈现给您任何额外的好处,只会消耗内存,CPU时间和您的开发时间没有任何目的.

而是改写OnPaint;在此方法中,根据控件的状态(按下,鼠标悬停,默认,具有键盘焦点等),使用其事件参数参数,获取System.Drawing.Graphics的实例并用于渲染适当的图像.更改状态处理控件的事件.状态更改后,请不要忘记触发控件的重新渲染.这是使用Control.Invalidate完成的.

另请参阅:如何从中清除面板旧图纸 [ http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/598t492a.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8dtk06x2.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/wtzka3b5.aspx [ ^ ].

至于按钮示例,CodeProject是一个搜索的好地方.

观看:
http://www.codeproject.com/search.aspx? q = Custom + button +%22VB.NET%22& doctypeid = 1 [ http://www.codeproject.com/search.aspx? q = Custom + button +%22C%23%22& doctypeid = 1 [
Create a custom control derived from Control or Button. For goodness sake, don''t even think about using PictireBox — it is designed for very different purposes and won''t give you any additional benefits over rendering right in the surface of the control, will only consume memory, CPU time and your development time without any purpose.

Instead, override OnPaint; in this method, use its event arguments parameter, get an instance of System.Drawing.Graphics and use it to render appropriate image, depending on control''s status (pressed, mouse hover, default, has keyboard focus…). Change status handling control''s event. When the status is changed, don''t forget to trigger re-rendering of the control. This is done using Control.Invalidate.

See also: How do I clear a panel from old drawing[^].

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^],
http://msdn.microsoft.com/en-us/library/598t492a.aspx[^],
http://msdn.microsoft.com/en-us/library/8dtk06x2.aspx[^],
http://msdn.microsoft.com/en-us/library/wtzka3b5.aspx[^].

As to the button samples, CodeProject is a good place to search in.

Watch this:
http://www.codeproject.com/search.aspx?q=Custom+button+%22VB.NET%22&doctypeid=1[^],
http://www.codeproject.com/search.aspx?q=Custom+button+%22C%23%22&doctypeid=1[^].

Now, pay attention: 7 articles in VB.NET, 47 in C#. Yes, to find good samples and any general help in .NET you really need to understand at least some of C#.

—SA


不要创建UserControl.而是,创建一个从Button控件继承的类.启用所有者绘制模式并自己处理图形.
Don''t create a UserControl. Instead, create a class that inherits from the Button control. Enable Owner drawn mode and handle the drawing yourself.


这篇关于如何创建具有与按钮相同属性的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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