2013-12-16 96 views
1

我想創建一個無邊界的CheckBox。它應該在選中時仍顯示覆選標記。如何刪除複選框的邊框?

+1

是掛羊頭賣狗肉出了問題?我知道它需要額外的工作,但您可以創建自己的控件或使用沒有邊框和白色背景的標籤,只需在「OnMouseUp」事件上添加/刪除檢查。 –

+0

@AndreiV很確定這會起作用,但正如你所說,它意味着更多的工作(實際上是解決方法)。希望得到一個暗含CheckBox控件屬性的答案。謝謝! –

+0

當沒有別的作品,作弊:)。斯波爾。 –

回答

0

繼AndreiV建議使自己的customCheckbox,我創建了從標籤繼承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)); 
      } 
     } 
    } 
} 
0

的驗證答案上this question狀態:

因爲複選框被Windows畫和它幾乎全有或全無你不能只刪除邊框。

這是因爲System.Windows.CheckBox是本地控件。

解決此問題的方法是繪製您自己的CustomCheckBox,但沒有可視邊框。

希望這會有所幫助。

+0

如果通過CustomCheckBox,您的意思是從CheckBox繼承的Control,那麼這將無法100%工作。即使覆蓋OnPaint事件,當按住鼠標點擊複選框時,邊框將變爲可見。但是,另一個自定義控件可能只是訣竅。謝謝! –

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

     } 
    } 

} 

這是生成一個定製的圓形按鈕類,可以很好的入門爲你做同樣

+1

這是一個首發,但我認爲這不是一個好的首發。 OP不能達到他想要的效果,我認爲在這個問題上與'Region'無關,他需要一些自定義繪畫。 –

+1

@KingKing,這個例子提供了OP需要解決他們問題的所有知識。通過改變2行我得到這個做什麼OP什麼。 –

+1

@AshBurlaczenko你可能不瞭解OP的實際問題,他可能是指圍繞checkMark的**方框**的邊界,而不是checkBox本身的邊界。你能發佈一些你獲得成果的屏幕截圖嗎? –