C#控件重繪學習(一)


由於要做一個工控軟件,傳統的控件顯然已經不能滿足實際的要求了,所以控件的重繪迫在眉睫。由於考研耽誤了很多時間,C#的學習也擱淺了很長一段時間了,所以趁這個機會,我打算把控件的重繪認真的學習透徹。

好了,控件的重繪,讓我們從普通按鈕開始吧!

先刨一下Button的老底:

命名空間: System.Windows.Forms
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)

Button的屬性就太多了:比如BackColor、Font等等。我們要做的就是修改他的這些屬性。

我們先自定義一個類,繼承自System.Windows.Forms.Button

   1:  using System.Drawing;
   2:   
   3:  namespace WindowsFormsApplication1
   4:  {
   5:      class MyButton:System.Windows.Forms.Button
   6:      {
   7:          public MyButton()
   8:          {
   9:              BackColor = Color.Blue;
  10:              ForeColor = Color.White;
  11:              FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  12:              Font = System.Windows.Forms.Control.DefaultFont;
  13:              UseWaitCursor = true;
  14:          }
  15:      }
  16:  }

然后呢:在窗體中隨便拖入一個Button,並且改寫窗體源文件Form1.Designer.cs,將源文件中的System.Windows.Forms.Button();改為MyButton();

   1:  namespace WindowsFormsApplication1
   2:  {
   3:      partial class Form1
   4:      {
   5:          /// <summary>
   6:          /// 必需的設計器變量。
   7:          /// </summary>
   8:          private System.ComponentModel.IContainer components = null;
   9:   
  10:          /// <summary>
  11:          /// 清理所有正在使用的資源。
  12:          /// </summary>
  13:          /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
  14:          protected override void Dispose(bool disposing)
  15:          {
  16:              if (disposing && (components != null))
  17:              {
  18:                  components.Dispose();
  19:              }
  20:              base.Dispose(disposing);
  21:          }
  22:   
  23:          #region Windows 窗體設計器生成的代碼
  24:   
  25:          /// <summary>
  26:          /// 設計器支持所需的方法 - 不要
  27:          /// 使用代碼編輯器修改此方法的內容。
  28:          /// </summary>
  29:          private void InitializeComponent()
  30:          {
  31:              this.button1 = new MyButton();
  32:              this.SuspendLayout();
  33:              // 
  34:              // button1
  35:              // 
  36:              this.button1.Location = new System.Drawing.Point(12, 12);
  37:              this.button1.Name = "button1";
  38:              this.button1.Size = new System.Drawing.Size(75, 23);
  39:              this.button1.TabIndex = 0;
  40:              this.button1.Text = "button1";
  41:              this.button1.UseVisualStyleBackColor = true;
  42:              // 
  43:              // Form1
  44:              // 
  45:              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  46:              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  47:              this.ClientSize = new System.Drawing.Size(284, 261);
  48:              this.Controls.Add(this.button1);
  49:              this.Name = "Form1";
  50:              this.Text = "Form1";
  51:              this.ResumeLayout(false);
  52:   
  53:          }
  54:   
  55:          #endregion
  56:   
  57:          private MyButton button1;
  58:      }
  59:  }
  60:   

 

運行之后效果如下:

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM