C#制作高仿360安全衛士窗體(三)


距上篇C#制作高仿360安全衛士窗體(二)也將近一個多月了,這個月事情還是像往常一樣的多。不多我也樂在其中,畢竟我做的是我喜歡做的東西。今天特地抽空把怎么制作文本框寫一下。同時也希望有愛好這些玩意的同仁和我進行交流... 文本框的開發比起按鈕開發還是有一點不同,因為我這里主要是給文本框做美化,所以不需要完完全全的進行自己開發。只是重寫它的某些事件,然后展現不同的效果。下面是運行后的效果。

這個文本框實現了多行以及鼠標進入移出等事件的效果,那么開發這個素材只有一個也是從之前360皮膚包里面提取出來進行修改的:

一、嵌入資源

將以上素材另存為,在解決方案中Images目錄里面建立一個TextBoxImages文件夾,將圖片素材拷貝進去,並設置圖片屬性中生成操作選擇為“嵌入的資源”。

二、添加控件

資源嵌入之后再在ControlEx目錄中建立一個TextBoxEx文件夾,在該文件夾下創建一個名為TextBoxEx的用戶控件。該用戶控件是用來實現皮膚變化,而真正的TextBox需要再從工具欄中拖一個到用戶控件中。調整用戶控件的寬高為為160*22,TextBox的寬高為154*16,TextBox的Margin屬性為3,3,3,3,TextBox的BorderStyle屬性值為None,將屬性都調整完畢之后就可以開始進行代碼的處理了。

三、編碼
該控件的主要處理方法都比較簡單,主要思路是重寫TextBox的狀態,然后再在用戶控件上根據狀態繪制不同的樣式。
1、變量聲明

 1 #region 聲明
 2 private Bitmap _TextBoxBackImg = ImageObject.GetResBitmap("FANGSI.UI.Images.TextBoxImages.Textbox.png");
 3 private State state = State.Normal;
 4 private bool _Isico = false;
 5 private Bitmap _Ico;
 6 private Padding _IcoPadding = new Padding(3, 3, 0, 0);
 7 //枚鼠標狀態
 8 private enum State
 9 {
10     Normal = 1,
11     MouseOver = 2,
12     MouseDown = 3,
13     Disable = 4,
14     Default = 5
15 }
16 #endregion

2、構造參數處理,初始化控件的屬性

 1 #region 構造
 2 public TextBoxEx()
 3 {
 4     InitializeComponent();
 5     this.SetStyle(ControlStyles.UserPaint, true);
 6     this.SetStyle(ControlStyles.DoubleBuffer, true);
 7     this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
 8     this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
 9     this.SetStyle(ControlStyles.StandardDoubleClick, false);
10     this.SetStyle(ControlStyles.Selectable, true);
11     this.BackColor = Color.Transparent;
12 }
13 #endregion

3、屬性定義,其中可以加入自己想要功能的特殊字段再根據自己的需要進行處理

  1 #region 屬性
  2 
  3 [Category("放肆雷特擴展屬性"), Description("輸入最大字符數")]
  4 public int MaxLength
  5 {
  6     get { return BaseText.MaxLength; }
  7     set { BaseText.MaxLength = value; }
  8 
  9 }
 10 
 11 [Category("放肆雷特擴展屬性"), Description("與控件關聯的文本")]
 12 public new string Text
 13 {
 14     get
 15     {
 16         return BaseText.Text;
 17     }
 18     set
 19     {
 20         BaseText.Text = value;
 21     }
 22 }
 23 
 24 [Category("放肆雷特擴展屬性"), Description("將控件設為密碼顯示")]
 25 public bool IsPass
 26 {
 27     get
 28     {
 29         return BaseText.UseSystemPasswordChar;
 30     }
 31     set
 32     {
 33         BaseText.UseSystemPasswordChar = value;
 34     }
 35 }
 36 
 37 [Category("放肆雷特擴展屬性"), Description("密碼顯示字符")]
 38 public char PassChar
 39 {
 40     get
 41     {
 42         return BaseText.PasswordChar;
 43     }
 44     set
 45     {
 46         BaseText.PasswordChar = value;
 47     }
 48 }
 49 
 50 [Category("放肆雷特擴展屬性"), Description("將控件設為多行文本顯示")]
 51 public bool Multiline
 52 {
 53     get
 54     {
 55         return BaseText.Multiline;
 56     }
 57     set
 58     {
 59         BaseText.Multiline = value;
 60         if (value)
 61         { 
 62             BaseText.Height = this.Height - 6; 
 63         }
 64         else
 65         {
 66             base.Height = 22;
 67             BaseText.Height = 16;
 68             this.Invalidate();
 69         }
 70 
 71     }
 72 }
 73 
 74 [Category("放肆雷特擴展屬性"), Description("設置控件中文本字體")]
 75 public Font font
 76 {
 77     get
 78     {
 79         return BaseText.Font;
 80     }
 81     set
 82     {
 83         BaseText.Font = value;
 84     }
 85 }
 86 
 87 [Category("放肆雷特擴展屬性"), Description("將控件設為只讀")]
 88 public bool ReadOnly
 89 {
 90     get
 91     {
 92         return BaseText.ReadOnly;
 93     }
 94     set
 95     {
 96         BaseText.ReadOnly = value;
 97     }
 98 }
 99 
