找回密码
 加入
搜索
楼主: itzyx

[AU3基础] 【已解决】ObjGet 获取对象的问题

[复制链接]
 楼主| 发表于 2022-3-12 23:38:48 | 显示全部楼层
zghwelcome 发表于 2022-3-12 22:15
我也插个嘴,这个问题处理下com错误就行了

大佬什么意思  麻烦指点一下 万分感谢
发表于 2022-3-13 08:56:53 | 显示全部楼层


#include <GuiListView.au3>
#include <Editconstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ARRAY.au3>


Global $__g_oTemplateCOMErrorHandler = 0
Template_COMErrorRegister()
Global $objWMIService
Global $ListView_HitItem, $Listview_HitItemIndex, $Listview_HitItemSubItem, $ListView_HitItemInput, $Portnames;定义全局变量

Global Const $DBT_DEVICEARRIVAL = 0x8000 ;设备插入
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004 ;设备移除
Global Const $DBT_DEVTYP_PORT = 0x00000003

$Form = GUICreate("端口读取工具", 820, 450)
$Button = GUICtrlCreateButton('OK', 727, 412, 80, 30, -1, $WS_EX_WINDOWEDGE)
GUICtrlCreateLabel("BY-Lay  Version1.0.1", 8, 420, 200, 40)
Local $sFont = "宋体"
GUICtrlSetFont(-1, 10, 100, 2, $sFont)
GUICtrlSetState(-1, 128)
GUISetFont(9, 400, 0, "微软雅黑")
$Combox = GUICtrlCreateCombo('', 620, 415, 80, 30)
GUICtrlSetTip($Combox, '选择已有的快捷方式名称,或者手动粘贴需要打开软件的路径', '提示', 1, 1)
$Check = GUICtrlCreateCheckbox('', 600, 415, 15, 30)
GUICtrlSetTip($Check, '勾选复选框,根据端口数量打开对应软件窗口数量', '提示', 1, 1)
GUICtrlSetData($Combox, 'QGNSS|PowerGPS|TTFF_Tester', 'QGNSS')
$ListView = GUICtrlCreateListView("序列(List)|端口(Port)|模块编号(ModuleId)", 8, 8, 800, 400)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES);扩展风格
$label = GUICtrlCreateLabel("", -100, -100, 1, 1)
GUISetState(@SW_SHOW)
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 1, 500)
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 2, 195)

Local $Ports = ''
$objWMIService = ObjGet("WinMgmts:")
$colBios = $objWMIService.ExecQuery("Select * from  Win32_PNPEntity")
For $object In $colBios
        $Ports &= StringMid($object.Caption, 1) & @CRLF
Next
$Bports = StringRegExp($Ports, '(?m).+\(COM\d+\)$', 3)

If UBound($Bports) < 1 Then
        Sleep(50)
Else
        For $i = 1 To UBound($Bports)
                GUICtrlCreateListViewItem($i & '|' & $Bports[$i - 1] & "|", $ListView)
        Next
EndIf


GUIRegisterMsg($WM_DEVICECHANGE, 'My_Message_Test')
;~ GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
;~ GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
While GUIGetMsg() <> -3
        Sleep(10)
WEnd
Template_COMErrorUnregister()

Func My_Message_Test($hWnd, $Msg, $wParam, $lParam)
        Switch $wParam
                Case $DBT_DEVICEARRIVAL
                        Local $dbhdr = DllStructCreate('DWORD size;DWORD devicetype;DWORD reserved', $lParam)
                        If DllStructGetData($dbhdr, "devicetype") = $DBT_DEVTYP_PORT Then
                        Local $dbport = DllStructCreate('DWORD size;DWORD devicetype;DWORD reserved;wchar name[128]', $lParam)
                        $Portnames = DllStructGetData($dbport, 'name')
                        AdlibRegister('_Refresh', 500)
                        EndIf
        EndSwitch
EndFunc   ;==>My_Message_Test

Func _Refresh()
        $colBios = $objWMIService.ExecQuery("Select * from Win32_SerialPort")
        If @error Then Return SetError(1, 0, '')
        AdlibUnRegister('_Refresh')
        GUICtrlSendMsg($ListView, $LVM_DELETEALLITEMS, 0, 0)
        Local $Ports
        For $object In $colBios
                $Ports &= StringMid($object.Caption, 1) & @CRLF
        Next
        $Bports = StringRegExp($Ports, '(?m).+\(COM\d+\)$', 3)
        If Not @error Then
                For $i = 1 To UBound($Bports)
                        GUICtrlCreateListViewItem($i & '|' & $Bports[$i - 1] & "|", $ListView)
                Next
        EndIf
EndFunc   ;==>_Refresh

Func Template_COMErrorRegister()
        $__g_oTemplateCOMErrorHandler = ObjEvent("AutoIt.Error", "Template_COMErrFunc")
EndFunc   ;==>Template_COMErrorRegister

Func Template_COMErrorUnregister()
        $__g_oTemplateCOMErrorHandler = 0
EndFunc   ;==>Template_COMErrorUnregister

Func Template_COMErrFunc()
EndFunc   ;==>Template_COMErrFunc



评分

参与人数 1金钱 +40 收起 理由
itzyx + 40 感谢大佬,学习到了,我来研究研究

查看全部评分

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

本版积分规则

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

GMT+8, 2024-12-22 17:19 , Processed in 0.160022 second(s), 15 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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