Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何删除复选框';中国的边界?_C#_.net_Winforms_Checkbox - Fatal编程技术网

C# 如何删除复选框';中国的边界?

C# 如何删除复选框';中国的边界?,c#,.net,winforms,checkbox,C#,.net,Winforms,Checkbox,我想创建一个无边框的复选框。选中时,它仍应显示复选标记。已验证的回答状态为: 您不能只删除边框,因为复选框是由windows绘制的,它几乎是全部或全部 这是因为System.Windows.CheckBox是本机控件 解决方法是绘制您自己的CustomCheckBox,没有可见的边框 希望这有帮助 using System; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Windows

我想创建一个无边框的
复选框。选中时,它仍应显示复选标记。

已验证的回答状态为:

您不能只删除边框,因为复选框是由windows绘制的,它几乎是全部或全部

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

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

希望这有帮助

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。同样地,按照AndreiV的建议,我创建了一个从Label继承的CustomControl。接下来,我重写OnPaint和OnClick事件,使其外观和行为类似于复选框。为了显示复选框“选中的图像”,我用了一点油漆将其裁剪到我需要的位置

以下是完整代码:

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));
            }
        }
    }
}

欺骗是不可能的吗?我知道这需要额外的工作,但您可以创建自己的控件,或者使用没有边框和白色背景的标签,只需添加/删除对
OnMouseUp
事件的检查。@AndreiV非常确定这会起作用,尽管正如您所说,这意味着需要更多的工作(实际上需要解决)。希望得到一个包含复选框控件属性或其他内容的答案。谢谢当其他方法都不起作用时,作弊:)。我按照你说的做了:创建一个没有边框的自定义标签控件,它工作得非常好。谢谢这是一个首发,但我认为这不是一个好的首发。OP不能用这个实现他想要的,而且我认为这与
区域
无关。在这个问题上,他需要一些自定义的绘画。@KingKing,这个例子提供了OP解决问题所需的所有知识。通过改变两行,我得到了这个,这正是OP所做的。@Ashburaczenko你可能不理解OP的实际问题,他可能指的是复选标记周围方形框的边框,而不是复选框本身的边框。你能发布一些你所取得成就的屏幕截图吗?@KingKing,你可能是对的,这是使用上面的例子,@Ashburaczenko上面的例子只使用了
区域
属性,你如何设置它的
区域
,以便在去掉方形框的边框时仍然显示文本?除非我们使用一些复杂的区域,其中必须同时包含方形框和文本。如果CustomCheckBox指的是从CheckBox继承的控件,那么这将无法100%工作。即使覆盖OnPaint事件,当按住鼠标并单击复选框时,边框也将可见。但是,另一个自定义控件可能正好起作用。谢谢