zoukankan      html  css  js  c++  java
  • 重写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     }
    Top
    收藏
    关注
    评论
  • 相关阅读:
    SpringBoot整合ActiveMQ同时支持P2P和发布订阅模式(三)
    SpringBoot整合ActiveMQ的publish/subscribe发布订阅模式(二)
    Windows启动ActiveMQ报Wrapper Stopped错误
    IDEA从远程仓库克隆项目
    Git的安装
    IDEA上传项目到使用github上
    Mybaits的逆向工程
    posman测试接口需要登录验证的使用
    SSM整合SpringSecurity
    SpringBoot整合MongoDB的连接用户名和密码问题
  • 原文地址:https://www.cnblogs.com/Joy-et/p/4280006.html
Copyright © 2011-2022 走看看