100 [Category("放肆雷特擴展屬性"), Description("多行文本的編輯行")]
101 public String[] lines
102 {
103     get
104     {
105         return BaseText.Lines;
106     }
107     set
108     {
109         BaseText.Lines = value;
110     }
111 }
112 
113 [Category("放肆雷特擴展屬性"), Description("是否顯示圖標")]
114 public bool Isico
115 {
116     get
117     {
118         return _Isico;
119     }
120     set
121     {
122         _Isico = value;
123         if (value)
124         {
125             if (_Ico != null)
126             {
127                 BaseText.Location = new Point(_IcoPadding.Left + _Ico.Width, 3);
128                 BaseText.Width = BaseText.Width - _IcoPadding.Left - _Ico.Width;
129             }
130             else
131             {
132                 BaseText.Location = new Point(25, 3);
133                 BaseText.Width = BaseText.Width - 25;
134             }
135         }
136         this.Invalidate();
137     }
138 }
139 
140 [Category("放肆雷特擴展屬性"), Description("圖標文件")]
141 public Bitmap Ico
142 {
143     get
144     {
145         return _Ico;
146     }
147     set
148     {
149         _Ico = value;
150     }
151 }
152 
153 [Category("放肆雷特擴展屬性"), Description("控件內部間距,圖標文件")]
154 public Padding IcoPadding
155 {
156     get { return _IcoPadding; }
157     set
158     {
159         _IcoPadding = value;
160         this.Invalidate();
161     }
162 }
163 #endregion

4、委托,委托圖標點擊事件

1 #region 委托
2 public event EventHandler IcoOnclick;
3 #endregion

5、方法處理

 1 #region 方法
 2 protected override void OnPaint(PaintEventArgs e)
 3 {
 4     Rectangle rc = this.ClientRectangle;
 5     Graphics g = e.Graphics;
 6     ImageDrawRect.DrawRect(g, _TextBoxBackImg, rc, Rectangle.FromLTRB(10, 10, 10, 10), (int)state, 5);
 7     if (_Isico)
 8     {
 9         if (_Ico != null)
10         {
11             g.DrawImage(_Ico, new Point(_IcoPadding.Left, _IcoPadding.Top));
12         }
13     }
14     base.OnPaint(e);
15 }
16 
17 private void TextBoxEx_Resize(object sender, EventArgs e)
18 {
19     if (this.Height > 22)
20     {
21         Multiline = true;
22     }
23     else
24     {
25         this.Height = 22;
26         Multiline = false;
27     }
28 }
29 
30 private void NotifyIcoOnclick()
31 {
32     if (IcoOnclick != null)
33     {
34         IcoOnclick(this, EventArgs.Empty);
35     }
36 }
37 
38 public void AppendText(string ss)
39 {
40     BaseText.AppendText(ss);
41 }
42 
43 private void BaseText_MouseEnter(object sender, EventArgs e)
44 {
45     state = State.MouseOver;
46     this.Invalidate();
47 }
48 
49 private void BaseText_MouseLeave(object sender, EventArgs e)
50 {
51     state = State.Normal;
52     this.Invalidate();
53 }
54 
55 private void TextBoxEx_MouseUp(object sender, MouseEventArgs e)
56 {
57     if (_Ico != null)
58     {
59         if (new Rectangle(_IcoPadding.Left, _IcoPadding.Top, _Ico.Width, _Ico.Height).Contains(e.X, e.Y))
60         {
61             NotifyIcoOnclick();
62         }
63     }
64 }
65 
66 private void TextBoxEx_MouseEnter(object sender, EventArgs e)
67 {
68     state = State.MouseOver;
69     this.Invalidate();
70 }
71 
72 private void TextBoxEx_MouseLeave(object sender, EventArgs e)
73 {
74     state = State.Normal;
75     this.Invalidate();
76 }
77 #endregion

OK,寫完收工…這個控件功力強大,使用簡單很符合中國程序猿的使用習慣直接從工具欄拖放即可..如果還有不懂的歡迎進行留言。下一篇就開始講360安全衛士最上面一排的水晶按鈕的制作敬請期待喔。。


本文來自 放肆雷特 | 胖子的技術博客


免責聲明!

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



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