在C#⾥怎么重写⼀个TextBox控件,例如添加输⼊提⽰信息using    System;
using    System.Drawing;
using    System.Windows.Forms;
namespace    ManuApp
{
///    <summary>
///    TextBoxNoManu    的摘要说明。
///    </summary>
public    class    TextBoxNoManu:System.Windows.Forms.TextBox
{
public    TextBoxNoManu()
{
//
//    TODO:    在此处添加构造函数逻辑
//
}
protected    override    void    WndProc(ref    Message    m)
{
if(m.Msg    !=    0x007B)
{
base.WndProc    (ref    m);
}
}
}
}
⾃定义TextBox控件的外观(需要重写消息循环)
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication2
{
[ToolboxItem(true)]
public partial class InfoTextBox : System.Windows.Forms.TextBox
{
[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 Color _BorderColor = Color.FromArgb(0xA7, 0xA6, 0xAA);
private Color _HotColor = Color.Green;
private string _tooltip = "点击这⾥输⼊信息";
private new BorderStyle BorderStyle
{
get { return BorderStyle.FixedSingle; }
}
#region
[Category("Appearance"), Description("输⼊提⽰信息")]
public string InfoText
{
get
return this._tooltip;
}
set
{
this._tooltip = value;
this.Invalidate();
}
}
[Category("Appearance"), Description("边框颜⾊"), DefaultValue(typeof(Color), "#A7A6AA")]
public Color BorderColor
{
get
{
return this._BorderColor;
}
set
{
this._BorderColor = value;
this.Invalidate();
}
}
[Category("Appearance"), Description("获得焦点颜⾊"), DefaultValue(typeof(Color), "#996699")]
public Color HotColor
{
get
{
return this._HotColor;
}
set
{
this._HotColor = value;
this.Invalidate();
}
}
#endregion
public InfoTextBox()
: base()
{
this.Text = "";
}
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
if (string.IsNullOrEmpty(this.SelectedText))
this.Cursor = Cursors.SizeAll;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
int _HotBlur = this.Height / 4;
if (e.X < this.Width + _HotBlur && e.X > this.Width - _HotBlur)
this.Cursor = Cursors.SizeWE;
else
{
if ((e.X > _HotBlur && e.X < this.Width - _HotBlur) || (e.Y > _HotBlur && e.Y < this.Height - _HotBlur))                    this.Cursor = Cursors.IBeam;
else
this.Cursor = Cursors.SizeAll;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
this.Cursor = Cursors.IBeam;
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) return;
if (this.BorderStyle == BorderStyle.FixedSingle)
{
System.Drawing.Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle(new Pen(this._BorderColor, 1), 0, 0, this.Width - 1, this.Height - 1);
g.DrawLine(new Pen(this._HotColor, 2), new Point(this.Width - 1, this.Height / 4), new Point(this.Width - 1, this.Height / 4 * 3));                }
if (Text.Trim().Length == 0 && !this.Focused)
{
SolidBrush drawBrush = new SolidBrush(Color.BurlyWood);
PointF drawPoint = new PointF(0F, (this.Height - this.FontHeight) / 2);
System.Drawing.Graphics g = Graphics.FromHdc(hDC);
g.DrawString(_tooltip, this.Font, drawBrush, drawPoint);
}
m.Result = IntPtr.Zero;
ReleaseDC(m.HWnd, hDC);
}
}
}
}
⼀个只能接收某些字符的textbox
经常某些输⼊的⽂本要求只能是数字等,⽐如qq登陆框上的qq帐号,如果按键不是数字,则没有反应。原理当然是很简单的,只需要在相应消息到来时阻⽌控件去处理消息即可。
这种例⼦很多,当然可以override keypress事件。也可以从textbox继承⼀个类,然后重写wndpro,从⽽⽆视某些消息。
最重要的消息是WM_CHAR。此外,还有⼏个特殊按键是永远不能屏蔽的,分别是backspace, delete,此外还有快捷键,ctrl-a,
ctrl-c,ctrl-x,ctrl-v.再此外,我们还要在执⾏粘贴时对⽂本做⼀次判断,不合法⽂本则被忽略。
可以⽤⼀个FilterString的string来记录合法字符,不在此字符串中认为是不接受的字符,
///<summary>
///覆盖窗⼝过程!处理WM_CHAR消息!
///</summary>
/
//<param name="m"></param>
protected override void WndProc(ref Message m)
{
int charcode = (int)m.WParam;
switch (m.Msg)
{
case WM_CHAR:
// 遇到⾮法字符,直接return即可过滤⾮法字符!break表⽰处理该字符                    //屏蔽⼩数点
if (charcode == (int)Keys.Decimal)
return;
// 注意delete,backspace字符不能过滤
/
/ ctrl-a,ctrl-c,ctrl-v快捷键操作不能屏蔽!
if (charcode == (int)Keys.Back || charcode == (int)Keys.Delete)
break;
//如果按下了CTRL键
if (charcode == 1    //ctrl a
|| charcode == 3  //ctrl c
|| charcode == 22  //ctrl v
|| charcode == 24    //ctrl x
)
break;
if (this.m_FilterStr.IndexOf((char)charcode) < 0)
return;
break;
case WM_KEYDOWN:
//ctrl-A 全选
if (Control.ModifierKeys == Keys.Control)
{
if(charcode==(int)Keys.A)
this.SelectAll();
}
break;
case WM_PASTE:
/
/粘贴消息
IDataObject obj = Clipboard.GetDataObject();
if (obj == null)
return;textbox控件边框设置
if (obj.GetDataPresent(DataFormats.Text))
{
string text = obj.GetData(DataFormats.Text) as string;
if (text == null)
return;
foreach (char c in text)
{
/
/查看是否含有过滤字符以外的字符!
if (this.m_FilterStr.IndexOf(c) < 0)
return;
}
}
break;
}
//处理消息
base.WndProc(ref m);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。