当前位置:编程学习 > C#/ASP.NET >>

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);



2)利用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

{

/// <summary>

/// Form1 的摘要说明。

/// </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;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;



public Form1()

{

//

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

//

InitializeComponent();



//

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

//

}



/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}



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

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.textBox1 = new System.Windows.Forms.TextBox();

this.SuspendLayout();

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(32, 16);

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 0;

this.textBox1.Text = "555";

this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;



//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(152, 53);

this.Controls.Add(this.textBox1);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

this.Closed += new System.EventHandler(this.Form1_Closed);

this.ResumeLayout(false);



}

#endregion



/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{



Application.Run(new Form1());







}



private IntPtr TextBoxWndProc(IntPtr_u104 ?Wnd, int msg, IntPtr wParam, IntPtr lParam)

{

IntPtr returnVar=IntPtr.Zero;



switch (msg)

{

//粘贴消息包括 Ctrl+V Or 右键菜单粘贴

case WM_PASTE:

//取剪贴板对象

IDataObject iData = Clipboard.GetDataObject();

//判断是否是Text

if(iData.GetDataPresent(DataFormats.Text))

{

//取数据

string str;

str = (String)iData.GetData(Dat

上一个:用Visual C#调用Windows API函数
下一个:C#一个封装的加密解密类

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,