using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace LYFZ.ComponentLibrary
{
public class TextBoxBasicEx : System.Windows.Forms.TextBox
{
public enum QQControlState
{
///
/// 正常状态
///
Normal = 0,
///
/// /鼠标进入
///
Highlight = 1,
///
/// 鼠标按下
///
Down = 2,
///
/// 获得焦点
///
Focus = 3,
///
/// 控件禁止
///
Disabled = 4
}
//private bool _isCloseQaJiaoChar = true;
/////
///// 是否限制全角输入
/////
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
//[Category("控件扩展属性"), Description("是否限制全角输入")]
//public bool IsCloseQaJiaoChar
//{
// get { return _isCloseQaJiaoChar; }
// set { _isCloseQaJiaoChar = value; }
//}
/////
///// 重写输入限制
/////
/////
//protected override void OnKeyPress(KeyPressEventArgs e)
//{
// if (this.IsCloseQaJiaoChar && LYFZ.BLL.OtherCommonModel.IsQaJiaoChar(e.KeyChar))
// { e.Handled = true; }
// else
// { base.OnKeyPress(e); }
//}
/////
///// 重写离开事件
/////
/////
//protected override void OnLeave(EventArgs e)
//{
// if (this.IsCloseQaJiaoChar)
// {
// if (LYFZ.BLL.OtherCommonModel.StringContainsQaJiao(this.Text.Trim()))
// { this.Text = ""; }
// }
// base.OnLeave(e);
//}
internal class ColorTable
{
public static Color QQBorderColor = Color.FromArgb(174, 168, 168); // //LightBlue = Color.FromArgb(173, 216, 230)
public static Color QQHighLightColor = Color.FromArgb(174, 168, 168); //GetColor(QQBorderColor, 255, -63, -11, 23); //Color.FromArgb(110, 205, 253)
public static Color QQHighLightInnerColor = Color.FromArgb(174, 168, 168); //GetColor(QQBorderColor, 255, -100, -44, 1); //Color.FromArgb(73, 172, 231);
}
///
/// 返回给定的颜色的ARGB的分量差值的颜色
///
///
/// A
/// R
/// G
/// B
///
public static Color GetColor(Color colorBase, int a, int r, int g, int b)
{
int a0 = colorBase.A;
int r0 = colorBase.R;
int g0 = colorBase.G;
int b0 = colorBase.B;
if (a + a0 > 255) { a = 255; } else { a = Math.Max(a + a0, 0); }
if (r + r0 > 255) { r = 255; } else { r = Math.Max(r + r0, 0); }
if (g + g0 > 255) { g = 255; } else { g = Math.Max(g + g0, 0); }
if (b + b0 > 255) { b = 255; } else { b = Math.Max(b + b0, 0); }
return Color.FromArgb(a, r, g, b);
}
#region Field
private QQControlState _state = QQControlState.Normal;
private Font _defaultFont = new Font("微软雅黑", 9);
//当Text属性为空时编辑框内出现的提示文本
private string _emptyTextTip;
private Color _emptyTextTipColor = Color.FromArgb(174, 168, 168);
#endregion
#region Constructor
public TextBoxBasicEx()
{
SetStyles();
this.Font = _defaultFont;
}
#endregion
#region Properites
[Description("当Text属性为空时编辑框内出现的提示文本")]
public String EmptyTextTip
{
get { return _emptyTextTip; }
set
{
if (_emptyTextTip != value)
{
_emptyTextTip = value;
base.Invalidate();
}
}
}
[Description("获取或设置EmptyTextTip的颜色")]
public Color EmptyTextTipColor
{
get { return _emptyTextTipColor; }
set
{
if (_emptyTextTipColor != value)
{
_emptyTextTipColor = value;
base.Invalidate();
}
}
}
#endregion
#region Override
protected override void OnMouseEnter(EventArgs e)
{
_state = QQControlState.Highlight;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
//if (_state == QQControlState.Highlight && Focused)
//{
// _state = QQControlState.Focus;
//}
//else if (_state == QQControlState.Focus)
//{
// _state = QQControlState.Focus;
//}
//else
//{
_state = QQControlState.Normal;
//}
base.OnMouseLeave(e);
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
_state = QQControlState.Highlight;
}
base.OnMouseDown(mevent);
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
{
if (ClientRectangle.Contains(mevent.Location))
{
_state = QQControlState.Highlight;
}
else
{
_state = QQControlState.Focus;
}
}
base.OnMouseUp(mevent);
}
protected override void OnLostFocus(EventArgs e)
{
_state = QQControlState.Normal;
base.OnLostFocus(e);
}
protected override void OnEnabledChanged(EventArgs e)
{
if (Enabled)
{
_state = QQControlState.Normal;
}
else
{
_state = QQControlState.Disabled;
}
base.OnEnabledChanged(e);
}
protected override void WndProc(ref Message m)
{//TextBox是由系统进程绘制,重载OnPaint方法将不起作用
base.WndProc(ref m);
if (m.Msg == Win32.WM_PAINT || m.Msg == Win32.WM_CTLCOLOREDIT)
{
WmPaint(ref m);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_defaultFont != null)
{
_defaultFont.Dispose();
}
}
_defaultFont = null;
base.Dispose(disposing);
}
#endregion
#region Private
private void SetStyles()
{
// TextBox由系统绘制,不能设置 ControlStyles.UserPaint样式
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
UpdateStyles();
}
private void WmPaint(ref Message m)
{
Graphics g = Graphics.FromHwnd(base.Handle);
g.SmoothingMode = SmoothingMode.HighQuality;
if (!Enabled)
{
_state = QQControlState.Disabled;
}
switch (_state)
{
case QQControlState.Normal:
DrawNormalTextBox(g);
break;
case QQControlState.Highlight:
DrawHighLightTextBox(g);
break;
case QQControlState.Focus:
DrawFocusTextBox(g);
break;
case QQControlState.Disabled:
DrawDisabledTextBox(g);
break;
default:
break;
}
if (Text.Length == 0 && !string.IsNullOrEmpty(EmptyTextTip) && !Focused)
{
TextRenderer.DrawText(g, EmptyTextTip, Font, ClientRectangle, EmptyTextTipColor, GetTextFormatFlags(TextAlign, RightToLeft == RightToLeft.Yes));
}
}
private void DrawNormalTextBox(Graphics g)
{
using (Pen borderPen = new Pen(ColorTable.QQHighLightInnerColor))
{
g.DrawRectangle(
borderPen,
new Rectangle(
ClientRectangle.X,
ClientRectangle.Y,
ClientRectangle.Width - 1,
ClientRectangle.Height - 1));
}
}
private void DrawHighLightTextBox(Graphics g)
{
using (Pen highLightPen = new Pen(ColorTable.QQHighLightColor))
{
Rectangle drawRect = new Rectangle(
ClientRectangle.X,
ClientRectangle.Y,
ClientRectangle.Width - 1,
ClientRectangle.Height - 1);
// g.DrawRectangle(highLightPen, drawRect);
//InnerRect
drawRect.Inflate(-1, -1);
highLightPen.Color = ColorTable.QQHighLightInnerColor;
g.DrawRectangle(highLightPen, drawRect);
}
}
private void DrawFocusTextBox(Graphics g)
{
using (Pen focusedBorderPen = new Pen(ColorTable.QQHighLightInnerColor))
{
g.DrawRectangle(
focusedBorderPen,
new Rectangle(
ClientRectangle.X,
ClientRectangle.Y,
ClientRectangle.Width - 1,
ClientRectangle.Height - 1));
}
}
private void DrawDisabledTextBox(Graphics g)
{
using (Pen disabledPen = new Pen(ColorTable.QQHighLightInnerColor))
{
g.DrawRectangle(disabledPen,
new Rectangle(
ClientRectangle.X,
ClientRectangle.Y,
ClientRectangle.Width - 1,
ClientRectangle.Height - 1));
}
}
private static TextFormatFlags GetTextFormatFlags(HorizontalAlignment alignment, bool rightToleft)
{
TextFormatFlags flags = TextFormatFlags.WordBreak |
TextFormatFlags.SingleLine;
if (rightToleft)
{
flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
}
switch (alignment)
{
case HorizontalAlignment.Center:
flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
break;
case HorizontalAlignment.Left:
flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
break;
case HorizontalAlignment.Right:
flags |= TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
break;
}
return flags;
}
#endregion
public class Win32
{
#region Window Const
public const int WM_KEYDOWN = 0x0100;
public const int WM_KEYUP = 0x0101;
public const int WM_CTLCOLOREDIT = 0x133;
public const int WM_ERASEBKGND = 0x0014;
public const int WM_LBUTTONDOWN = 0x0201;
public const int WM_LBUTTONUP = 0x0202;
public const int WM_LBUTTONDBLCLK = 0x0203;
public const int WM_WINDOWPOSCHANGING = 0x46;
public const int WM_PAINT = 0xF;
public const int WM_CREATE = 0x0001;
public const int WM_ACTIVATE = 0x0006;
public const int WM_NCCREATE = 0x0081;
public const int WM_NCCALCSIZE = 0x0083;
public const int WM_NCPAINT = 0x0085;
public const int WM_NCACTIVATE = 0x0086;
public const int WM_NCLBUTTONDOWN = 0x00A1;
public const int WM_NCLBUTTONUP = 0x00A2;
public const int WM_NCLBUTTONDBLCLK = 0x00A3;
public const int WM_NCMOUSEMOVE = 0x00A0;
public const int WM_NCHITTEST = 0x0084;
public const int HTLEFT = 10;
public const int HTRIGHT = 11;
public const int HTTOP = 12;
public const int HTTOPLEFT = 13;
public const int HTTOPRIGHT = 14;
public const int HTBOTTOM = 15;
public const int HTBOTTOMLEFT = 0x10;
public const int HTBOTTOMRIGHT = 17;
public const int HTCAPTION = 2;
public const int HTCLIENT = 1;
public const int WM_FALSE = 0;
public const int WM_TRUE = 1;
#endregion
#region Public extern methods
[DllImport("gdi32.dll")]
public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);
[DllImport("user32.dll")]
public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
public static extern int DeleteObject(int hObject);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
#endregion
}
}
}