[英]TableLayoutPanel NO Border Around First Column?
我有一個擴展TableLayoutPanel定義/構造的類,如下所示:
public class MyTableLayout : TableLayoutPanel
{
public MyTableLayout()
{
this.ColumnCount = 5;
this.RowCount = 1;
this.CellBorderStyle = TableLayoutPanelCellBorderStyle.Outset;
}
}
當我的表被繪制時,它具有所有5列的邊界(正如人們所期望的那樣,給定代碼來設置上面的CellBorderStyle )。
有沒有辦法阻止在第一列周圍繪制邊框?
我知道你可以添加一個CellPaint回調:
this.CellPaint += tableLayoutPanel_CellPaint;
在此回調中,您可以執行諸如更改特定列的邊框顏色之類的操作:
private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
if (e.Column == 0 && e.Row == 0)
{
e.Graphics.DrawRectangle(new Pen(Color.Red), e.CellBounds);
}
}
但是你怎么畫“No”矩形呢?
我嘗試將顏色設置為Color.Empty,但這不起作用:
e.Graphics.DrawRectangle(new Pen(Color.Empty), e.CellBounds);
反過來嘗試一下。 只繪制要設置邊框的單元格周圍的邊框:
private void tableLayoutPanel_CellPaint(object sender,
TableLayoutCellPaintEventArgs e) {
if (e.Column > 0 && e.Row == 0) {
e.Graphics.DrawRectangle(new Pen(Color.Red), e.CellBounds);
}
}
顯然,將邊框設置為無,以便繪畫可以接管作業:
this.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;
單元格邊框的繪制由OnPaintBackground覆蓋中的TableLayoutPanel執行。
要修改邊框的繪制方式,您需要設置無邊框(因此基類不繪制任何內容),然后在您自己的OnPaintBackground覆蓋中繪制所有其他邊框。
TableLayoutPanel使用內部函數ControlPaint.PaintTableCellBorder來執行邊框繪制。 由於您無法使用它,您應該查看源代碼(使用Reflector或ILSpy)以了解它們是如何做到的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.