#include <WinAPI.au3>
#include <GUIEdit.au3>
#include <WindowsConstants.au3>
$hGUI = GUICreate("Test", 200, 100)
$iIpt = GUICtrlCreateInput("", 20, 20, 160, 20)
$hCtrl = GUICtrlGetHandle(-1)
$hCall = DllCallbackRegister("_IptProc", "int", "hWnd;uint;wparam;lparam")
$pCall = DllCallbackGetPtr($hCall)
$hOldCall = _WinAPI_SetWindowLong($hCtrl, -4, $pCall)
GUISetState()
Do
Until guiGetMsg() = -3
Func _IptProc($hWnd, $iMsg, $wparam, $lparam)
Switch $iMsg
Case $WM_PASTE
Return 1
Case $WM_SETTEXT
Return 1
Case $WM_CHAR
Local $aXY, $sString
If Not StringIsDigit(Chr($wparam)) AND $wparam <> 8 Then Return 0
$aXY = _GetCaretPos()
$aXY = _GUICtrlEdit_CharFromPos($hCtrl, $aXY[0], $aXY[1])
$sString = GUICtrlRead($iIpt)
If $wparam <> 8 Then
$sString = StringLeft($sString, $aXY[0]) & Chr($wparam) & StringRight($sString, StringLen($sString) - $aXY[0])
Else
$sString = StringLeft($sString, $aXY[0] - 1) & StringRight($sString, StringLen($sString) - $aXY[0])
EndIf
If StringLeft($sString, 1) = "0" Then Return 0
If $sString <> "" AND (Number($sString) < 1 OR Number($sString) > 50) Then Return 0
EndSwitch
Return _WinAPI_CallWindowProc($hOldCall, $hWnd, $iMsg, $wparam, $lparam)
EndFunc ;==>_IptProc
Func _GetCaretPos()
Local $aXY[2], $iResult, $tBuffer
$tBuffer = DllStructCreate("int X;int Y")
$iResult = DllCall("User32.dll", "int", "GetCaretPos", "ptr", DllStructGetPtr($tBuffer))
$aXY[0] = DllStructGetData($tBuffer, "X")
$aXY[1] = DllStructGetData($tBuffer, "Y")
$tBuffer = 0
Return $aXY
EndFunc ;==>_GetCaretPos
|