如何让消息响应多个控件(已解决)
本帖最后由 gzh888666 于 2012-12-6 04:30 编辑最近学习WIN消息,下面的代码可以响应单个控件,我试了很多种方法无法响应多个控件,还是没有弄明白消息工作的原理,特来求助!#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>
GUICreate("Form1", 400, 300)
$Date1 = GUICtrlCreateDate("", 100, 10, 91, 28)
$Date2 = GUICtrlCreateDate("", 100, 110, 91, 28)
$Date3 = GUICtrlCreateDate("", 100, 210, 91, 28)
$Button1 = GUICtrlCreateButton("E", 250, 100, 100, 30)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1
MsgBox(0, '', '相应键')
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndDate1, $tInfo
If Not IsHWnd($Date1) Then $hWndDate1 = GUICtrlGetHandle($Date1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndDate1
Switch $iCode
Case $NM_KILLFOCUS ; 控件失去输入焦点
_Time($Date1)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _Time($ID);定义时间比较函数
$Time_Array = StringRegExp(GUICtrlRead($ID), "\d+", 3)
$Time = $Time_Array & "/" & $Time_Array & "/" & $Time_Array
$iDateCalc = _DateDiff("D", $Time, _NowCalc())
If $iDateCalc > 0 Then Return 1 ;如果控件时间小于现在时间返回1
If $iDateCalc = 0 Then Return 0; 如果控件时间等于现在时间返回0
If $iDateCalc < 0 Then
MsgBox(262144,"提示","选择日期不能超过当前日期,请重新输入!")
GUICtrlSetData($ID,@YEAR&"年"&@MON&"月"&@MDAY&"日")
GUICtrlSetState ($ID,$GUI_FOCUS)
Return -1 ;如果控件时间大于现在时间返回-1
EndIf
EndFunc ;==>_Time 有id有句柄,判断这两个就行 本帖最后由 gzh888666 于 2012-12-6 04:15 编辑
回复 2# 风行者
现在我在不同的TAB页中测试还是不成功!
我就是不知道怎么来判断多个,麻烦兄弟给个例子,谢谢了! 本帖最后由 gzh888666 于 2012-12-6 04:17 编辑
现在我在不同的TAB页中测试还是不成功! #include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>
GUICreate("Form1", 400, 300)
$Date1 = GUICtrlCreateDate("", 100, 10, 91, 28)
$Date2 = GUICtrlCreateDate("", 100, 110, 91, 28)
$Date3 = GUICtrlCreateDate("", 100, 210, 91, 28)
$Button1 = GUICtrlCreateButton("E", 250, 100, 100, 30)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $Button1
MsgBox(0, '', '相应键')
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndDate1, $tInfo
If Not IsHWnd($Date1) Then $hWndDate1 = GUICtrlGetHandle($Date1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $iCode
Case $NM_KILLFOCUS
Switch $iIDFrom
Case $Date1 To $Date3 ; 控件失去输入焦点
_Time($iIDFrom)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _Time($ID);定义时间比较函数
$Time_Array = StringRegExp(GUICtrlRead($ID), "\d+", 3)
$Time = $Time_Array & "/" & $Time_Array & "/" & $Time_Array
$iDateCalc = _DateDiff("D", $Time, _NowCalc())
If $iDateCalc > 0 Then Return 1 ;如果控件时间小于现在时间返回1
If $iDateCalc = 0 Then Return 0; 如果控件时间等于现在时间返回0
If $iDateCalc < 0 Then
MsgBox(262144,"提示","选择日期不能超过当前日期,请重新输入!")
GUICtrlSetData($Date1,@YEAR&"年"&@MON&"月"&@MDAY&"日")
GUICtrlSetState ($Date1,$GUI_FOCUS)
Return -1 ;如果控件时间大于现在时间返回-1
EndIf
EndFunc ;==>_Time 本帖最后由 gzh888666 于 2012-12-6 04:24 编辑
回复 5# 风行者
OK了!谢谢!原来我_Time里写错了!已经改正!真是疏忽! tab页不是用这个消息吧 回复 7# 风行者
OK了!谢谢!原来我_Time里$id写错了!已经改正!真是疏忽!
页:
[1]