早上看到的帖子,说是限制Input1只能是0-23,Input2只能是0-59
当时看到时我挺有兴趣写了点,后来有事。
刚写了一个我的版本
让大家指正
#include <GUIConstantsEX.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>
#include <GuiEdit.au3>
Opt('MustDeclareVars', 1)
Global $hGui, $msg, $InputA, $InputB
Global $wProcNew, $wProcOld, $hInputA, $hInputB
Global $iTipIcon = $TTI_ERROR
Global $sTipTitle = "Unacceptable Character"
Global $sTipTextA = "You can only enter numbers 0 to 23 here."
Global $sTipTextB = "You can only enter numbers 0 to 59 here."
$hGui = GUICreate("Custom inputs, tooltips and cue banners for INPUT CONTROL", 450, 200)
GUICtrlCreateLabel("", -10, -10)
GUICtrlSetState(-1, $GUI_FOCUS)
$InputA = GUICtrlCreateInput("", 10, 10, 400, 30, $ES_NUMBER)
$hInputA = GUICtrlGetHandle(-1)
_GUICtrlEdit_SetCueBanner($hInputA, $sTipTextA)
$InputB = GUICtrlCreateInput("", 10, 80, 400, 30, $ES_NUMBER)
$hInputB = GUICtrlGetHandle(-1)
_GUICtrlEdit_SetCueBanner($hInputB, $sTipTextB)
$wProcNew = DllCallbackRegister("_InputWinProc", "int", "hwnd;uint;wparam;lparam")
$wProcOld = _WinAPI_SetWindowLong($hInputA, $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))
_WinAPI_SetWindowLong($hInputB, $GWL_WNDPROC, DllCallbackGetPtr($wProcNew))
GUISetState()
While True
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then _Exit()
WEnd
Func _GUICtrlEdit_SetCueBanner($hWnd, $sText)
If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
Local $tText = _WinAPI_MultiByteToWideChar($sText)
Return _SendMessage($hWnd, $EM_SETCUEBANNER, False, DllStructGetPtr($tText)) = 1
EndFunc
Func _Exit()
If $wProcOld Then
_WinAPI_SetWindowLong($hInputA, $GWL_WNDPROC, $wProcOld)
_WinAPI_SetWindowLong($hInputB, $GWL_WNDPROC, $wProcOld)
EndIf
If $wProcNew Then DllCallbackFree($wProcNew)
GUIDelete($hGui)
Exit
EndFunc
Func _GuiInputSetToolTip(ByRef $hWnd, ByRef $msg, ByRef $wParam, ByRef $lParam, $sTitle = "", $sText = "", $iIcon = $TTI_NONE)
If Not IsHWnd($hWnd) Then Return 0
Local $tTitle, $tText, $tTT
$tTitle = _WinAPI_MultiByteToWideChar($sTitle)
$tText = _WinAPI_MultiByteToWideChar($sText)
If $lParam And ($msg = $EM_SHOWBALLOONTIP) Then
$tTT = DllStructCreate($tagEDITBALLOONTIP, $lParam)
Else
$tTT = DllStructCreate($tagEDITBALLOONTIP)
DllStructSetData($tTT, "Size", DllStructGetSize($tTT))
EndIf
If Not IsDllStruct($tTitle) Or Not IsDllStruct($tText) Or Not IsDllStruct($tTT) Then
Return _WinAPI_CallWindowProc($wProcOld, $hWnd, $msg, $wParam, $lParam)
EndIf
DllStructSetData($tTT, "Title", DllStructGetPtr($tTitle))
DllStructSetData($tTT, "Text", DllStructGetPtr($tText))
DllStructSetData($tTT, "Icon", $iIcon)
If ($msg <> $EM_SHOWBALLOONTIP) Or (Not $lParam) Then $lParam = DllStructGetPtr($tTT)
Return _WinAPI_CallWindowProc($wProcOld, $hWnd, $EM_SHOWBALLOONTIP, $wParam, $lParam)
EndFunc
Func _InputWinProc($hWnd, $msg, $wParam, $lParam)
#forceref $hWnd, $Msg, $wParam, $lParam
Local $iStrLen, $iNum
Switch $msg
Case $EM_SHOWBALLOONTIP
Switch $hWnd
Case $hInputA
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextA, $iTipIcon)
Case $hInputB
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextB, $iTipIcon)
EndSwitch
Case $WM_CHAR
If $wParam = 7 Or $wParam = 8 Then ContinueCase
Switch $hWnd
Case $hInputA
$iStrLen = StringLen(GUICtrlRead($InputA))
If $iStrLen = 0 Then
ContinueCase
ElseIf $iStrLen = 1 Then
If GUICtrlRead($InputA) == 0 Then
$wParam = 0
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextA, $iTipIcon)
EndIf
$iNum = Number(GUICtrlRead($InputA) & Chr($wParam))
If $iNum > 23 Then
$wParam = 0
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextA, $iTipIcon)
EndIf
ElseIf $iStrLen = 2 Then
$wParam = 0
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextA, $iTipIcon)
EndIf
Case $hInputB
$iStrLen = StringLen(GUICtrlRead($InputB))
If $iStrLen = 0 Then
ContinueCase
ElseIf $iStrLen = 1 Then
If GUICtrlRead($InputB) == 0 Then
$wParam = 0
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextB, $iTipIcon)
EndIf
$iNum = Number(GUICtrlRead($InputB) & Chr($wParam))
If $iNum > 59 Then
$wParam = 0
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextB, $iTipIcon)
EndIf
ElseIf $iStrLen = 2 Then
$wParam = 0
Return _GuiInputSetToolTip($hWnd, $msg, $wParam, $lParam, $sTipTitle, $sTipTextB, $iTipIcon)
EndIf
EndSwitch
EndSwitch
Return _WinAPI_CallWindowProc($wProcOld, $hWnd, $msg, $wParam, $lParam)
EndFunc
|