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

來源:互聯網
上載者:User
自己動手用c#寫控制項(下)
作者: willsound   www.ASPCool.com 時間:2002-7-11 17:24:15  閱讀次數:6736

     因為我們這個控制項是實現背景漸層及文字填充,所以override Paint事件以完成自畫。
  
  為了完成override,現在以下的準備工作(寫幾個在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函數,使之可以用圖案填充文本
  
   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);
  
   }
  
  好了,現在開始重寫Paint事件了.
  
  protected override void OnPaint(PaintEventArgs pe)
  
   {
  
  
  
   Graphics g=pe.Graphics;
  
   base.OnPaint(pe); //調用父控制項的方法
  
   if(isUseFloat==true) //假如使用角度控制漸層的角度
  
   DrawButtonWithAngle(g);
  
   if(isUseFloat==false)
  
   DrawButtonWithMode(g,mode);
  
   if(isUseStyle==true)//假如使用圖案填充文字
  
   DrawButtonText(g,hatchStyle);
  
   else
  
   DrawButtonText(g);
  
   }
  
  好了,現在大功告成了,進行儲存,產生。
  
  建立測試專案
  
  1. 在[檔案] 功能表上,指向“添加項目”,然後單擊“建立項目”以開啟“添加新項目”對話方塊。
  
  2. 選擇“Visual C# 項目”節點,然後單擊“Windows 應用程式”。
  
  3. 在“名稱”框中鍵入 Test。
  
  4. 在方案總管中,右擊測試專案的“引用”節點,然後從捷徑功能表中選擇“添加引用”以顯示“添加引用”對話方塊。
  
  5. 單擊標記為“項目”的選項卡。
  
  6. 雙擊 LinearGradientButtonLib 項目,並注意該項目此時出現在“選定的組件”窗格中。
  
  添加引用後,應將新控制項添加到工具箱。如果您的控制項已經出現在工具箱中,則應該跳過下一節。
  
  將控制項添加到工具箱
  
  1. 右擊工具箱,然後從捷徑功能表中選擇“自訂工具箱”。
  
  “自訂工具箱”對話方塊開啟。
  
  2. 選擇“.NET 架構組件”選項卡並單擊“瀏覽”。瀏覽到 LinearGradientButtonLib/bin/debug 檔案夾並選擇 LinearGradientButtonLib.dll。
  
  LinearGradientButton 出現在“自訂工具箱”對話方塊的組件列表中。
  
  3. 在“自訂工具箱”對話方塊中,單擊 LinearGradientButton 旁的框並關閉視窗。
  
  LinearGradientButton 被添加到選定的工具箱的選項卡上。
  
  將控制項添加到表單
  
  1. 在方案總管中,右擊“Form1.cs”,然後從捷徑功能表中選擇“視圖設計器”。
  
  2. 在工具箱中,向下滾動直到到達標記為 LinearGradientButton 的表徵圖。雙擊該表徵圖。
  
  表單上顯示一個“LinearGradientButton”。
  
  3. 右擊“LinearGradientButton”並從捷徑功能表中選擇“屬性”。
  
  4. 在“屬性”視窗中檢查該控制項的屬性。注意,它們與標準按鈕公開的屬性相同,不同的是多了我們自己加入的一些屬性
  
  5. 設定本控制項的前景色彩及背景色,然後可以選擇是否填充文字,是使用角度還是使用系統設定值進行漸層角度的變化。
  
  6. 從“調試”菜單中選擇“啟動”。 出現 Form1。
  
  誰如果需要源碼的話,請給我發信.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.