C#WinForms中的自定义按钮 [英] Custom Buttons in C# WinForms

查看:172
本文介绍了C#WinForms中的自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些研究,但似乎找不到我要找的东西。



我想要做的是在一个自定义按钮窗体窗体。这基本上只是将默认的灰色背景改为自定义图像的问题。自定义图片也可用于悬停并点击按钮。



这不仅仅是改变背景图片的问题,因为我想使用的图片有具有透明背景的圆形边缘,我希望自定义图像悬停/点击。我希望有关按钮的所有其他操作方式与普通按钮相同。



这可能吗?

FlatStyle 设置为 Flat 并将所有边框设置为0。按钮焦点出现问题(显示一个小边框)。为了解决这个问题,我遵循了这个教程:

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屋!

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