找回密码
 加入
搜索
查看: 1470|回复: 5

[系统综合] GUI列表框 实时刷新U盘设备

[复制链接]
发表于 2021-9-4 13:10:37 | 显示全部楼层 |阅读模式
代码如下:

#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[0]
                        $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盘,再插上又自动显示到列表框,实在是不会弄,求助

发表于 2021-9-4 13:16:04 | 显示全部楼层
应该采用注册消息的方式
发表于 2021-9-4 16:58:01 | 显示全部楼层
 楼主| 发表于 2021-9-4 17:34:43 | 显示全部楼层
注册消息?,不会弄,给个示例呀
发表于 2021-9-4 19:41:46 | 显示全部楼层
发表于 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[0]
                        $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

您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-12-22 22:04 , Processed in 0.244728 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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