如何删除CheckBox的边框?

时间:2013-12-16 11:07:17

标签: c# .net winforms checkbox

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

this question州的经过验证的答案:

  

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

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

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

希望这有帮助。

答案 2 :(得分:0)

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