【问题标题】:Circular RadioButton List in Windows FormsWindows 窗体中的圆形单选按钮列表
【发布时间】:2016-07-14 05:38:39
【问题描述】:

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

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

【问题讨论】:

  • 是 WPF 还是 WinForms?
  • 嗨@RezaAghaei 我想要这个在winform中。

标签: c# .net winforms user-controls gdi+


【解决方案1】:

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

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

自定义面板

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;

【讨论】:

  • @Phani 谢谢,这并不完美,但这是一个很好的起点:)
  • 嗨@Reza Aghaei,非常感谢你。它的工作真棒。
  • 嗨@Reza Aghaei,当鼠标悬停在圆圈上时,我们得到了yash颜色。我希望 yashcolor 在按钮单击期间圈出。你能建议我吗。
  • 不客气 :) 如果我正确理解了您的问题,如果您不需要在鼠标悬停时突出显示颜色,则可以将 FlatAppearace.MouseOverBackColor 设置为 Color.Transparent。这样最好将Cursor 设置为Cursors.Hand 以便让用户知道鼠标何时悬停在单选按钮上。
  • 嗨@RezaAghaei,再次感谢你,如果我有任何疑问,请建议我。
【解决方案2】:

【讨论】:

  • 嗨@Phani,请提供正确的链接。
【解决方案3】:

您可以从列表视图或列表框开始 将项目模板更改为按钮,并根据需要为按钮添加样式。 将列表视图的选择模式设为Single。

【讨论】:

  • Windows 窗体 ListView 或 ListBox 没有这样的 ItemTemplate 属性。
  • 嗨@Reza Aghaei,我想要这个在winform中。
  • 我真的不认为 winforms listview 可以做到这一点。
  • 嗨 @Joseph 在 wpf 中有可能
  • @Gopal 显然你可以在 wpf 中做到这一点。你可以灵活地编辑列表视图的项目模板和控制模板,这样你就可以实现这一点。试试我的答案。
猜你喜欢
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 2020-09-04
  • 2011-01-17
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
相关资源
最近更新 更多