h111666b 发表于 2021-9-4 13:10:37

GUI列表框 实时刷新U盘设备

代码如下:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 294, 187, 192, 124)
$Edit1 = GUICtrlCreateCombo("", 16, 16, 265, 113)
$Button1 = GUICtrlCreateButton("获取U盘", 152, 152, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        move()
        EndSwitch
WEnd

Func move()
        $zi = "已连接的设备盘符为 ==> "
        $drive = DriveGetDrive("REMOVABLE");获取可移动驱动器
        If Not @error Then
                Local $daxie = ""
                For $i = 1 To $drive
                        $daxie &= StringUpper($drive[$i]) & Chr(32);将小写盘符转换大写
                Next
                GUICtrlSetData($Edit1, $zi & $daxie);修改指定控件的数据,$Edit2是那个输入框
        Else
                MsgBox(16, "警告!", "未发现有U盘。", "", $Form1);显示一个简单的对话框
        EndIf
EndFunc   ;==>move


现在需要插上U盘不需要按 获取U盘 按钮自动显示U盘到列表框,拨了U盘提示 请插入U盘,再插上又自动显示到列表框,实在是不会弄,求助

zghwelcome 发表于 2021-9-4 13:16:04

应该采用注册消息的方式

sh1536 发表于 2021-9-4 16:58:01

https://www.autoitx.com/thread-55129-1-1.html

h111666b 发表于 2021-9-4 17:34:43

注册消息?,不会弄,给个示例呀

zghwelcome 发表于 2021-9-4 19:41:46

https://autoitx.com/forum.php?mod=viewthread&tid=17747

chzj589 发表于 2021-9-6 09:12:52

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$DBT_DEVICEARRIVAL = 0x8000
$DBT_DEVICEREMOVECOMPLETE = 0x8004
$DBT_DEVTYP_VOLUME = 0x00000002
$DEV_BROADCAST_HDR = "DWORD size;DWORD devicetype;DWORD reserved"
$DEV_BROADCAST_VOLUME = "DWORD size;DWORD devicetype;DWORD reserved;DWORD unitmask;WORD flags"
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 294, 187, 192, 124)
$Edit1 = GUICtrlCreateCombo("", 16, 16, 265, 25)
GUICtrlSetFont(-1, 10, 400, 0, 'Arial')
$Button1 = GUICtrlCreateButton("获取U盘", 152, 152, 75, 25)
move()
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_DEVICECHANGE, "WM_DEVICECHANGE")
#EndRegion ### END Koda GUI section ###
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        move()
        EndSwitch
WEnd
Func WM_DEVICECHANGE($hWnd, $MsgID, $WParam, $LParam)
        Switch $WParam
                Case $DBT_DEVICEARRIVAL, $DBT_DEVICEREMOVECOMPLETE
                        $dbhdr = DllStructCreate($DEV_BROADCAST_HDR, $LParam)
                        $devicetype = DllStructGetData($dbhdr, "devicetype")
                        Switch $devicetype
                                Case $DBT_DEVTYP_VOLUME
                                        $dbv = DllStructCreate($DEV_BROADCAST_VOLUME, $LParam)
                                        $unitmask = DllStructGetData($dbv, "unitmask")
                                        For $i = 0 To 25
                                                If BitAND(BitShift(1, -$i), $unitmask) <> 0 Then move()
                                        Next
                        EndSwitch
        EndSwitch
EndFunc   ;==>WM_DEVICECHANGE
Func move()
        GUICtrlSetData($Edit1, "", "")
        GUICtrlSetColor($Edit1, 0x000000)
        $zi = "已连接的设备盘符为 ==> "
        $drive = DriveGetDrive("REMOVABLE");获取可移动驱动器
        If Not @error Then
                Local $daxie = ""
                For $i = 1 To $drive
                        $daxie &= StringUpper($drive[$i]) & Chr(32);将小写盘符转换大写
                Next
                GUICtrlSetData($Edit1, $zi & $daxie, $zi & $daxie);修改指定控件的数据,$Edit2是那个输入框
                ControlCommand($Form1, "", 'ComboBox1', "SetCurrentSelection", 0)
                GUICtrlSetColor($Edit1, 0x0000FF)
        Else
                MsgBox(16, "警告!", "未发现有U盘。", "", $Form1);显示一个简单的对话框
        EndIf
EndFunc   ;==>move



页: [1]
查看完整版本: GUI列表框 实时刷新U盘设备