重寫Label控制項Demo

2021-10-01 18:33:47 字數 3297 閱讀 6882

重寫帶邊框的圓角矩形label控制項,效果如下圖

實現如下:

public enum direction

public static class graphicspathextension

return path;

}///

/// 建立乙個箭頭指向特定方向的等邊三角形,且與指定矩形的中心重合

///

public static graphicspath create********path(this graphics g, rectanglef rect, float f, direction direction)

else if (direction == direction.right)

else if (direction == direction.top)

else

var path = new graphicspath();

path.addlines(new );

return path;

}///

/// 建立圓角矩形路徑

///

public static graphicspath createroundedrectanglepath(this graphics g, rectangle rect, int cornerradius)

roundedrect.addline(rect.x + cornerradius, rect.y, rect.right - cornerradius * 2, rect.y);

if (cornerradius > 0)

roundedrect.addline(rect.right, min(rect.y + cornerradius * 2, rect.y + rect.height / 2), rect.right, max(rect.y + rect.height - cornerradius * 2, rect.y + rect.height / 2));

if (cornerradius > 0)

roundedrect.addline(rect.right - cornerradius * 2, rect.bottom, rect.x + cornerradius * 2, rect.bottom);

if (cornerradius > 0)

roundedrect.addline(rect.x, max(rect.bottom - cornerradius * 2, rect.y + rect.height / 2), rect.x, min(rect.y + cornerradius * 2, rect.y + rect.height / 2));

roundedrect.closefigure();

return roundedrect;

}///

/// 建立圓角矩形路徑(r1:左上,r2:右上,r3:右下,r4:左下)

///

public static graphicspath createroundedrectanglepath(this graphics g, rectangle rect, int r1, int r2, int r3, int r4)

roundedrect.addline(rect.x + r1, rect.y, rect.right - r2 * 2, rect.y);

if (r2 > 0)

roundedrect.addline(rect.right, min(rect.y + r2 * 2, rect.y + rect.height / 2), rect.right, max(rect.y + rect.height - r3 * 2, rect.y + rect.height / 2));

if (r3 > 0)

roundedrect.addline(rect.right - r3 * 2, rect.bottom, rect.x + r3 * 2, rect.bottom);

if (r4 > 0)

roundedrect.addline(rect.x, max(rect.bottom - r4 * 2, rect.y + rect.height / 2), rect.x, min(rect.y + r1 * 2, rect.y + rect.height / 2));

roundedrect.closefigure();

return roundedrect;}}

///

/// 帶邊框的label

///

public class uclabel : label

set }

private int borderwidth = 1;

///

///     邊框顏色

///

[category("border")]

public color bordercolor set }

private color bordercolor = color.fromargb(95, 132, 205);

///

///     邊框樣式

///

[category("border")]

public dashstyle borderdashstyle set }

private dashstyle borderdashstyle = dashstyle.solid;

///

///     是否顯示邊框

///

[category("border")]

public bool showborder

setinvalidate();}}

private bool showborder = false;

///

///     圓角

///

[category("border")]

public int radius

set

}private int radius = 0;

#endregion

#region 建構函式

public uclabel()

#endregion

#region 重繪

///

/// 引發 事件。

///

/// 包含事件資料的 。

protected override void onpaint(painteventargs e)

else}}

}#endregion

}

拒絕單色調,重寫Label控制項

在winform中,微軟提供的label中文字只能是一種顏色 要麼全是黑色,要麼全是紅色或者其它顏色。當我們為了強調某些文字,讓它用紅色顯示時只能新建乙個lable物件,設定它fontcolor為紅色。但是如果在一行文字內嵌三個紅色,那豈不是要拼接六個label控制項?坑爹啊,這麼麻煩,我們還不如自...

Label控制項

開發工具與關鍵技術 visual studio 2015 label 控制項通常在使用者介面 ui 中提供資訊。一直以來,label 只包含文字,但由於 windows presentation foundation 附帶的 label 是乙個 contentcontrol,所以它可以包含文字或 u...

Label控制項

文字控制項包含標籤控制項 label 按鈕控制項 button 文字框控制項 textbox 和有格式文字控制項 richtextbox label控制項可以說是最簡單的控制項,是 system.windows.forms.label 類提供的控制項。label控制項作用 用來提供其他控制項的描述文...