WinForm自定義控制項開發(1)

2022-01-16 02:37:20 字數 3710 閱讀 9577

1)具有漸變色的label控制項mylabel:具有漸變色的標籤

2)實現座標系控制項 mycoordinate

1)mylabel

using

system;

using

system.collections.generic;

using

system.linq;

using

system.text;

using

system.windows.forms;

using

system.drawing;

using

system.componentmodel;

using

system.drawing.drawing2d;

namespace

windowsformscontrollibrary

set }

public

override

string

text

set }

protected

override

void

onpaint(painteventargs

e)

private

void

initializecomponent()

} }

2)mycoordinate

using

system;

using

system.collections.generic;

using

system.linq;

using

system.text;

using

system.windows.forms;

using

system.drawing;

using

system.componentmodel;

using

system.drawing.drawing2d;

namespace

windowsformscontrollibrary

set }

//////

y座標標籤間距

///private

intcoordinatelablepady = 30;

public

intcoordinatelablepady

set }

//////

y座標軸起點座標

///public

point

ycoordinatestartpoint

} //////

y座標軸終點座標

///public

point

ycoordinateendpoint

} //////

x座標軸起點座標

///public

point

xcoordinatestartpoint

} //////

x座標軸終點座標

///public

point

xcoordinateendpoint

} //////

當前滑鼠的位置座標

///private

point

currentmouselocation = new

point

();

private

void

initializecomponent()

//////

過載繪製事件

//////

protected

override

void

onpaint(painteventargs

e)

//////過載滑鼠移動事件

//////

protected

override

void

onmousemove(mouseeventargs

e)

//////繪製座標標記

///private

void

drawcoordinatelabel(graphics

gp,point

startypoint,point

endypoint,point

startxpoint,point

endxpoint)

currentvalue = -1;

//繪製x軸

for(int

current = xcoordinatestartpoint.x ; current < xcoordinateendpoint.x ; current += coordinatelablepadx)

//繪製交叉線

list

> list = getcurrentarrowlinespoints(currentmouselocation);

gp.drawline(pens

.blue , list[0], list[1]);

gp.drawline(pens

.blue, list[2], list[3]);

//繪製交叉點的座標

drawmousepointlabel(gp, currentmouselocation);

} //////

繪製當前滑鼠所指的座標位置

//////

///private

void

drawmousepointlabel(graphics

ph,point

current)

y:", (current.x -xpad)/ coordinatelablepadx,

((ycoordinatestartpoint.y-ycoordinateendpoint.y)/coordinatelablepady)-(current.y-ypad)/coordinatelablepady

);ph.drawstring(text, font, brushes

.black , new

pointf

((float

)current.x+margin , (float

)current.y)); }

//////當前滑鼠經過位置點的交叉線座標

//////

///private

list

> getcurrentarrowlinespoints(point

p)

//////修正滑鼠位置

//////

///private

point

limitmouselocation(point

p)

else

if(p.x < xcoordinatestartpoint.x)

//y越界後修正

WinForm 建立自定義控制項

雖然vs為我們提供了很多控制項可以使用,但有時候這些控制項仍然不能滿足我們的要求,比如我們要對部分控制項進行一些個性化的定製,例如美化控制項,這時候就需要自己繪製控制項,或是在原有控制項的基礎上進行修改 自定義控制項分為三種 1 組合控制項 compositecontrols 在原有控制項的基礎上根...

WinForm自定義Loading控制項

在vs2008下封裝winform自定義loading控制項。autocircle.cs封裝了旋轉的圓形,直接在裡面新增label的話,計算圓形扇面的方法有點小複雜,所以在loadingpanel.cs將autocircle和label封裝在一起。該控制項中提供了當loadingpanel狀態變化時...

winform自定義分頁控制項

1 控制項 public partial class pagercontrol usercontrol endregion region 分頁欄位和屬性 private int pageindex 1 當前頁面 public virtual intpageindex set private int ...