本帖最后由 imutraveler 于 2017-2-12 11:43 编辑
在代码中的279行,在switch右键菜单的 编辑按钮 事件中,引用了函数TreeEdit,而TreeEdit函数创建了已个GUI,这个GUI中使用了消息模式While 1 循环
函数被正常按钮引用的时候,就是正常的,,例如程序界面的上的“添加主机”按钮。
但,,在WM_Notify中的Switch中引用,程序会假死。
#NoTrayIcon
#Region ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#AccAu3Wrapper_OutFile=Console.exe
#AccAu3Wrapper_UseUpx=Y
#AccAu3Wrapper_UseX64=n
#AccAu3Wrapper_Res_Comment=
#AccAu3Wrapper_Res_Description=远程集中管理
#AccAu3Wrapper_Res_Fileversion=1.0
#AccAu3Wrapper_Res_LegalCopyright=
#AccAu3Wrapper_Res_Language=2052
#AccAu3Wrapper_Res_requestedExecutionLevel=None
#EndRegion ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <GuiImageList.au3>
#include <GuiButton.au3>
#include <String.au3>
#include <WinAPI.au3>
#include <GuiMenu.au3>
;~ #include <Array.au3>
#include <Constants.au3> ; 托盘事件$TRAY_EVENT_PRIMARYDOUBLE
;~ Opt("GUIOnEventMode", 1) ;事件模式
Opt("GUICloseOnESC", 0) ;禁用 Esc 键退出程序
Opt("TrayMenuMode", 3) ;不显示默认托盘菜单项“脚本暂停/退出”
Opt("TrayOnEventMode", 1) ;响应托盘事件
Opt("TrayIconHide", 1) ;隐藏脚本自身托盘图标
Opt("WinDetectHiddenText", 1) ;检测隐藏窗口的文本或窗口的隐藏文本
Opt("WinTitleMatchMode", 4) ;窗口的匹配模式
;定义变量
Global $DbPath, $DbPass, $TableGroup, $TableServer, $TableUsers, $Conn
Local $Title = "远程集中管理 (控制台) " ,$OemInfo
$OemInfo = ""
;设置主程序窗口标题
If $OemInfo <> "" Then $Title &= " -- " & $OemInfo
;======================== ;;; 开始:创建程序主窗口 ;;; ========================
$Form1 = GUICreate($Title, 960, 615)
;创建 工具栏
$MenuItem1 = GUICtrlCreateMenu("参数设置")
$MenuItem101 = GUICtrlCreateMenuItem("Mstsc 默认选项", $MenuItem1)
$MenuItem102 = GUICtrlCreateMenuItem("功能02", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("管理功能")
$MenuItem103 = GUICtrlCreateMenuItem("用户管理", $MenuItem2)
$MenuItem104 = GUICtrlCreateMenuItem("OEM信息", $MenuItem2)
$MenuItem3 = GUICtrlCreateMenu("关于程序")
;创建 组框:"搜索"
$SearchGroup = GUICtrlCreateGroup("", 10, 2, 215, 44)
$SearchText = GUICtrlCreateInput("", 21, 14, 120, 25) ;创建搜索文本框
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
$SearchBtn = GUICtrlCreateButton("查找", 145, 13, 70, 27) ;创建查找按钮
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
;创建 组框:"连接按钮"
$BtnGroup = GUICtrlCreateGroup("", 230, 2, 720, 44)
$AddHostBtn = GUICtrlCreateButton("添加主机", 240, 13, 100, 27) ;"添加主机"按钮
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
$AddGroupBtn = GUICtrlCreateButton("添加分组", 350, 13, 100, 27) ;"添加分组"按钮
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
$EditHostBtn = GUICtrlCreateButton("修改主机", 460, 13, 100, 27) ;"修改主机"按钮
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
GUICtrlSetState(-1, $GUI_DISABLE)
$DelHostBtn = GUICtrlCreateButton("删除主机", 570, 13, 100, 27) ;"删除主机"按钮
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
GUICtrlSetState(-1, $GUI_DISABLE)
;创建 TreeView
$TreeView = GUICtrlCreateTreeView(10, 50, 215, 508, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 110)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 131)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 165)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 168)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 137)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 146)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 4)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 45)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 39)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 40)
_GUICtrlTreeView_SetNormalImageList($TreeView, $hImage)
$Group = _GUICtrlTreeView_Add($TreeView, 0, "服务器分组列表", 0, 0)
_GUICtrlTreeView_SetBold($TreeView, $Group)
;~ _GUICtrlTreeView_SetTextColor(-1, 0xff0000)
;创建 TreeView 右键菜单
Local Enum $TreeEdit = 1000, $TreeDelete
Local $TreeContext = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($TreeContext, 0, "修改分组", $TreeEdit)
_GUICtrlMenu_InsertMenuItem($TreeContext, 1, "")
_GUICtrlMenu_InsertMenuItem($TreeContext, 2, "删除分组", $TreeDelete)
;创建 ListView
$ListView = GUICtrlCreateListView("", 230, 50, 720, 310)
_GUICtrlListView_AddColumn($ListView, "名称", 130) ;创建列
_GUICtrlListView_AddColumn($ListView, "隶属分组", 140) ;创建列
_GUICtrlListView_AddColumn($ListView, "IP", 120) ;创建列
_GUICtrlListView_AddColumn($ListView, "端口", 60) ;创建列
_GUICtrlListView_AddColumn($ListView, "备注", 246) ;创建列
_GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES)) ;设置ListView样式
_GUICtrlListView_SetImageList($ListView, $hImage, 1)
;~ GUICtrlSetColor($ListView , 0xff7f24)
;创建 组框:"手动运行"
$SoftGroup = GUICtrlCreateGroup(" 手动运行 ", 230, 365, 355, 194)
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
;创建 组框:"Mstsc 选项"
$MstscGroup = GUICtrlCreateGroup(" Mstsc 连接选项 ", 590, 365, 360, 97)
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
GUICtrlSetColor($MstscGroup, 0xff7f24)
$MstscRes1024 = GUICtrlCreateRadio(" 1024 x 768", 630, 405, 100, 20)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
$MstscResFull = GUICtrlCreateRadio(" 全屏", 630, 425, 100, 20)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
$MstscResConsole = GUICtrlCreateCheckbox(" 控制台", 750, 385, 120, 25)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
$MstscResFull = GUICtrlCreateCheckbox(" 连接磁盘", 750, 420, 120, 25)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
;创建 组框:"Radmin 中转"
$RadminGroup = GUICtrlCreateGroup(" Radmin 中转 ", 590, 462, 360, 97)
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
;创建 状态栏
$StatusBar = _GUICtrlStatusBar_Create($Form1)
Dim $StatusBar_PartsWidth[5] = [140, 280, 480, 750]
_GUICtrlStatusBar_SetParts($StatusBar, $StatusBar_PartsWidth)
_GUICtrlStatusBar_SetText($StatusBar, " 总计分组: 组", 0)
$GroupCountLabel = GUICtrlCreateLabel("0", 70, 574, 28, 16, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x1c86ee)
_GUICtrlStatusBar_SetText($StatusBar, " 主机数量: 台", 1)
$HostCountLabel = GUICtrlCreateLabel("0", 210, 574, 28, 16, $SS_RIGHT)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x1c86ee)
_GUICtrlStatusBar_SetText($StatusBar, " 当前用户:", 2)
$CurrentUserLabel = GUICtrlCreateLabel("wangzhongheng123", 350, 574, 120, 16)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
GUICtrlSetColor(-1, 0x1c86ee)
_GUICtrlStatusBar_SetText($StatusBar, " "& $OemInfo, 3)
_GUICtrlStatusBar_SetText($StatusBar, " By XXX. 版本:V1.0", 4)
_GUICtrlStatusBar_SetMinHeight($StatusBar, 25)
GUISetState(@SW_SHOW)
;======================== ;;; 结束:创建程序主窗口 ;;; ========================
;======================== ;;; 开始:创建托盘菜单、注册托盘事件 ;;; ========================
;创建 托盘菜单
$TrayAbout = TrayCreateItem("关于程序")
TrayCreateItem("") ;菜单分隔条
$TrayView = TrayCreateItem("显示主窗口")
TrayCreateItem("") ;菜单分隔条
$TrayExit = TrayCreateItem("退出")
;注册 鼠标事件
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
;注册 托盘菜单事件
TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "TrayEvent") ;注册托盘图标双击事件
TrayItemSetOnEvent($TrayAbout, "TrayEvent") ;注册托盘菜单"关于程序"事件
TrayItemSetOnEvent($TrayView, "TrayEvent") ;注册托盘菜单"显示界面"事件
TrayItemSetOnEvent($TrayExit, "TrayEvent") ;注册托盘菜单"退出"事件
TraySetState(1) ;显示托盘图标
TraySetClick(16) ;释放鼠标右键(显示托盘菜单)
TraySetToolTip("远程集中管理") ;设置托盘图标的提示文本
;======================== ;;; 结束:创建托盘菜单、注册托盘事件 ;;; ========================
Call(ConnectDB) ;连接数据库
;读取 分组列表
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
;~ $RS.Open("Select * From " & $TableGroup & " Order By GroupName")
$RS.Open("Select * From " & $TableGroup)
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
_GUICtrlTreeView_AddChild($TreeView, $Group, $RS.Fields(1).value, 6, 7) ;生成分组列表
$RS.movenext
WEnd
_GUICtrlTreeView_Sort($TreeView) ;排序TreeView项目
GUICtrlSetData($GroupCountLabel, _GUICtrlTreeView_GetCount($TreeView)) ;设置总计分组label
_GUICtrlTreeView_Expand($TreeView, $Group) ;展开列表
_GUICtrlTreeView_SelectItemByIndex($TreeView, $Group, 0) ;启动时默认选择第一个分组
Local $AutoSelected = GUICtrlRead($TreeView, 1)
Global $lText = $AutoSelected
;~ MsgBox(0, "", $test)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
$RS.close
$Conn.Close
Exit
Case $MenuItem102
MsgBox(0, "", "这个是功能2")
Case $AutoSelected ;启动时候生成第一分组的Listview
Call("ShowItems")
$AutoSelected = Null
Case $AddHostBtn
Call("treeedit")
EndSwitch
WEnd
;=============== 函数 - 定义 ===============
Func ConnectDB() ;连接数据库
$DbPath = @ScriptDir & "\" & "Data.mdb"
$DbPass = "123"
$TableGroup = "TB_Group"
$TableServer = "TB_Server"
$TableUsers = "TB_Users"
$Conn = ObjCreate("ADODB.Connection")
$Conn.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $DbPath & "; Jet OLEDB:Database Password=" & $DbPass)
EndFunc ;==>ConnectDB
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
Local $hWndListView = $ListView,$hWndTreeView = $TreeView
If Not IsHWnd($ListView) Then $hWndListView = GUICtrlGetHandle($ListView)
If Not IsHWnd($TreeView) Then $hWndTreeView = GUICtrlGetHandle($TreeView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeView
Switch $iCode
Case $NM_CLICK ; 鼠标左键单击
Local $mPos = _WinAPI_GetMousePos(True, $hWndTreeView)
Local $mText = _GUICtrlTreeView_HitTestItem($hWndTreeView, DllStructGetData($mPos, "X"), DllStructGetData($mPos, "Y"))
Local $iPos = _GUICtrlTreeView_HitTest($hWndTreeView, DllStructGetData($mPos, "X"), DllStructGetData($mPos, "Y"))
Local $cText = _GUICtrlTreeView_GetText($hWndTreeView, $mText) ;获得当前坐标项目文本
Local $gText = _GUICtrlTreeView_GetText($hWndTreeView, $Group) ;获得分组标题文本
If $cText <> $gText Then ;判断是否点击了分组标题,若是,则不响应
If ($iPos = 2) Or ($iPos = 4) Or ($iPos = 64) Then ;判断鼠标是否在图标或文本上,若否,则不响应
If $lText <> $cText Then ;判断是否点重复点击,若是,则不响应
$lText = $cText
_GUICtrlTreeView_SelectItem($hWndTreeView, $mText)
Call("ShowItems")
EndIf
EndIf
EndIf
Case $NM_DBLCLK ; 鼠标左键双击
;~ MsgBox(0, "", "双击了<左键>")
Case $NM_RCLICK ; 鼠标右键单击
Local $mPos = _WinAPI_GetMousePos(True, $hWndTreeView)
Local $mText = _GUICtrlTreeView_HitTestItem($hWndTreeView, DllStructGetData($mPos, "X"), DllStructGetData($mPos, "Y"))
Local $iPos = _GUICtrlTreeView_HitTest($hWndTreeView, DllStructGetData($mPos, "X"), DllStructGetData($mPos, "Y"))
Local $cText = _GUICtrlTreeView_GetText($hWndTreeView, $mText) ;获得当前坐标项目文本
Local $gText = _GUICtrlTreeView_GetText($hWndTreeView, $Group) ;获得分组标题文本
If $cText <> $gText Then ;判断是否点击了分组标题,若是,则不响应
If ($iPos = 2) Or ($iPos = 4) Or ($iPos = 64) Then ;判断鼠标是否在图标或文本上,若否,则不响应
If $lText <> $cText Then ;判断是否点重复点击,若是,则不响应
$lText = $cText
_GUICtrlTreeView_SelectItem($hWndTreeView, $mText)
Call("ShowItems")
EndIf
Switch _GUICtrlMenu_TrackPopupMenu($TreeContext, $hWndFrom, -1, -1, 1, 1, 2)
Case $TreeEdit
Call("TreeEdit")
Case $TreeDelete
;~ Call("TreeDelete")
EndSwitch
EndIf
EndIf
EndSwitch
Case $hWndListView
Switch $iCode
Case $NM_CLICK ; 鼠标左键单击
$index = _GUICtrlListView_GetSelectedIndices($ListView)
If $index <> "" Then
;~ MsgBox(0, "", "单击了<左键>")
GUICtrlSetState($EditHostBtn, $GUI_ENABLE)
GUICtrlSetState($DelHostBtn, $GUI_ENABLE)
Else
GUICtrlSetState($EditHostBtn, $GUI_DISABLE)
GUICtrlSetState($DelHostBtn, $GUI_DISABLE)
EndIf
Case $NM_DBLCLK ; 鼠标左键双击
MsgBox(0, "", "双击了<左键>")
Case $NM_RCLICK ; 鼠标右键单击
MsgBox(0, "", _GUICtrlListView_GetSelectedIndices($ListView))
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func ShowItems() ;生成主机列表
_GUICtrlListView_DeleteAllItems($ListView)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
$RS.open("SELECT * FROM " & $TableServer & " WHERE Groups='" & GUICtrlRead($TreeView, 1) & "'")
Local $i = 0
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
_GUICtrlListView_AddItem($ListView, $RS.Fields(1).value, 4)
_GUICtrlListView_AddSubItem($ListView, $i, $RS.Fields(7).value, 1)
_GUICtrlListView_AddSubItem($ListView, $i, $RS.Fields(2).value, 2)
_GUICtrlListView_AddSubItem($ListView, $i, $RS.Fields(3).value, 3)
_GUICtrlListView_AddSubItem($ListView, $i, $RS.Fields(9).value, 4)
$i += 1
$RS.movenext
WEnd
GUICtrlSetData($HostCountLabel, _GUICtrlListView_GetItemCount($ListView)) ;设置总计主机label
EndFunc ;==>ShowItems
Func TreeEdit() ;修改分组GUI
$TreeEditGUI = GUICreate("修改分组", 300, 100)
$nLabel = GUICtrlCreateLabel("分组名称:" , 50, 23, 60, 25)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
$nInput = GUICtrlCreateInput(GUICtrlRead($TreeView, 1), 110, 20, 140, 25)
GUICtrlSetFont(-1, 9, 400, 0, "微软雅黑")
$Btn1 = GUICtrlCreateButton("确定", 90, 65, 70, 27)
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
$Btn2 = GUICtrlCreateButton("取消", 180, 65, 70, 27)
GUICtrlSetFont(-1, 10, 400, 0, "微软雅黑")
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE, $Btn2
GuiDelete($TreeEditGUI)
WinActivate($Form1, "")
ExitLoop
Case $Btn1
EndSwitch
WEnd
EndFunc ;==>TreeEdit
Func TrayEvent() ;托盘事件
Switch @TRAY_ID
Case $TrayAbout
Call("About")
Case $TrayView, $TRAY_EVENT_PRIMARYDOUBLE
GUISetState(@SW_SHOW, $Form1)
GUISetState(@SW_RESTORE, $Form1)
Case $TrayExit
Exit
EndSwitch
EndFunc
Func About()
GUISetState(@SW_RESTORE, $Form1)
GUISetState(@SW_DISABLE ,$Form1)
Msgbox(64, "关于程序"," 远程集中管理(控制台) V1.0 ", 0)
GUISetState(@SW_ENABLE ,$Form1)
GUISetState(@SW_RESTORE, $Form1)
EndFunc
|