重写TextBox实现显示提示信息

  1     /// <summary>
  2     /// TextBox提示信息
  3     /// </summary>
  4     /// <author>Tim_et</author>
  5     /// <description>为TextBox提供提示信息,有ToolTip方式,边框提示方式,ErrorProvider方式</description>
  6     [ToolboxItem(true)]
  7     [ToolboxBitmap("information.bmp")]
  8     public partial class TipTextBox : TextBox
  9     {
 10         /// <summary>
 11         /// 窗体句柄绘制
 12         /// </summary>
 13         /// <param name="hWnd"></param>
 14         /// <returns></returns>
 15         [System.Runtime.InteropServices.DllImport("user32.dll")]
 16         static extern IntPtr GetWindowDC(IntPtr hWnd);
 17         [System.Runtime.InteropServices.DllImport("user32.dll")]
 18         static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
 19
 20
 21         //private string pattern = @"^[0-9]*$";
 22         private int WM_PAINT = 0xF;
 23         private string tipText;
 24         private Color tipTextColor = Color.DarkGray;
 25         private bool isShowTipText = false;
 26         private Color tipBorderColor = Color.Red;
 27         private Color normalBorderColor = Color.DimGray;
 28         private bool isShowTipBorder = false;
 29         #region errorProvider
 30         private ErrorProvider errorProvider;
 31         private string errorMsg = string.Empty;
 32         /// <summary>
 33         /// 错误信息
 34         /// </summary>
 35         public string ErrorMsg
 36         {
 37             get { return errorMsg; }
 38             set { errorMsg = value; }
 39         }
 40         /// <summary>
 41         /// 闪烁风格
 42         /// </summary>
 43         public ErrorBlinkStyle BlinkStyle
 44         {
 45             get { return errorProvider.BlinkStyle; }
 46             set { errorProvider.BlinkStyle = value; }
 47         }
 48         /// <summary>
 49         /// 闪烁间隔
 50         /// </summary>
 51         public int BlinkRate
 52         {
 53             get { return errorProvider.BlinkRate; }
 54             set { errorProvider.BlinkRate = value; }
 55         }
 56
 57         #endregion
 58
 59
 60         /// <summary>
 61         /// 提示信息内容
 62         /// </summary>
 63         [DefaultValue("")]
 64         public string TipText
 65         {
 66             get { return tipText; }
 67             set
 68             {
 69                 tipText = value;
 70                 base.Invalidate();
 71             }
 72         }
 73         /// <summary>
 74         /// 提示信息的颜色
 75         /// </summary>
 76         [DefaultValue(typeof(Color), "DarkGray")]
 77         public Color TipTextColor
 78         {
 79             get { return tipTextColor; }
 80             set
 81             {
 82                 tipTextColor = value;
 83                 base.Invalidate();
 84             }
 85         }
 86         /// <summary>
 87         /// 是否显示提示信息
 88         /// </summary>
 89         public bool IsShowTipText
 90         {
 91             get { return isShowTipText; }
 92             set { isShowTipText = value; }
 93         }
 94         /// <summary>
 95         /// 提示边框颜色
 96         /// </summary>
 97         public Color TipBorderColor
 98         {
 99             get { return tipBorderColor; }
100             set { tipBorderColor = value; }
101         }
102         /// <summary>
103         /// 正常时颜色(无需提示时)
104         /// </summary>
105         public Color NormalBorderColor
106         {
107             get { return normalBorderColor; }
108             set { normalBorderColor = value; }
109         }
110         /// <summary>
111         /// 是否显示提示边框
112         /// </summary>
113         public bool IsShowTipBorder
114         {
115             get { return isShowTipBorder; }
116             set { isShowTipBorder = value; }
117         }
118
119         public TipTextBox()
120             : base()
121         {
122             errorProvider = new ErrorProvider();
123         }
124
125
126         protected override void WndProc(ref Message m)
127         {
128
129             base.WndProc(ref m);
130
131             if (m.Msg == WM_PAINT || m.Msg == 0x133)
132             {
133                 if (isShowTipBorder)
134                 {
135                     IntPtr hDC = GetWindowDC(m.HWnd);
136                     if (hDC.ToInt32() == 0)
137                     {
138                         return;
139                     }
140
141                     System.Drawing.Pen pen = new Pen(this.tipBorderColor, 1);
142                     if (this.Focused || this.Text.Length > 0)
143                     {
144                         pen.Color = normalBorderColor;
145                     }
146                     //绘制边框
147                     System.Drawing.Graphics g = Graphics.FromHdc(hDC);
148                     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
149                     g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
150                     pen.Dispose();
151                     m.Result = IntPtr.Zero;
152                     //释放
153                     ReleaseDC(m.HWnd, hDC);
154                 }
155
156                 if (IsShowTipText)
157                 {
158                     ///使用TextRenderer绘制底层的显示字样
159                     using (Graphics graphics = Graphics.FromHwnd(this.Handle))
160                     {
161                         if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(TipText) && !Focused)
162                         {
163                             TextFormatFlags textFormatFlags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
164                             if (this.RightToLeft == RightToLeft.Yes)
165                             {
166                                 textFormatFlags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
167                             }
168                             TextRenderer.DrawText(graphics, TipText, this.Font, base.ClientRectangle, TipTextColor, textFormatFlags);
169                         }
170                     }
171                 }
172
173             }
174         }
175
176         /// <summary>
177         /// 显示errorProvider
178         /// </summary>
179         /// <param name="msg"></param>
180         public void ShowErrorProvider(string msg)
181         {
182             errorProvider.SetError(this, msg);
183         }
184
185         private void InitializeComponent()
186         {
187             this.SuspendLayout();
188             this.ResumeLayout(false);
189
190         }
191     }
时间: 2025-01-10 01:41:11

