【问题标题】:How can I remove a CheckBox's borders?如何删除 CheckBox 的边框?
【发布时间】:2013-12-18 23:03:59
【问题描述】:

我想创建一个没有边界的CheckBox。选中后仍应显示复选标记。

【问题讨论】:

  • 诡计不可能吗?我知道这需要额外的工作,但您可以创建自己的控件或使用没有边框和白色背景的标签,只需添加/删除对 OnMouseUp 事件的检查。
  • @AndreiV 很确定这会起作用,尽管正如您所说,这意味着更多的工作(实际上是变通方法)。希望得到一个暗示 CheckBox 控件属性或其他东西的答案。谢谢!
  • 当没有其他办法时,作弊:)。体育。
  • 我按照你说的做了:创建一个没有边框的自定义Label控件,效果很好。谢谢!

标签: c# .net winforms checkbox


【解决方案1】:
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class RoundButton : Button
    {

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            GraphicsPath grPath = new GraphicsPath();
            grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
            this.Region = new System.Drawing.Region(grPath);
            base.OnPaint(e);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);

        }
    }

}

这是一个生成自定义圆形按钮的类,可以很好地让您通过类似的方式制作自己的 customCheckbox

【讨论】:

  • 这是一个首发,但我认为它不是一个好的首发。 OP不能用这个实现他想要的,而且我认为这个问题与Region无关,他需要一些自定义绘画。
  • @KingKing,该示例提供了 OP 解决问题所需的所有知识。通过更改 2 行,我得到了这个 OP 的内容。
  • @AshBurlaczenko 你可能不明白OP的实际问题,他可能指的是checkMark周围方形框的边框,而不是checkBox本身的边框。您能否发布一些您取得的成果的屏幕截图?
  • @KingKing,你可能是对的,这是使用上面的例子,i40.tinypic.com/slqs85.png
  • @AshBurlaczenko 上面的例子只使用了Region 属性,你怎么能设置它的Region 以便在方形框的边框被切掉时仍然显示文本?除非我们使用一些复杂的区域,它必须同时包含方框和文本。
【解决方案2】:

this question 上的验证答案指出:

你不能只删除边框,因为复选框是由窗口绘制的,它几乎是全有或全无。

这是因为 System.Windows.CheckBox 是本机控件。

解决方法是绘制您自己的 CustomCheckBox,没有可见的边框。

希望这会有所帮助。

【讨论】:

  • 如果 CustomCheckBox 是指从 CheckBox 继承的控件,那么这不会 100% 起作用。即使重写了 OnPaint 事件,当按住鼠标单击复选框时,边框也会变得可见。但是,另一个自定义控件可能只是解决问题。谢谢!
【解决方案3】:

按照 AndreiV 的建议,我创建了一个继承自 Label 的 CustomControl。接下来,我将覆盖 OnPaint 和 OnClick 事件,使其看起来和行为类似于 CheckBox。为了显示复选框“选中的图像”,我使用了一些颜料将其裁剪成我需要的样子。

以下是完整代码:

public partial class FlatCheckBox : Label
{
    public bool Checked { get; set; }

    public FlatCheckBox()
    {
        InitializeComponent();
        Checked = false;
    }

    protected override void OnClick(EventArgs e)
    {
        Checked = !Checked;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        if (!DesignMode)
        {
            pevent.Graphics.Clear(Color.White);

            var bigRectangle = new Rectangle(pevent.ClipRectangle.X, pevent.ClipRectangle.Y,
                                             pevent.ClipRectangle.Width, pevent.ClipRectangle.Height);
            var smallRectangle = new Rectangle(pevent.ClipRectangle.X + 1, pevent.ClipRectangle.Y + 1,
                                               pevent.ClipRectangle.Width - 2, pevent.ClipRectangle.Height - 2);

            var b = new SolidBrush(UllinkColors.NEWBLUE);
            var b2 = new SolidBrush(Color.White);

            pevent.Graphics.FillRectangle(b, bigRectangle);
            pevent.Graphics.FillRectangle(b2, smallRectangle);

            if (Checked)
            {
                pevent.Graphics.DrawImage(Resources.flatCheckedBox, new Point(3, 3));
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 2019-10-16
    • 1970-01-01
    • 2018-11-01
    • 2015-03-08
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    相关资源
    最近更新 更多