找回密码
 加入
搜索
查看: 1636|回复: 10

如何检测GUI中的Button按下与放开

[复制链接]
发表于 2009-9-9 07:21:19 | 显示全部楼层 |阅读模式
本帖最后由 lrbin50 于 2009-9-9 16:19 编辑

如题,但这不是点击,我想在鼠标按下这个按钮时执行一段代码,而放开时又执行另一段代码

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 227, 59, 192, 124)
$Button1 = GUICtrlCreateButton("确定", 16, 16, 75, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("取消", 112, 16, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        MsgBox (0,"","你点击了【确定】按钮")
                Case $Button2        
                        MsgBox (0,"","你点击了【取消】按钮")
        EndSwitch
WEnd
这样的是不符合我的要求的!
请大家指教,谢谢!
发表于 2009-9-9 07:45:58 | 显示全部楼层
本帖最后由 netegg 于 2009-9-9 07:47 编辑

看看关于$WM_Notify,可能有帮助,那个在列表框和树形列表里用的挺多的,不过不好意思,我没搞得太明白
或者去官网找找看关于buttonhover,记忆里好象有
发表于 2009-9-9 09:19:27 | 显示全部楼层
本帖最后由 水木子 于 2009-9-9 09:28 编辑

等待高手来解答吧!帮你顶起。
发表于 2009-9-9 11:47:48 | 显示全部楼层
本帖最后由 zjimmy 于 2009-9-9 11:49 编辑

我利用UDF搞成了这段代码,方法比较“土”,可能不够高级,希望有所帮助。
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>

Global $PreBtnDown

$Form1 = GUICreate("Form1", 227, 59, 192, 124)
$Button1 = GUICtrlCreateButton("确定", 16, 16, 75, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("取消", 112, 16, 75, 25, $WS_GROUP)

GUISetState(@SW_SHOW)

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $GUI_EVENT_PRIMARYDOWN
                        Select
                                Case _ButtonIsDown($Button1, $PreBtnDown)
                                        MsgBox(0, "", "你按下了【确定】按钮")
                                Case _ButtonIsDown($Button2, $PreBtnDown)
                                        MsgBox(0, "", "你按下了【取消】按钮")
                        EndSelect
                Case $GUI_EVENT_PRIMARYUP
                        Select
                                Case _ButtonIsRelease($Button1, $PreBtnDown)
                                        MsgBox(0, "", "你松开了【确定】按钮")
                                Case _ButtonIsRelease($Button2, $PreBtnDown)
                                        MsgBox(0, "", "你松开了【取消】按钮")
                        EndSelect
        EndSwitch
WEnd

Func _ButtonIsDown($hBtn, ByRef $hPreBtnDown)
        If BitAND(_GUICtrlButton_GetState($hBtn), $BST_PUSHED) Then
                $hPreBtnDown = $hBtn
                Return 1
        Else
                Return 0
        EndIf
EndFunc   ;==>_ButtonIsDown

Func _ButtonIsRelease($hBtn, ByRef $hPreBtnDown)
        If $hBtn <> $hPreBtnDown Then Return 0
        If BitAND(_GUICtrlButton_GetState($hBtn), $BST_PUSHED) Then
                $hPreBtnDown = $hBtn
                Return 0
        Else
                Return 1
        EndIf
EndFunc   ;==>_ButtonIsRelease
 楼主| 发表于 2009-9-9 12:48:24 | 显示全部楼层
谢了,zjimmy ,不过有个问题,就是拖动窗口也说你松开了按钮。看一下还有没有更多的答案。
发表于 2009-9-9 13:04:27 | 显示全部楼层
谢了,zjimmy ,不过有个问题,就是拖动窗口也说你松开了按钮。看一下还有没有更多的答案。
lrbin50 发表于 2009-9-9 12:48


不谢,还没有解决问题呢。

嗯,这的确是个问题,大概“松开按钮”的判断时机选择不对,我再琢磨琢磨看有没有解决办法。

期待高手有比较高级的解决方法!
发表于 2009-9-9 13:06:25 | 显示全部楼层
Msgbox(xxx) 最好改成 ToolTip("你xxx按钮") 呵呵
发表于 2009-9-9 14:14:05 | 显示全部楼层
本帖最后由 afan 于 2009-9-9 14:19 编辑

修改了下LS的代码:
_ButtonIsRelease 函数修改了下,只会响应按钮;
提示改为工具提示。
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>

Dim $PreBtnDown
GUICreate("鼠标左键状态", 255, 60)
$Button1 = GUICtrlCreateButton("确定", 10, 16, 75, 25)
$Button2 = GUICtrlCreateButton("取消", 90, 16, 75, 25)
$Button3 = GUICtrlCreateButton("退出", 170, 16, 75, 25)
GUISetState()

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE, $Button3
                        Exit
                Case $GUI_EVENT_PRIMARYDOWN
                        Select
                                Case _ButtonIsDown($Button1, $PreBtnDown)
                                        ToolTip("您按下了【确定】按钮")
                                Case _ButtonIsDown($Button2, $PreBtnDown)
                                        ToolTip("您按下了【取消】按钮")
                        EndSelect
                Case $GUI_EVENT_PRIMARYUP
                        Select
                                Case _ButtonIsRelease($Button1, $PreBtnDown)
                                        ToolTip("您松开了【确定】按钮")
                                Case _ButtonIsRelease($Button2, $PreBtnDown)
                                        ToolTip("您松开了【取消】按钮")
                                Case Else
                                        ToolTip("")
                        EndSelect
        EndSwitch
WEnd

Func _ButtonIsDown($hBtn, ByRef $hPreBtnDown)
        If BitAND(_GUICtrlButton_GetState($hBtn), $BST_PUSHED) Then
                $hPreBtnDown = $hBtn
                Return 1
        Else
                Return 0
        EndIf
EndFunc   ;==>_ButtonIsDown

Func _ButtonIsRelease($hBtn, ByRef $hPreBtnDown)
        If $hBtn <> $hPreBtnDown Then Return 0
        If BitAND(_GUICtrlButton_GetState($hBtn), $BST_PUSHED) Then
                $hPreBtnDown = $hBtn
                Return 0
        Else
                $hPreBtnDown = 0
                Return 1
        EndIf
EndFunc   ;==>_ButtonIsRelease
 楼主| 发表于 2009-9-9 15:36:01 | 显示全部楼层
感谢AFAN!你这个代码有效!
发表于 2009-9-9 15:41:32 | 显示全部楼层
一句“$hPreBtnDown = 0”,差之毫厘,谬以千里呀~~

受教了!
发表于 2009-9-9 15:49:42 | 显示全部楼层
呵呵,代码都是LS滴,我只小小改动而已~~
我是新手,还要向各位多多学习~~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-5 13:28 , Processed in 0.074801 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表