国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

c#自定義控件開(kāi)發(fā)實(shí)例(2)

2024-07-21 02:19:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
源文件:http://ded.nuaa.edu.cn/download/windows%20extended%20controls.rar

示例代碼:http://ded.nuaa.edu.cn/download/windowsapplication6.rar

下面講一下控件具體如何工作,首先要寫他的屬性以及重寫他的屬性,
private color _bordercolor=new color();
[defaultvalue("black"),description("邊框顏色"),category("appearance")]
public color bordercolor
{
get
{
// insert code here.
return _bordercolor;
}
set
{
_bordercolor=value;
this.invalidate();
}
}
defaultvalue:設(shè)定默認(rèn)值,description:描述,就是屬性下面的說(shuō)明,category:屬性分類.其他的同理
設(shè)置好屬性以后應(yīng)該重寫他的繪制過(guò)程,也就是
protected override void onpaint(painteventargs pe)
{
// calling the base class onpaint
base.onpaint(pe);
redrawcontrol(pe.graphics);
}
private void redrawcontrol(graphics graphics)
{

try
{
//繪制邊框
rectangle rectborder=new rectangle();
pen borderpen=new pen(this._bordercolor,1);
rectborder.x = 7;
rectborder.y = 7;
rectborder.height = this.height-15;
rectborder.width = this.width-15;
graphics.drawrectangle(borderpen, rectborder);
//繪制編輯框
if (_resizeble)
{
drawselector(graphics);
}
}
catch(exception e)
{
throw e;
}
finally
{
graphics.dispose();
}
}
[description("控件選擇區(qū)域"),category("behavior")]
public rectangle selectrectangle
{
get
{
rectangle selectrectangler=new rectangle();
selectrectangler.x = this.location.x+7;
selectrectangler.y = this.location.y+7;
selectrectangler.height = this.height-15;
selectrectangler.width = this.width-15;
return selectrectangler;
}

}

redrawcontrol中定義了rectangle (矩形),讓后填充改矩形的邊框:graphics.drawrectangle(borderpen, rectborder);,這里要說(shuō)明的是邊框外面還有編輯框,所以大小不是控件的大小。drawselector就是繪制8個(gè)選擇框的,基本和繪制邊框差不多,即使定義好坐標(biāo)就可以了。干好了之后不要忘了釋放資源:graphics.dispose();

selectrectangle:控件所選擇的rectangle,最終要得就是它了

ok,這樣一個(gè)基本的東西出來(lái)了,下面我們要寫他的move和resize函數(shù)了,先添加事件:


this.resize += new system.eventhandler(this.shapeex_resize);
this.mouseup += new system.windows.forms.mouseeventhandler(this.shapeex_mouseup);
this.mousemove += new system.windows.forms.mouseeventhandler(this.shapeex_mousemove);
this.mouseleave += new system.eventhandler(this.shapeex_mouseleave);
this.mousedown += new system.windows.forms.mouseeventhandler(this.shapeex_mousedown);
原理:當(dāng)鼠標(biāo)點(diǎn)擊的時(shí)候this.mousedown,記錄鼠標(biāo)的位置,控件的原始位置和大小,判斷位置:_rectleftbottomselector.contains(e.x,e.y):表示點(diǎn)擊的是左下,設(shè)置鼠標(biāo),記錄狀態(tài)this._selectselctedindex:判斷點(diǎn)擊了那個(gè)選擇框,取值0-8:0代表移動(dòng)整個(gè)控件,1是右上移動(dòng),2-8順時(shí)針?biāo)饕x擇框。this.mousemove處理如何改變控件的大小和位置
case 0://只移動(dòng)位置
this.location=new point(cursor.position.x-(_mouselocation.x-_selflocation.x),cursor.position.y-(_mouselocation.y-_selflocation.y));
break;
1,5不僅移動(dòng)位置,還改變大小,2,3,4,6,7,8只改變大小

其他則是清理工作。

好了,那么趕緊編譯。生成就可以了。


中國(guó)最大的web開(kāi)發(fā)資源網(wǎng)站及技術(shù)社區(qū),
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁陵县| 黑河市| 信丰县| 定远县| 五莲县| 天镇县| 德州市| 临高县| 绍兴县| 额敏县| 梁山县| 临沧市| 永州市| 嘉兴市| 四川省| 马关县| 大庆市| 观塘区| 晋宁县| 江陵县| 新建县| 新巴尔虎左旗| 揭东县| 肇东市| 余姚市| 思茅市| 江安县| 磴口县| 中牟县| 安新县| 凉城县| 宽城| 北京市| 库尔勒市| 紫云| 富锦市| 淮北市| 峨边| 阜康市| 宝坻区| 两当县|