C#WinForm - 在按钮或其他控件的右上角绘制红色点(带数字)像android/ios应用程序
问题描述:
绘制一个红色的点像android/ios应用程序,用户注意到一个新的事件。C#WinForm - 在按钮或其他控件的右上角绘制红色点(带数字)像android/ios应用程序
有没有简单的情况下建立在一些控制?
如TabPage标题右上角,Button右上角,Groupbox右上角。
答
遵循相同概念的两个想法来到我的脑海。在Winforms中,您可以通过PaintEvents绘制GDI可能实现的所有内容。正如@Kinect所提到的那样,解决方案中没有内置的处理方式。
这两种思路可以为你工作:
- 的寄托都创建自定义控件,你需要一个红点。所以你可能会创建一个自己的Framework,它扩展了所有的基础Winforms并扩展它(也许存在一些第三方的东西)。
- 您创建了一个处理您绘画的组件。
对于第一种方式,我创造出延伸的按钮一个小例子,追加红点图中:
public class CustomButton : Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent); //Call base for creating default Button
using (Pen pen = new Pen(Color.Red, 3)) //Create small Red Pen
{
//Create the area we want to draw. In this case we draw at the Button top right corner (because Android, iOS do this)
Rectangle rectangle = new Rectangle(pevent.ClipRectangle.X + pevent.ClipRectangle.Width - 10, 0, 10, 10);
pevent.Graphics.DrawEllipse(pen, rectangle); //Draw a ellipse
pevent.Graphics.FillEllipse(new SolidBrush(Color.Red), rectangle); //Fill this ellipse to be red
//Paint a string to the ellipse so that it looks like Android, iOS Notification
//You have to replace the 1 with dynamic count of your notifications
pevent.Graphics.DrawString("1", DefaultFont, new SolidBrush(Color.White), rectangle.X, rectangle.Y);
}
}
}
您需要做某事。像你想要扩展的每个控件一样。
或者你选择了第二种方式,并使用一个组件:因为组件持有型控制字典,你可以链接延伸控制(这是最)每WinForm的对象
public partial class RedPointDrawer : Component
{
/// <summary>
/// Get: Dictionary of bound Controls which become a RedPoint with number of notifications
/// </summary>
public Dictionary<Control, int> Controls { get; }
/// <summary>
///
/// </summary>
public RedPointDrawer()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="container"></param>
public RedPointDrawer(IContainer container)
{
container.Add(this);
InitializeComponent();
Controls = new Dictionary<Control, int>();
}
/// <summary>
/// Draws a RedPoint to all
/// </summary>
public void ActivateRedPoint()
{
foreach (Control control in Controls.Keys)
{
control.Paint += Control_Paint;
}
}
private void Control_Paint(object sender, PaintEventArgs e)
{
int count;
var control = sender as Control;
if (control != null &&
Controls.TryGetValue(control, out count))
{
using (Pen pen = new Pen(Color.Red, 3))
{
Rectangle rectangle = new Rectangle(e.ClipRectangle.X + e.ClipRectangle.Width - 10, 0, 10, 10);
e.Graphics.DrawEllipse(pen, rectangle);
e.Graphics.FillEllipse(new SolidBrush(Color.Red), rectangle);
e.Graphics.DrawString(count.ToString(), new Font(new FontFamily("Arial"), 8), new SolidBrush(Color.White),
rectangle.X,
rectangle.Y);
}
}
}
}
。该组件可以从您的工具箱中选择并添加到您的表单中。然后在您的表单启动例程中调用以下代码:
private void FormStartUp() //Shown event or sth.
{
redPointDrawer1.Controls.Add(button1, notifications.Count);
redPointDrawer1.Controls.Add(button2, notifications2.Count);
redPointDrawer1.ActivateRedPoint();
}
那么这不是完美的,但也许是一个很好的起点。
这不是在.net用户控件中构建的功能,如果这就是您要求的功能。你将不得不自己编码.. – Kinetic
[看这里](http://*.com/questions/29756038/add-a-badge-to-ac-sharp-winforms-control/29757940?s=1|0.4901 #29757940)为一个简单的解决方案,添加一个标签。相反,您可以使用相同的解决方案将控件连接到常见的Paint事件,该事件只需在所有注册的控件上执行FillEllipse。注册所有应该装饰的控件的想法是相同的。 – TaW