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

首頁 > 開發(fā) > 綜合 > 正文

自己動手用c#寫控件(下)

2024-07-21 02:27:08
字體:
供稿:網(wǎng)友

因為我們這個控件是實現(xiàn)背景漸變及文字填充,所以override paint事件以完成自畫。

為了完成override,現(xiàn)在以下的準(zhǔn)備工作(寫幾個在paint事件用的著的事件).

//使用角度的方法漸近重畫button

          private void drawbuttonwithangle(graphics dbg)

         {

              lineargradientbrush brush=new lineargradientbrush(new rectangle(0,0,this.width,this.height),frocolor,backcolor,angle);

              dbg.fillrectangle(brush,0,0,this.width,this.height);

              brush.dispose();

         }

         ////使用模式的方法漸近重畫button

          private void drawbuttonwithmode(graphics dbg,lineargradientmode mode)

         {

              lineargradientbrush brush=new lineargradientbrush(new rectangle(0,0,this.width,this.height),frocolor,backcolor,mode);

              dbg.fillrectangle(brush,0,0,this.width,this.height);

              brush.dispose();

         }

         //重畫button的文本(text),不使用圖案填充

          private void drawbuttontext(graphics dbg)

         {

              stringformat format=new stringformat();

              format.linealignment=stringalignment.center;

              format.alignment=stringalignment.center;

              dbg.drawstring(this.text,this.font,new solidbrush(this.forecolor),new rectangle(0,0,this.width,this.height),format);

         }

          //override drawbuttontext函數(shù),使之可以用圖案填充文本

          private  void drawbuttontext(graphics dbg, hatchstyle hs)

         {

              stringformat format=new stringformat();

              format.linealignment=stringalignment.center;

              format.alignment=stringalignment.center;

              dbg.drawstring(this.text,this.font,new hatchbrush(hs,this.forecolor,color.aquamarine),new rectangle(0,0,this.width,this.height),format);

     }

好了,現(xiàn)在開始重寫paint事件了.

protected override void onpaint(painteventargs pe)

         {

             

              graphics g=pe.graphics;

              base.onpaint(pe); //調(diào)用父控件的方法

              if(isusefloat==true) //假如使用角度控制漸變的角度

                   drawbuttonwithangle(g);

              if(isusefloat==false)

                   drawbuttonwithmode(g,mode);

              if(isusestyle==true)//假如使用圖案填充文字

             drawbuttontext(g,hatchstyle);

              else

                   drawbuttontext(g);

     }

好了,現(xiàn)在大功告成了,進行保存,生成。

創(chuàng)建測試項目

1.        在“文件”菜單上,指向“添加項目”,然后單擊“新建項目”以打開“添加新項目”對話框。

2.        選擇“visual c# 項目”節(jié)點,然后單擊“windows 應(yīng)用程序”。

3.        在“名稱”框中鍵入 test。

4.        在解決方案資源管理器中,右擊測試項目的“引用”節(jié)點,然后從快捷菜單中選擇“添加引用”以顯示“添加引用”對話框。

5.        單擊標(biāo)記為“項目”的選項卡。

6.        雙擊 lineargradientbuttonlib 項目,并注意該項目此時出現(xiàn)在“選定的組件”窗格中。

添加引用后,應(yīng)將新控件添加到工具箱。如果您的控件已經(jīng)出現(xiàn)在工具箱中,則應(yīng)該跳過下一節(jié)。

將控件添加到工具箱

1.        右擊工具箱,然后從快捷菜單中選擇“自定義工具箱”。

“自定義工具箱”對話框打開。

2.        選擇“.net 框架組件”選項卡并單擊“瀏覽”。瀏覽到 lineargradientbuttonlibindebug 文件夾并選擇 lineargradientbuttonlib.dll。

lineargradientbutton 出現(xiàn)在“自定義工具箱”對話框的組件列表中。

3.        在“自定義工具箱”對話框中,單擊 lineargradientbutton 旁的框并關(guān)閉窗口。

lineargradientbutton 被添加到選定的工具箱的選項卡上。

將控件添加到窗體

1.        在解決方案資源管理器中,右擊“form1.cs”,然后從快捷菜單中選擇“視圖設(shè)計器”。

2.        在工具箱中,向下滾動直到到達(dá)標(biāo)記為 lineargradientbutton 的圖標(biāo)。雙擊該圖標(biāo)。

窗體上顯示一個“l(fā)ineargradientbutton”。

3.        右擊“l(fā)ineargradientbutton”并從快捷菜單中選擇“屬性”。

4.        在“屬性”窗口中檢查該控件的屬性。注意,它們與標(biāo)準(zhǔn)按鈕公開的屬性相同,不同的是多了我們自己加入的一些屬性

5.        設(shè)定本控件的前景色及背景色,然后可以選擇是否填充文字,是使用角度還是使用系統(tǒng)設(shè)定值進行漸變角度的變化。

6.        從“調(diào)試”菜單中選擇“啟動”。 出現(xiàn) form1。

誰如果需要源碼的話,請給我發(fā)信.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 商水县| 夏津县| 衡山县| 留坝县| 邢台市| 商洛市| 红桥区| 宜川县| 克什克腾旗| 抚顺市| 承德县| 民权县| 南宫市| 玉环县| 临高县| 宽甸| 保康县| 武强县| 绥芬河市| 永德县| 紫云| 友谊县| 淮北市| 鹤峰县| 广安市| 泸州市| 福贡县| 丹寨县| 宁远县| 菏泽市| 本溪市| 赤壁市| 安泽县| 湘乡市| 伊宁市| 漳州市| 通城县| 天台县| 外汇| 伊金霍洛旗| 和田市|