重写TextBox实现显示提示信息
/// <summary>
/// TextBox提示信息
/// </summary>
/// <author>Tim_et</author>
/// <description>为TextBox提供提示信息,有ToolTip方式,边框提示方式,ErrorProvider方式</description>
[ToolboxItem(true)]
[ToolboxBitmap("information.bmp")]
public partial class TipTextBox : TextBox
{
/// <summary>
/// 窗体句柄绘制
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); //private string pattern = @"^[0-9]*$";
private int WM_PAINT = 0xF;
private string tipText;
private Color tipTextColor = Color.DarkGray;
private bool isShowTipText = false;
private Color tipBorderColor = Color.Red;
private Color normalBorderColor = Color.DimGray;
private bool isShowTipBorder = false;
#region errorProvider
private ErrorProvider errorProvider;
private string errorMsg = string.Empty;
/// <summary>
/// 错误信息
/// </summary>
public string ErrorMsg
{
get { return errorMsg; }
set { errorMsg = value; }
}
/// <summary>
/// 闪烁风格
/// </summary>
public ErrorBlinkStyle BlinkStyle
{
get { return errorProvider.BlinkStyle; }
set { errorProvider.BlinkStyle = value; }
}
/// <summary>
/// 闪烁间隔
/// </summary>
public int BlinkRate
{
get { return errorProvider.BlinkRate; }
set { errorProvider.BlinkRate = value; }
} #endregion /// <summary>
/// 提示信息内容
/// </summary>
[DefaultValue("")]
public string TipText
{
get { return tipText; }
set
{
tipText = value;
base.Invalidate();
}
}
/// <summary>
/// 提示信息的颜色
/// </summary>
[DefaultValue(typeof(Color), "DarkGray")]
public Color TipTextColor
{
get { return tipTextColor; }
set
{
tipTextColor = value;
base.Invalidate();
}
}
/// <summary>
/// 是否显示提示信息
/// </summary>
public bool IsShowTipText
{
get { return isShowTipText; }
set { isShowTipText = value; }
}
/// <summary>
/// 提示边框颜色
/// </summary>
public Color TipBorderColor
{
get { return tipBorderColor; }
set { tipBorderColor = value; }
}
/// <summary>
/// 正常时颜色(无需提示时)
/// </summary>
public Color NormalBorderColor
{
get { return normalBorderColor; }
set { normalBorderColor = value; }
}
/// <summary>
/// 是否显示提示边框
/// </summary>
public bool IsShowTipBorder
{
get { return isShowTipBorder; }
set { isShowTipBorder = value; }
} public TipTextBox()
: base()
{
errorProvider = new ErrorProvider();
} protected override void WndProc(ref Message m)
{ base.WndProc(ref m); if (m.Msg == WM_PAINT || m.Msg == 0x133)
{
if (isShowTipBorder)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == )
{
return;
} System.Drawing.Pen pen = new Pen(this.tipBorderColor, );
if (this.Focused || this.Text.Length > )
{
pen.Color = normalBorderColor;
}
//绘制边框
System.Drawing.Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle(pen, , , this.Width - , this.Height - );
pen.Dispose();
m.Result = IntPtr.Zero;
//释放
ReleaseDC(m.HWnd, hDC);
} if (IsShowTipText)
{
///使用TextRenderer绘制底层的显示字样
using (Graphics graphics = Graphics.FromHwnd(this.Handle))
{
if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(TipText) && !Focused)
{
TextFormatFlags textFormatFlags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
if (this.RightToLeft == RightToLeft.Yes)
{
textFormatFlags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
}
TextRenderer.DrawText(graphics, TipText, this.Font, base.ClientRectangle, TipTextColor, textFormatFlags);
}
}
} }
} /// <summary>
/// 显示errorProvider
/// </summary>
/// <param name="msg"></param>
public void ShowErrorProvider(string msg)
{
errorProvider.SetError(this, msg);
} private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false); }
}
重写TextBox实现显示提示信息的更多相关文章
- 基于JQuery的浮动DIV显示提示信息并自动隐藏
/** * 浮动DIV定时显示提示信息,如操作成功, 失败等 * @param string tips (提示的内容) * @param int height 显示的信息距离浏览器顶部的高度 * @p ...
- div显示提示信息
div显示提示信息 <body> <style type="text/css"> a.link{position:relative;} a.link div ...
- MFC 学习之 鼠标移动到Toolbar按钮上显示提示信息(tooltip),状态栏也随之改变
1.在ResourceView里加入Toolbar资源,命名IDR_TOOLBAR1 2.在主程序的.h文件中加入变量: CToolBar m_toolbar;CImageList ...
- VC/MFC 当鼠标移到控件上时显示提示信息
VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...
- div显示提示信息【转】
div显示提示信息 <body> <style type="text/css"> a.link{position:relative;} a.link div ...
- ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点、线、圆,显示提示信息
ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点.线.圆,显示提示信息 /// <summary> /// 绘制界面上的点和线 /// ...
- jQuery实现鼠标悬停显示提示信息窗口的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C#——DataGridView选中行,在TextBox中显示选中行的内容
C#--DataGridView选中行,在TextBox中显示选中行的内容,在DataGridView的SelectionChanged实践中设置如下代码 private void dataGridV ...
- jQuery鼠标悬停显示提示信息窗体
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- 牛逼的验证码,printf返回值
牛逼的验证码,如下图, 结果是4321,为什么呢,主要是printf返回值问题?那么printf到底返回什么? 经查阅,printf的返回值是打印的字符个数,因此结果是4321就很明显了.
- CentOS 如何使用第三方软件库-EPEL与RPMForge、RPMFusion软件库
在CentOS下运行yum install flash-plugin或yum install mplayer的时候,提示库里没有找到这个软件?为什么会这样?因为CentOS是RHEL编译过来的,去掉了 ...
- Qt--将Qt 动态链接生成的exe及依赖dll打包方法
Qt静态编译链接生成的exe文件,不需依赖dll,可以独立运行,发布很方便. 但绝大多数用的都是Qt开源版本,如果用静态链接,会有些限制. 方法之一,就是用动态编译,然后把exe和需要的dll整合成一 ...
- SessionFactory的创建和Session的获得
1.当我们调用 Configuration config=new Configuration().configure(); 时候Hibernate会自己主动在当前的CLASSPATH中搜寻hibern ...
- Java EE (7) -- Java EE 6 Enterprise Architect Certified Master(1z0-807)
Application Design Concepts and Principles Identify the effects of an object-oriented approach to sy ...
- D3js-堆栈图
效果图: 源码: <%@ page language="java" import="java.util.*" pageEncoding="UTF ...
- 最小路径覆盖 hdu 1151 hdu 3335
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- Windows 8 应用开发 - 本地数据存储
原文:Windows 8 应用开发 - 本地数据存储 在应用中通常会遇到用户主动或被动存储信息的情况,当应用关闭后这些数据仍然会存储在本地设备上,用户下次重新激活应用时会自动加载这些数据.下 ...
- c++宏源证券编程
1,定义宏功能,作,func(x) = x-1? 最简单的, 假设x = 2则 #define func(x) 1 特化出来x,令 #define func_helper_2 1 #defi ...
- Windows 7下一个:该应用程序不能正常启动(0xc0150002)
在新系统中正确安装QQ2010无法执行,同一时候安装的TM2009也无法执行. 相同显示为"应用程序无法正常启动(0xc0150002). 请单击"确定" ...