重写TextBox实现显示提示信息的相关文章

div显示提示信息

div显示提示信息 <body> <style type="text/css"> a.link{position:relative;} a.link div.tips{ border:1px solid #333; padding:10px; background-color:#ff0; position:absolute; top:16px; left:0px; display:none; } a.link:hover{} a.link:hover div.t

C#——DataGridView选中行,在TextBox中显示选中行的内容

C#--DataGridView选中行,在TextBox中显示选中行的内容,在DataGridView的SelectionChanged实践中设置如下代码 private void dataGridView1_SelectionChanged(object sender, EventArgs e) { int index = dataGridView1.SelectedRows[0].Index; //获取选中行的行号 textBox1.Text = dataGridView1.Rows[ind

VC/MFC 当鼠标移到控件上时显示提示信息

VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法. 一般用法步骤:  添加CToolTipCtrl成员变量 m_tt.  在父窗口中调用EnableToolTips(TRUE);  在窗口的OnCreate(或者其他适当的位置)中向ToolTip中添加需要显示Tip的子窗口,并同时指定相应的显示字串CToolTipCtrl::AddTool(pW

jQuery鼠标悬停显示提示信息窗体

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>鼠标悬停显示提示信息窗体</titl

137在搜索框中实现下拉列表效果(扩展知识:表格视图数据源为空数据时显示提示信息)

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 #import "DropDownListViewController.h" 3 4 @interface ViewController : UITableViewController<UISearchBarDelegate, PassValueDelegate> 5 @property (strong, nonatomic) UISearchBar *sear

jQuery鼠标悬停显示提示信息窗口

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>鼠标悬停显示提示信息窗口</title&g

jquery 文本框失去焦点显示提示信息&amp;&amp;单击置空文本框

/** * @param {Object} id 'cname' * @param {Object} pointout '请输入收藏夹名称' */function pointoutListener(id, pointout) { //为cname添加单击时间和失去焦点的监听器 var myinput = document.getElementById(id); addListener(myinput, "click", function(){ var value1 = $('#'+id

要求:鼠标移入显示提示信息框;鼠标离开,信息框消失,消失的效果延迟

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatib

TextBox显示提示信息

属性placeholder可以设置TextBox 提示信息如: <asp:TextBox ID ="txt1" runat ="server"  ToolTip ="123" Wrap ="true" placeholder="跟踪号/订单号" ></asp:TextBox>