徽章添加到C#的WinForms控制 [英] Add a badge to a C# winforms control

查看:232
本文介绍了徽章添加到C#的WinForms控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CSS中写,我可以添加一个类徽章,并得到我想要的东西。附近的一个按钮或标签与一些风格给它一个小数字,以表明这种控制已经挂起,需要检讨的信息。

When writing in CSS I can add a class of "badge" and get what I want. A small number near a button or tab with some styling to it, to show that this control has pending info that needs review.

甚至有一个职位的here某人试图做我想要在iOS什么

There is even a post here of somebody trying to do what I want on iOS

我想这样做对的WinForms在任一按钮或选项卡。如果一个简单的解决方案在WPF可用,那么我可能会考虑使用,而不是

I want to do this on WinForms on either a button or a tab. If an easier solution is available in WPF, then I might consider using that instead.

下面是表示什么,我想要实现一个示例图像:

Here is an image showing an example of what I want to achieve:

推荐答案

这是一个静态的装饰器类,非常快,比较脏的方式。

And here is a way with a static Adorner class, very quick and rather dirty..

它可以将标签添加到许多控件,它包括一个点击动作,动态文本并有代码以删除标签

It can add a Label to many controls and it includes a click action, dynamic text and has code to remove the Label.

添加徽章到一个按钮把一行:

Adding a Badge to a Button takes one line:

    public Form1()
    {
        InitializeComponent();
        // adorn one Button with a Badge Label:
        Adorner.AddBadgeTo(button1, "123");
        // if you want to you can add a click action:
        Adorner.SetClickAction(button1, dobidoo);
    }

    // a test action
    void dobidoo(Control ctl)
    {
        Console.WriteLine("You have clicked on :" + ctl.Text);
    }

下面是装饰器类:

static class Adorner
{
    private static List<Control> controls = new List<Control>();

    static public bool AddBadgeTo(Control ctl, string Text)
    {
        if (controls.Contains(ctl)) return false;

        Badge badge = new Badge();
        badge.AutoSize = true;
        badge.Text = Text;
        badge.BackColor = Color.Transparent;
        controls.Add(ctl);
        ctl.Controls.Add(badge);
        SetPosition(badge, ctl);

        return true;
    }

    static public bool RemoveBadgeFrom(Control ctl)
    {
        Badge badge = GetBadge(ctl);
        if (badge != null)
        {
            ctl.Controls.Remove(badge);
            controls.Remove(ctl);
            return true;
        }
        else return false;
    }

    static public void SetBadgeText(Control ctl, string newText)
    {
        Badge badge = GetBadge(ctl);
        if (badge != null) 
        {
                badge.Text = newText;
                SetPosition(badge, ctl);
        }
    }

    static public string GetBadgeText(Control ctl)
    {
        Badge badge = GetBadge(ctl);
        if (badge != null) return badge.Text;
        return "";
    }

    static private void SetPosition(Badge badge, Control ctl)
    {
       badge.Location = new Point(ctl.Width  - badge.Width - 5, 
                                  ctl.Height - badge.Height - 5);
    }

    static public void SetClickAction(Control ctl, Action<Control> action)
    {
        Badge badge = GetBadge(ctl);
        if (badge != null)  badge.ClickEvent = action;
    }

    static Badge GetBadge(Control ctl)
    {
        for (int c = 0; c < ctl.Controls.Count; c++)
            if (ctl.Controls[c] is Badge) return ctl.Controls[c] as Badge;
        return null;
    }


    class Badge : Label
    {
        Color BackColor = Color.SkyBlue;
        Color ForeColor = Color.White;
        Font font = new Font("Sans Serif", 8f);

        public Action<Control> ClickEvent;

        public Badge()   {}

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillEllipse(new SolidBrush(BackColor), this.ClientRectangle);
            e.Graphics.DrawString(Text, font, new SolidBrush(ForeColor), 3, 1);
        }

        protected override void OnClick(EventArgs e)
        {
            ClickEvent(this);
        }

    }
}

请注意,同时可以将其添加到大多数控件,不是所有的工作,以及一个按钮。 A 的TabControl 是相当困难来装饰它的标签真的不是控制只是画领域,所以就像加入了关闭X它,你将不得不用户抽奖所有的TabPages <的徽章/ code> ..

Note that while you can add it to most controls, not all work as well as a Button. A TabControl is rather hard to adorn as its Tabs really are not Controls but just painted areas, so just like adding a 'close X' to it you would have to user draw the badges of all TabPages..

这篇关于徽章添加到C#的WinForms控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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