/ ************************************************** ******************
Windows control limits the basic method of the user (.NET)
Vb.net
-------------------------------------------------- -----------------
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.
/// summary>
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.
/// summary>
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.
/// summary>
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.
/// summary>
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.
/// 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)
{
// 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);
}
}
}
Flashelf October 31, 2004 16:25:21
'/ ********************************************************** ******************
'Windows control restricts the basic method of users (.NET) for VB.NET
'C # articles
'Http://blog.9cbs.net/flashelf/archive/
2004/10/31
/ 161024.aspx
'
'------------------------------------- ------------------
'This code demonstrates the basic way to control the user's input (shielded non-numeric character input)
'.NET limit user input, see many people doing in the keyboard, or Textbox's textchanged event
'Personally think that is incorrect,
'1. Cannot restrict users' paste
'2. Serious interference data binding operation
'3. Sometimes you need to back up the original data to restore
'The timing of the correct limit input is when Windows message wm_char triggered
'But .NET did not provide event mapping for this message. What should I do?
'Provide two columns of schemes:
'1) inherit the TextBox to rewrite the WndProc function (the advantages of the advantage of 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 characters entered by the user.
'// If it is a restricted character directly Return
'// Don't leave Base.WndProc (Ref M);
'}
'IF (M.MSG == WM_Paste)
'{
'// Determine if the data of the clipboard is in line with the requirements if it does not do anything
'// Otherwise, Return will do not go to hand over it'}
'Base.wndproc (Ref M);
'2) Processing the function of the default processing message using the API setWindowlong replacement
'This article is written, demonstrate how to declare the API and this method can be used.
'But if there are multiple controls that need to be restricted in the program and do a universal class library
'Use recommended usage scheme one
'Non-talk, I'm talking about the code.
'********************************************************** ****************** /
Imports system.Runtime.InteropServices
Imports System.Text.RegularExpressions
Public Class Form1
Inherits System.Windows.Forms.form
'Statement a commission
Public Delegate Function NewWndProc (Byval Hwnd As INTPTR, BYVAL WPARAM AS INTPTR, BYVAL LPARAM AS INTPTR) AS INTPTR
'Declaration N API
Public Shared Function CallWindowProc (Byval WndProc As INTPTR, BYVAL MSG AS INTPTR, BYVAL WPARAM AS INTPTR, BYVAL LPARAM AS INTPTR) AS INTPTR
END FUNCTION
Public Shared Function SetWindowl (Byval Nindex As INTEGER, BYVAL WNDPROC AS NEWNDPROC) AS INTPTR
END FUNCTION
Public Shared Function SetWindowlong (Byval Nindex As INTEGER, BYVAL DWNEWLONG AS INTPTR) AS INTPTR
END FUNCTION
'Constant to use
Public const GWL_WNDPROC AS INTEGER = -4
'Enter a character message (if the keyboard is entered, the input method is input, it is not this message)
Public const wm_char as integer = 258
'Right-click menu message
Public const wm_contextMenu as integer = 123
'Paste message
Public const WM_PASTE AS INTEGER = 770
Private WPR AS NewWndProc
Private OldWndProc as INTPTR
#Region "Windows Form Designer Generated Code"
Public Sub New ()
Mybase.new ()
'This call is required for the Windows Form Designer.
InitializationComponent () 'Add any initialization after INITIALIZEComponent () call
End Sub
'Form rewriting Dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (Byval Disposing as Boolean)
IF Disposing then
IF not (Components Is Nothing) THEN
Components.dispose ()
END IF
END IF
Mybase.dispose (Disposing)
End Sub
'Windows Form Designer
Private Components as System.comPonentModel.icontainer
'Note: The following process is necessary for the Windows Form Designer.
'You can modify this process using the Windows Form Designer.
'Don't modify it using the code editor.
Friend Withevents TextBox1 As System.Windows.Forms.TextBox
Me.TextBox1 = new system.windows.Forms.TextBox
Me.suspendlayout ()
'
'TextBox1
'
Me.TextBox1.Location = new system.drawing.point (24, 16)
Me.TextBox1.name = "textbox1"
Me.TextBox1.tabindex = 0
Me.TextBox1.text = ""
Me.TextBox1.textAlign = System.Windows.Forms.horizontalalignment.Right
'
'Form1
'
Me.autoscalebasesize = new system.drawing.size (6, 14)
Me.ClientSize = new system.drawing.size (160, 53)
Me.Controls.add (Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (false)
End Sub
#End region
Private function textBoxWndProc (Byval Hwnd As INTPTR, BYVAL MSG AS INTEGER, BYVAL WPARAM AS INTPTR, BYVAL LPARAM AS INTPTR) AS INTPTR
DIM RETURNVAR AS INTPTR = INTPTR.ZERO
SELECT CASE MSG
Case WM_CHAR
DIM keychar as integer = wparam.toint32 ()
Dim Charok as boolean = (Keychar> 47 Andalso Keychar <58) _
Oralse Keychar = 8 Oralse_
Keychar = 3 oralse keychar = 22 Oracse Keychar = 24
'If the character wparam is not required to change to character 0
'Return (INTPTR) 0; but not there is no keyboard tone for the input
IF not charok then
WPARAM = INTPTR.ZERO
END IF
EXIT SELECT
'Paste message includes Ctrl V or right-key menu paste
Case WM_PASTE
'Take a shear
Dim obj1 as idataObject = clipboard.getdataObject
'Judgment is Text
IF Obj1.getdataPresent (DataFormats.Text) THEN
DIM text1 as string = ctype (obj1.getdata (dataformats.text), string)
'If you need a positive or negative, you must first judge the location of the cursor on Textbox.
'If the cursor can be used in front, ^ ((/ | -) / d)? / D *) $
'WM_CHAR below should also do corresponding changes
'
'If it is a number (you can paste the jump out)
IF regex.ismatch (text1, "^ (/ d {1,}) $") THEN
EXIT SELECT
END IF
END IF
Return INTPTR.ZERO
End SELECT
Returnvar = CallWindowProc (OldWndProc, HWND, MSG, WPARAM, LPARAM)
Return Returnvar
END FUNCTION
Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.Load
'// Lamined commission (here is the callback function)
WPR = New NewWndProc (Addressof Me.TextBoxWndProc)
'// Replace the default processing function of the control (and return the original default handler, is a function pointer geology)
OldWndProc = setWindowlong (TextBox1.Handle, GWL_WNDPROC, WPR)
End Sub
Private sub form1_closed (Byval e as system.eventargs) handles mybase.closed
'Restore default handler
IF not oldwndproc.equals (intptr.zero) THEN
Setwindowlong (TextBox1.Handle, GWL_WndProc, OldWndProc)
END IF
End Sub
END CLASS
Flashelf October 31, 2004 18:00:19
Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.
Published on October 31, 2004 4:18 PM
comment
#
Reply: Windows control limits the basic method of the user entered (.NET)
2004-10-31 6:07 PM
Flashelf
If you reprint, please notify you
#
Reply: Windows control limits the basic method of the user entered (.NET)
2004-11-01 10:35 AM
HBXTLHX
Take a look, try first, huh, huh
#
Reply: Windows control limits the basic method of the user entered (.NET)
2004-11-01 10:50 AM
Haining
Private INTPTR TEXTBOXWNDPROC (INTPTR HWND, INT MSG, INTPTR WPARAM, INTPTR LPARAM)
Yes, the previous sentence is garbled
#
Reply: Windows control limits the basic law gate (.NET article) of the user entered (.NET) 2004-11-01 10:59 AM
AHBIAN
Yes, I tested it.
Original posted: http://blog.9cbs.net/flashelf/archive/2004/10/31/161024.aspx