Windows 控件限制用户的基本法门(C#.NET 篇)

/** ****************************************************************

Windows 控件限制用户的基本法门 (.NET 篇 )

C#.NET 的在下面

-------------------------------------------------------------------

本代码演示 控制用户的输入的基本方式(屏蔽非数字字符输入)

.net 下限制用户输入,看见很多人是在 键盘,或 textBox 的 TextChanged 事件里做

个人认为那样是不正确的,

1. 不能限制用户的粘贴

2. 严重干扰数据绑定等操作

3. 有时还需要备份原始数据进行还原

其实正确的限制输入的时机是在 ,windows 消息 WM_CHAR 触发时

但 .net 恰恰没有提供这个消息的事件映射 . 怎么办 ?

提供方案两列 :

  1. 继承 TextBox 重写 WndProc 函数 ( 优点点 oo 编程的优点我不说了 )

处理

if (m.Msg==WM_CHAR){

// 然后取 m.WParam 进行判断 m.WParam 就是用户输入的字符的 int 表示方式

// 如果是被限制的字符 直接 Return

// 不走 base.WndProc (ref m);

}

if(m.Msg==WM_PASTE)

{

// 判断剪贴板的数据是否是符合要求如果符合不做任何处理

// 否则 Return 不走默然处理即可

}

base.WndProc (ref m);

  1. 利用 API SetWindowLong 替换默认的处理消息的函数进行处理

本文写的就是这种 , 演示如何声明 API 而且本方法很多语言都可以使用 ,

但如果程序中有多个需要限制输入的控件而且相做通用类库的话

使用建议使用方案一

废话不多说了看代码吧 .

  • ***************************************************************** */

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

using System.Text.RegularExpressions;

using System.Diagnostics;

namespace SETWNDPROC

{

///

1<summary>
2
3///  Form1  的摘要说明。 
4
5///  </summary>

public class Form1 : System.Windows.Forms.Form

{

// 声明一个委托

public delegate IntPtr NewWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

//API 具体帮助请察看 MSDN 或到 MS 网站上去找

[DllImport("user32.dll", CharSet=CharSet.Auto)]

public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, NewWndProc wndproc);

[DllImport("user32.dll", CharSet=CharSet.Auto)]

public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

// 没用到

[DllImport("user32.dll", CharSet=CharSet.Auto)]

public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", CharSet=CharSet.Auto)]

public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

//SetWindowLong 用的常数,不知道什么意识的去看 msdn 吧

public const int GWL_WNDPROC = -4;

// 右键菜单消息

public const int WM_CONTEXTMENU = 0x007b;

// 粘贴消息

public const int WM_PASTE = 0x0302;

// 输入字符消息(键盘输入的,输入法输入的好像不是这个消息)

public const int WM_CHAR = 0x0102;

// 一定要声明为实列变量否则,局部变量发送给 API 后很容易被 _u71 ?C 回收,

// 会出现根本无法捕获的异常

private NewWndProc wpr= null ;

// 备份的默然处理函数

private IntPtr oldWndProc=IntPtr.Zero;

private System.Windows.Forms.TextBox textBox1;

///

1<summary>
2
3///  必需的设计器变量。 
4
5///  </summary>

private System.ComponentModel.Container components = null ;

public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent_u-29693 ? 用后添加任何构造函数代码

//

}

///

1<summary>
2
3///  清理所有正在使用的资源。 
4
5///  </summary>

protected override void Dispose( bool disposing )

{

if ( disposing )

{

if (components != null )

{

components.Dispose();

}

}

base .Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

///

1<summary>
2
3///  设计器支持所需的方法  \-  不要使用代码编辑器修改 
4
5///  此方法的内容。 
6
7&lt;SPAN lang=EN-US style="FONT-SI</summary>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus