gpt4 book ai didi

c# - Windows 窗体中的圆形单选按钮列表

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:53 24 4
gpt4 key购买 nike

我使用 jquery 在 Web 应用程序中设计了圆形按钮列表 plugin和 HTML。在此设计中,用户一次只能选择一天,就像单选按钮列表一样。设计如下图:

enter image description here

如何在 Windows 窗体中实现相同的设计和功能?请帮助我,从我要开始实现这一目标的地方。

最佳答案

在 Windows 窗体中有多个选项可以执行此操作。作为一种选择,您可以从自定义 RadioButtonPanel 控件开始。您可以创建一个从 Panel 派生的新类和一个从 RadioButton 派生的新类,然后覆盖 OnPaint这些类的方法并绘制所需的演示文稿。

这是我在这篇文章中分享的示例实现的结果:

enter image description here

自定义面板

public class MyPanel : Panel
{
public MyPanel()
{
this.Padding = new Padding(2);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var path = new GraphicsPath())
{
var d = this.Padding.All;
var r = this.Height - 2 * d;
path.AddArc(d, d, r, r, 90, 180);
path.AddArc(this.Width - r - d, d, r, r, -90, 180);
path.CloseFigure();
using (var pen = new Pen(Color.Silver, d))
e.Graphics.DrawPath(pen, path);
}
}
}

自定义单选按钮

public class MyRadioButton : RadioButton
{
public MyRadioButton()
{
this.Appearance = System.Windows.Forms.Appearance.Button;
this.BackColor = Color.Transparent;
this.TextAlign = ContentAlignment.MiddleCenter;
this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.FlatAppearance.BorderColor = Color.RoyalBlue;
this.FlatAppearance.BorderSize = 2;
}
protected override void OnPaint(PaintEventArgs e)
{
this.OnPaintBackground(e);
using (var path = new GraphicsPath())
{
var c = e.Graphics.ClipBounds;
var r = this.ClientRectangle;
r.Inflate(-FlatAppearance.BorderSize, -FlatAppearance.BorderSize);
path.AddEllipse(r);
e.Graphics.SetClip(path);
base.OnPaint(e);
e.Graphics.SetClip(c);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
if (this.Checked)
{
using (var p = new Pen(FlatAppearance.BorderColor,
FlatAppearance.BorderSize))
{
e.Graphics.DrawEllipse(p, r);
}
}
}
}
}

要求的使用

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

关于c# - Windows 窗体中的圆形单选按钮列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com