自定义控件之带水印的Textbox
代码地址:
http://download.csdn.net/detail/u010312811/9553195
Windows消息处理:
http://www.cnblogs.com/imstrive/p/5596030.html
1.私有变量
1 //水印文字
2 private string _waterText = String.Empty;
3 //水印颜色
4 private Color _waterColor = Color.DarkGray;
2.属性
1 /// <summary>
2 /// 水印文字
3 /// </summary>
4 [Description("水印文字"),Category("SLT")]
5 public string WaterText
6 {
7 get { return _waterText; }
8 set { _waterText = value; }
9 }
10 /// <summary>
11 /// 水印颜色
12 /// </summary>
13 [Description("水印文字"),Category("SLT")]
14 public Color WaterColor
15 {
16 get { return _waterColor; }
17 set { _waterColor = value; }
18 }
3.Override Methods
1 protected override void WndProc(ref Message m)
2 {
3 base.WndProc(ref m);
4
5 if (m.Msg == (int)WindowsMessage.WM_PAINT)
6 {
7 WmPaintWater(ref m);
8 }
9 }
4.方法
1 private void WmPaintWater(ref Message m)
2 {
3 //获取控件绑定到的窗口句柄
4 using (Graphics g = Graphics.FromHwnd(base.Handle))
5 {
6 if (string.IsNullOrEmpty(this.Text) && !String.IsNullOrEmpty(this.WaterText) && !this.Focused)
7 {
8 TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
9 TextRenderer.DrawText(g, WaterText, this.Font, this.ClientRectangle, WaterColor, flags);
10 }
11 }
12 }
菜鸟一枚,但有一颗不断进取的心;
兴趣所至,相信自己终会成功!!!!!
加油,imstrive