我想創建一個無邊界的CheckBox
。它應該在選中時仍顯示覆選標記。如何刪除複選框的邊框?
回答
繼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));
}
}
}
}
的驗證答案上this question狀態:
因爲複選框被Windows畫和它幾乎全有或全無你不能只刪除邊框。
這是因爲System.Windows.CheckBox是本地控件。
解決此問題的方法是繪製您自己的CustomCheckBox,但沒有可視邊框。
希望這會有所幫助。
如果通過CustomCheckBox,您的意思是從CheckBox繼承的Control,那麼這將無法100%工作。即使覆蓋OnPaint事件,當按住鼠標點擊複選框時,邊框將變爲可見。但是,另一個自定義控件可能只是訣竅。謝謝! –
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);
}
}
}
這是生成一個定製的圓形按鈕類,可以很好的入門爲你做同樣
這是一個首發,但我認爲這不是一個好的首發。 OP不能達到他想要的效果,我認爲在這個問題上與'Region'無關,他需要一些自定義繪畫。 –
@KingKing,這個例子提供了OP需要解決他們問題的所有知識。通過改變2行我得到這個做什麼OP什麼。 –
@AshBurlaczenko你可能不瞭解OP的實際問題,他可能是指圍繞checkMark的**方框**的邊界,而不是checkBox本身的邊界。你能發佈一些你獲得成果的屏幕截圖嗎? –
- 1. 如何刪除複選框邊框?
- 2. 如何刪除C#Visual Studio中複選框的焦點邊框?
- 3. 如何樣式複選框刪除邊框
- 4. 刪除複選框中的複選框
- 5. 複選框刪除
- 6. 如何刪除邊框?
- 7. 如何刪除此邊框?
- 8. 如何刪除複選框旁邊的多餘空間?
- 9. 如何刪除複選框中的行?
- 10. 如何刪除Android的複選框
- 11. Firefox - 從未打開的複選框中刪除邊框
- 12. 風格在Firefox中的複選框 - 刪除檢查和邊框
- 13. 在Firefox中刪除複選框的邊框
- 14. 如何刪除html列表框邊框?
- 15. 如何刪除Firefox中選擇框的虛線邊框
- 16. 在css中刪除複選框的「框」
- 17. 從定製的複選框刪除框
- 18. 如何刪除複選框文本
- 19. 刪除複選框指出
- 20. PHP複選框多刪除
- 21. multiple複選框sqlalchemy刪除
- 22. 刪除複選框中的複選框全部在複選框菜單Primefaces
- 23. 刪除ActionBar邊框
- 24. HTML邊框刪除
- 25. 刪除textareafield邊框
- 26. 刪除EmptyDataTemplate邊框
- 27. jquery radiogroup刪除選定的邊框aparat
- 28. 刪除選定標籤上的邊框
- 29. 刪除選定標籤下的邊框
- 30. 選擇框:移除邊框
是掛羊頭賣狗肉出了問題?我知道它需要額外的工作,但您可以創建自己的控件或使用沒有邊框和白色背景的標籤,只需在「OnMouseUp」事件上添加/刪除檢查。 –
@AndreiV很確定這會起作用,但正如你所說,它意味着更多的工作(實際上是解決方法)。希望得到一個暗含CheckBox控件屬性的答案。謝謝! –
當沒有別的作品,作弊:)。斯波爾。 –