找回密码
 加入
搜索
查看: 1543|回复: 8

[AU3基础] 如何实现一钮复用?[已解决]

[复制链接]
发表于 2019-8-31 01:29:51 | 显示全部楼层 |阅读模式
本帖最后由 cashiba 于 2019-8-31 08:56 编辑

#include <GUIConstantsEx.au3>
Global $hGUI,$idStart
Example()
Func Example()
    $hGUI = GUICreate("TEST", 300, 200)
    $idStart = GUICtrlCreateButton("记事本", 120, 170, 85, 25)
    Local $idClose = GUICtrlCreateButton("关 闭", 210, 170, 85, 25)
    GUISetState(@SW_SHOW, $hGUI)
    Local $iPID = 0
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idClose
                ExitLoop
            Case $idStart
                _ButtonToggle()
        EndSwitch
    WEnd
    GUIDelete($hGUI)
    If $iPID Then ProcessClose($iPID)
EndFunc   ;==>Example
Func _ButtonToggle()
 While 1
  Local $a = GUIGetCursorInfo($hGui)
  If $a[4] = $idStart Then
   if $a[3] = 1 then
     ConsoleWrite("鼠标右键按下" & @CRLF)
     If GUICtrlRead($idStart) = "记事本" Then
      GUICtrlSetData($idStart, "计算器")
     ElseIf GUICtrlSetData($idStart, "计算器") Then
      GUICtrlSetData($idStart, "记事本")
     EndIf
   ElseIf $a[2] = 1 then
     ConsoleWrite("鼠标左键按下" & @CRLF)
     If GUICtrlRead($idStart) = "记事本" Then
      $iPID = Run("notepad.exe", "", @SW_SHOWMAXIMIZED)
     ElseIf GUICtrlRead($idStart) = "计算器" Then
      $iPID = Run("calc.exe", "", @SW_SHOWMAXIMIZED)
     EndIf
   EndIf
  Else
   ExitLoop
  EndIf
  sleep(100)
 WEnd
EndFunc   ;==>_ButtonToggle

如上,想实现一个按钮左右开弓,一键多能:右击切换计算器和记事本,左击打开
按上述写法容易卡壳
想用按钮消息来实现,发现按钮的消息里没有右击
用鼠标事件绑定按钮,不是太熟悉,似乎也不好用
请大神们指点一下....
预谢!


发表于 2019-8-31 01:48:12 | 显示全部楼层
可以开任意个,主界面关闭全退
Global $hGUI, $idStart
Global $aName[2] = ['记事本', '计算器']
Global $aExe[2] = ['notepad.exe', 'calc.exe']
Global $iIx = 0
Global $aPid[100] = [0], $iSL = 0
Example()
Func Example()
        $hGUI = GUICreate('TEST', 300, 200)
        $idStart = GUICtrlCreateButton($aName[$iIx], 120, 170, 85, 25)
        Local $idClose = GUICtrlCreateButton('关 闭', 210, 170, 85, 25)
        GUISetState()
        While 1
                Switch GUIGetMsg()
                        Case -3, $idClose
                                ExitLoop
                        Case $idStart
                                _ButtonToggle()
                        Case -9
                                _Right()
                EndSwitch
        WEnd
        GUIDelete($hGUI)
        If $iSL Then
                For $ii = 1 To $iSL
                        ProcessClose($aPid[$ii])
                Next
        EndIf
EndFunc   ;==>Example
Func _Right()
        Local $a0 = GUIGetCursorInfo($hGUI)
        Local $iId = $a0[4]
        While $a0[3]
                $a0 = GUIGetCursorInfo($hGUI)
        WEnd
        If $a0[4] = $iId Then
                $iIx = Number($iIx = 0)
                GUICtrlSetData($idStart, $aName[$iIx])
        EndIf
EndFunc   ;==>_Right
Func _ButtonToggle()
        $iSL += 1
        $aPid[0] = $iSL
        $aPid[$iSL] = Run($aExe[$iIx], '', @SW_SHOWMAXIMIZED)
EndFunc   ;==>_ButtonToggle

评分

参与人数 1金钱 +20 收起 理由
cashiba + 20 赞一个!

查看全部评分

发表于 2019-8-31 02:00:22 | 显示全部楼层
不错支持一下
 楼主| 发表于 2019-8-31 08:52:51 | 显示全部楼层
A大思维敏捷,精通AU3,一个-9就解决问题了,佩服佩服!
发表于 2019-8-31 09:18:53 | 显示全部楼层
cashiba 发表于 2019-8-31 08:52
A大思维敏捷,精通AU3,一个-9就解决问题了,佩服佩服!

现在是界面任意处点击右键都可以切换,如果限制到按钮可以把35行改为以下
If $a0[4] = $iId And $iId = $idStart Then

评分

参与人数 1金钱 +20 收起 理由
cashiba + 20 赞一个!

查看全部评分

发表于 2019-8-31 12:36:39 | 显示全部楼层
又学了一招,A版威武。
 楼主| 发表于 2019-9-1 11:02:16 | 显示全部楼层
afan 发表于 2019-8-31 09:18
现在是界面任意处点击右键都可以切换,如果限制到按钮可以把35行改为以下

A大心细阿,这样的小问题隔天后还记得修正....
发表于 2019-9-3 11:29:25 | 显示全部楼层
afan 发表于 2019-8-31 01:48
可以开任意个,主界面关闭全退

不错支持一下
发表于 2019-9-4 08:25:35 | 显示全部楼层
afan 发表于 2019-8-31 01:48
可以开任意个,主界面关闭全退

赚钱学习来了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-20 06:10 , Processed in 0.082268 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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