如何自定义按钮控制像这样的? [英] How to customize Button Control like this one?

查看:153
本文介绍了如何自定义按钮控制像这样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个自定义按钮控制(图像按钮即可),赞一个。

I want to make a custom button control (image button is ok) like this one.

我是一个新用户,所以我不能在这里张贴的图像。所以我上传的图片这里

I'm a new user, so I can't post image here. So I uploaded the picture here

我有点绝望了,现在尝试一些教程后

I'm kind of desperate right now after trying some tutorials

任何建议是高度AP preciated。

Any suggestion is highly appreciated.

感谢

推荐答案

您可以创建一个从按钮将继承保留所有的造型在一个地方一类。要做到悬停和pressed状态可以覆盖鼠标进入/离开的按钮,改文风事件。

You could create a class that inherits from Button to keep all your styling in one place. To do the hover and pressed states you can override the mouse enter / leave events of the button and change style.

下面是我们的项目之一为例(我改变了颜色,但你的想法)。在这里我们改变一些颜色,你可以切换图像。

Here is an example from one of our projects (I changed the colours but your get the idea). Where we change some colours you could switch the images.

public class MossieButton : Button
{
    private static Font _normalFont = new Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

    private static Color _back = System.Drawing.Color.Grey;
    private static Color _border = System.Drawing.Color.Black;
    private static Color _activeBorder = System.Drawing.Color.Red;
    private static Color _fore = System.Drawing.Color.White;

    private static Padding _margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
    private static Padding _padding = new System.Windows.Forms.Padding(3, 3, 3, 3);

    private static Size _minSize = new System.Drawing.Size(100, 30);

    private bool _active;

    public MossieButton()
        : base()
    {
        base.Font = _normalFont;
        base.BackColor = _border;
        base.ForeColor = _fore;
        base.FlatAppearance.BorderColor = _back;
        base.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        base.Margin = _margin;
        base.Padding = _padding;
        base.MinimumSize = _minSize;
    }

    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);
        UseVisualStyleBackColor = false;
    }

    protected override void OnMouseEnter(System.EventArgs e)
    {
        base.OnMouseEnter(e);
        if (!_active)
            base.FlatAppearance.BorderColor = _activeBorder;
    }

    protected override void OnMouseLeave(System.EventArgs e)
    {
        base.OnMouseLeave(e);
        if (!_active)
            base.FlatAppearance.BorderColor = _border;
    }

    public void SetStateActive()
    {
        _active = true;
        base.FlatAppearance.BorderColor = _activeBorder;
    }

    public void SetStateNormal()
    {
        _active = false;
        base.FlatAppearance.BorderColor = _border;
    }
}

这篇关于如何自定义按钮控制像这样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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