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 windowsTest
{
[ToolboxItem(true)]
public class WTextBox : 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 new BorderStyle BorderStyle
{
get { return BorderStyle.FixedSingle; }
}
#region
[Category("Appearance"),
Description("Border color, Only for BorderStyle equal FixedSingle"),
DefaultValue(typeof(Color), "#A7A6AA")]
public Color BorderColor
{
get
{
return this._BorderColor;
}
set
{
this._BorderColor = value;
this.Invalidate();
}
}
[Category("Appearance"),
Description("Hot color, Only for BorderStyle equal FixedSingle"),
DefaultValue(typeof(Color), "#996699")]
public Color HotColor
{
get
{
return this._HotColor;
}
set
{
this._HotColor = value;
this.Invalidate();
}
}
#endregion
public WTextBox()
: base()
{
}
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));
}
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来记录合法字符,不在此字符串中认为是不接受的字符,
/// 覆盖窗口过程!处理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 ;
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);
}
【reprinted from http://lenny119.blog.sohu.com】