如何自定义像这样的按钮控件?

发布于 2024-10-24 01:16:03 字数 708 浏览 1 评论 0原文

我想制作一个像这样的自定义按钮控件(图像按钮可以)。

我是新用户,所以不能在这里发布图片。所以我上传了图片这里

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

任何建议都将受到高度赞赏。

谢谢

更新08/10/2019:我很多年前就问过这个问题,当时我没有上传图片的权限,所以我上传到第三方网站的图片是现在已经走了很久了。我收到了很多关于重新上传图像的请求,所以这是我八年前做的那个项目的记忆,我只是找到了一些与我的记忆相匹配的关于窗口窗体的随机图像

这是当按钮处于正常状态时 这是当按钮处于正常状态时

这是当按钮悬停或单击时,带有圆角边框 这是当按钮悬停或单击时,带有圆角边框

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

Any suggestion is highly appreciated.

Thanks

Updated 08/10/2019: I asked this question so many years ago, and at that time I didn't have the permission to upload image, so the image I uploaded to the third party site is long gone now. I got many requests about re-uploading the image, so here is what I remember from that project I did eight years ago, I just find some random images about window form that match my memory

This is when the button is in normal state
This is when the button is in normal state

This is when the button is hovered or clicked, with the rounded border
This is when the button is hovered or clicked, with the rounded border

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-10-31 01:16:03

您可以创建一个继承自 Button 的类,以将所有样式保留在一个位置。要执行悬停和按下状态,您可以覆盖按钮的鼠标输入/离开事件并更改样式。

这是我们的一个项目的示例(我更改了颜色,但您明白了)。在我们更改一些颜色的地方,您可以切换图像。

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;
    }
}

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;
    }
}
小…楫夜泊 2024-10-31 01:16:03

看不到图片,但我想你可以更改按钮的边框并设置背景图像。

button1.FlatStyle = FlatStyle.Flat;
button1.BackgroundImage = Bitmap.FromFile("image.jpg");

Can't see the picture but I guess you can change the border of the button and set a background image.

button1.FlatStyle = FlatStyle.Flat;
button1.BackgroundImage = Bitmap.FromFile("image.jpg");
在巴黎塔顶看东京樱花 2024-10-31 01:16:03

我认为最简单的方法是设置按钮的一些属性,如下所示,

this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Image = "Any Image"
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;

然后编写更新代码

  private void button1_Click(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "OnClick";
    }

    private void button1_MouseEnter(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "Enter";
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "Normal";
    }

我不知道我是否正确,但我认为您也可以通过放置按钮和标签来实现您的目标在面板内并根据您的选择排列它们。将 button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat 初始化为 Label.Text="Normal"。然后在鼠标进入面板时,在按钮周围绘制一个带有边框的矩形,并将标签文本更改为“Hover”。就像单击面板一样,您也可以根据您的需要更改矩形边框并设置 label.Text="OnClick"

I think the simplest way is set some properties of the button like below and

this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Image = "Any Image"
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;

then write the code for

  private void button1_Click(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "OnClick";
    }

    private void button1_MouseEnter(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "Enter";
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "Normal";
    }

Update:

I don't know whether I am going correct or not but I think You can also achive your goal by putting a Button and a label inside a panel and arrange them according to your choice. Make the button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat at initial with Label.Text="Normal". Then on Mouse enter to the Panel draw a rectangle with a border around the button and change the text of the label to "Hover". Like that Clicking on the Panel also you change the rectangle border according to you and make the label.Text="OnClick".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文