Windows control limits the user's basic method (C # .NET)

xiaoxiao2021-03-06  42

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

Windows control limits the basic method of the user (.NET)

C # .NET in the following

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

This code demonstrates the basic way for users' input (shielding non-numeric characters)

Under the .NET limit user input, see many people doing in the keyboard, or Textbox's TextChanged event

Individual thinks that is incorrect,

1. Cannot limit users' paste

2. Seriously interfere with data binding and other operations

3. Sometimes you need to back up the original data for restore

In fact, the correct limitation of the input is when the Windows message wm_char triggered.

But .NET does not have an event mapping of this message. What should I do?

Provide two columns of schemes:

1) Inherit the TEXTBOX to rewrite the WndProc function (the advantages of the OO programming, I don't say it)

deal with

IF (M.MSG == WM_CHAR) {

/ / Then take M.WParam to determine that M.WParam is the intidation method of the character entered by the user.

// If it is a restricted character directly Return

// Don't leave Base.WndProc (REF M);

}

IF (m.msg == wm_paste)

{

/ / Judging whether the data of the clipboard is in line with if it does not do anything

/ / Otherwise, Return will not be handled by unlicensed.

}

Base.WndProc (Ref M);

2) Use the API setWindowlong replace the default process of processing messages to process

This article writes this, demonstrates how to declare the API and you can use many languages.

However, if there are multiple controls that need to limit the input controls, there are multiple libraries that need to be restricted.

Use recommended usage

I don't have much nonsense.

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

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

{

///

/// Form1 summary description.

///

Public Class Form1: System.Windows.Forms.form

{

// Declare a commission

Public Delegate INTPTR NewWndProc (INTPTR HWND, INT MSG, INTPTR WPARAM, INTPTR LPARAM);

// API specifically helps see MSDN or go to the MS website to find

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern INTPTR SETWINDOWLONG (INTPTR HWND, INT NINDEX, NEWNDPROC WNDPROC);

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern INTPTR SETWINDOWLONG (INTPTR HWND, INT NINDEX, INTPTR DWNEWLON);

// Nothing [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);

// setwindowl uses constant, don't know what consciousness goes to see MSDN

Public const INT GWL_WNDPROC = -4;

/ / Right-click menu message

Public const INT WM_CONTEXTMENU = 0x007B;

// Paste message

Public const INT WM_PASTE = 0x0302;

// Enter a character message (if the keyboard is entered, the input method is input, it is not this message)

Public const Int wm_char = 0x0102;

// must be declared as a listed variable, it is easy to be recycled after the local variable is sent to the API,

// will have an exception that cannot be captured.

Private newWndProc WPR = NULL;

// Backup Silent Processing Function

Private INTPTR OLDWNDPROC = INTPTR.ZERO;

Private system.windows.Forms.TextBox textBox1;

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: In INITIALIZECOMPONENT_U-29693? Add any constructor after use

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

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

}

#ndregion

///

/// The main entry point for the application.

///

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

{

// Paste message includes Ctrl V or right-key menu paste

Case WM_PASTE:

// Take a shear

IdataObject iData = clipboard.getdataObject ();

/ / Judgment is TEXT

Idata.getdataPresent (DataFormats.Text))

{

// Take data

String Str;

Str = (String) iData.getdata (dataformats.text);

/ *

If you need a positive or negative, first determine the location of the cursor on Textbox

If the cursor can be used in front, ^ ((/ | -) / d)? / D *) $

The following WM_CHAR should also do corresponding changes

* /

// If it is a number (you can paste the jump out)

IF (Regex.ismatch (STR, @ "^ (/ d {1,}) $")).

}

/ / Can't paste

Return (INTPTR) 0;

Case WM_CHAR:

Int keychar = wpaham.toint32 ();

Debug.writeline (keychar);

Bool Charok = (Keychar> 47 && Keychar <58) || // Digital

Keychar == 8 || //

Keychar == 3 || Keychar == 22 || Keychar == 24; // Copy, Paste, Cut

// If it is not required, the character wPARAM is changed to character 0

// Return (INTPTR) 0; Also, but no keyboard tone is forbidden

IF (! charok) wparam = (intptr) 0;

Break;

// Prohibit right-click menu (if needed)

// Case WM_CONTextMenu:

// Return (INTPTR) 0;

}

// Toned the default processing of the backup

Returnvar = CallWindowProc (OldWndProc, HWND, MSG, WPARAM, LPARAM); Return Returnvar;

}

Private Void Form1_Load (Object Sender, System.EventArgs E)

{

THIS.SHOW ();

// Backup default handler

//oldwndproc=getWindowlong (TextBox1.handle, GWL_WndProc);

// Latest delegation (here is the callback function)

WPR = New newWndProc (this.TextBoxWndProc);

// Replace the default processing function of the control (and return the original default processing function, is a geological unit of a function pointer)

OldWndProc = setWindowlong (TextBox1.handle, GWL_WNDPROC, WPR);

}

Private void form1_closed (Object Sender, System.EventArgs E)

{

/ / Restore default handler

IF (! OldWndProc.equals (INTPTR.ZERO))

SetWindowlong (TextBox1.Handle, GWL_WndProc, OldWndProc);

}

}

}

Nice, the article from Flashelf http://blog.9cbs.net/flashelf/archive/2004/10/31/161024.aspx

转载请注明原文地址:https://www.9cbs.com/read-61671.html

New Post(0)