C#WinForms中的自定义按钮 [英] Custom Buttons in C# WinForms
问题描述
我做了一些研究,但似乎找不到我要找的东西。
我想要做的是在一个自定义按钮窗体窗体。这基本上只是将默认的灰色背景改为自定义图像的问题。自定义图片也可用于悬停并点击按钮。
这不仅仅是改变背景图片的问题,因为我想使用的图片有具有透明背景的圆形边缘,我希望自定义图像悬停/点击。我希望有关按钮的所有其他操作方式与普通按钮相同。
这可能吗?
http://dotnetstep.blogspot.com/2009/06/remove-focus-rectangle-from-button.html
有了这个功能,我只需将事件添加到按钮中,以便在执行某个操作时更改图像:
private void button1_MouseLeave(object sender,EventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
private void button1_MouseEnter(object sender,EventArgs e)
{
this.button1.Image = Properties.Resources._hover;
}
private void button1_MouseDown(object sender,MouseEventArgs e)
{
this.button1.Image = Properties.Resources._clicked;
}
private void button1_MouseUp(object sender,MouseEventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
希望这可以帮助别人!
I have done a bit of research but cannot seem to find what I am looking for.
What I want to do is make a "custom" button in a windows form. This would basically just be a matter of changing the default "grey" background to a custom image. A custom image would also be used for when hovering over and clicking the button.
This is not just a matter of changing the background image as the image I want to use has rounded edges with a transparent background and I want custom image for hovering / clicked. I want everything else about the button to behave in the same manner as a normal button.
Is this possible?
The solution I found was to set the FlatStyle of the button to Flat and set all the borders to 0. I then had a problem with the focus of the button (it displayed a little border). To solve this I followed this tutorial:
http://dotnetstep.blogspot.com/2009/06/remove-focus-rectangle-from-button.html
With this in place all I had to do was add events to the button so that the image was changed when a certain action was carried out on it:
private void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
private void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.Image = Properties.Resources._hover;
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
this.button1.Image = Properties.Resources._clicked;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
this.button1.Image = Properties.Resources._default;
}
Hope this will help someone else!
这篇关于C#WinForms中的自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!