AutoIt上路04-添加标签卡 http://www.gaohaiyan.com/922.html
上一篇博文中,通过原生方法创建了标签卡,并实现了单击时的切换响应。但是按照需求,我们希望在需要的时候打开某个标签,不需要时可以关闭,这是需要用到 UDF(用户自定义函数),可以较好的动态控制标签显示。
首先,原生方法创建出控制标签卡的菜单;对每个菜单项添加单击事件监听;
然后,更新标签卡为使用“用户自定义函数”创建项目-标签;此时标签卡单击事件仍保留;
接着,在菜单项单击事件里控制标签:根据菜单项查找标签,有则删除,无则添加。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#include <GUIConstantsEx.au3> #include <GuiTab.au3> ; 用户自定义函数 #include <GuiToolbar.au3> #include <GuiImageList.au3> #include <WinAPI.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) Global $itemInToolbar $guiWelcome = GUICreate("威格灵", 450, 200) GUISetOnEvent($GUI_EVENT_CLOSE, "Func_GUI_EVENT_CLOSE") $menuFile = GUICtrlCreateMenu ( "文件 &F") $itemOpenInMenuFile = GUICtrlCreateMenuItem("打开", $menuFile) GUICtrlCreateMenuItem("", $menuFile) $itemExitInMenuFile = GUICtrlCreateMenuItem("退出", $menuFile) GUICtrlSetOnEvent($itemExitInMenuFile, "Func_GUI_EVENT_CLOSE") ; 1.添加菜单,控制标签显示和移除 $menuTab = GUICtrlCreateMenu ( "窗口 &W") $itemWorkersInMenuTab = GUICtrlCreateMenuItem("工作日志", $menuTab) GUICtrlSetOnEvent($itemWorkersInMenuTab, "Func_MenuItem_ShowTab") ; 2.单击事件,打开/移除标签卡 $itemProductsInMenuTab = GUICtrlCreateMenuItem("公司产品", $menuTab) GUICtrlSetOnEvent($itemProductsInMenuTab, "Func_MenuItem_ShowTab") $itemSchoolsInMenuTab = GUICtrlCreateMenuItem("学校信息", $menuTab) GUICtrlSetOnEvent($itemSchoolsInMenuTab, "Func_MenuItem_ShowTab") $itemProjectsInMenuTab = GUICtrlCreateMenuItem("工程外包", $menuTab) GUICtrlSetOnEvent($itemProjectsInMenuTab, "Func_MenuItem_ShowTab") $itemAssetsInMenuTab = GUICtrlCreateMenuItem("资产管理", $menuTab) GUICtrlSetOnEvent($itemAssetsInMenuTab, "Func_MenuItem_ShowTab") ; 工具条代码未改动,见《AutoIt上路04-添加标签卡》http://www.gaohaiyan.com/922.html $toolbar = _GUICtrlToolbar_Create($guiWelcome) ; 略 ; 3.更新标签卡为使用“用户自定义函数”创建项目 $tabWelcome = GUICtrlCreateTab ( 1, 28, 448, 130) ; 4.为标签卡添加点击事件处理 GUICtrlSetOnEvent($tabWelcome, "Func_TabWelcome") ;----------------------------------------- 可选 start ---------------------------------------- ; 创建图像列表(ImageList)控件 $imgList = _GUIImageList_Create() ; 添加一或多个图像到图像列表(图像列表控件,调用WinAPI.au3创建单色位图) _GUIImageList_Add($imgList, _WinAPI_CreateSolidBitmap($guiWelcome, 0xFF0000, 16, 16)) _GUIImageList_Add($imgList, _WinAPI_CreateSolidBitmap($guiWelcome, 0x00FF00, 16, 16)) _GUIImageList_Add($imgList, _WinAPI_CreateSolidBitmap($guiWelcome, 0x0000FF, 16, 16)) _GUIImageList_Add($imgList, _WinAPI_CreateSolidBitmap($guiWelcome, 0xFF3399, 16, 16)) _GUIImageList_Add($imgList, _WinAPI_CreateSolidBitmap($guiWelcome, 0x00CCFF, 16, 16)) ;设置(标签选项卡)控件关联的图像列表(标签卡,图像列表) _GUICtrlTab_SetImageList($tabWelcome, $imgList) ; 设置(标签选项卡)的标签图像(标签卡,标签索引-始于0,图像索引-始于0) _GUICtrlTab_SetItemImage($tabWelcome, 0, 0) _GUICtrlTab_SetItemImage($tabWelcome, 1, 1) _GUICtrlTab_SetItemImage($tabWelcome, 2, 2) ;----------------------------------------- 可选 end ------------------------------------------ ; 5.添加默认标签(标题)。起始索引为0 $itemWorkerInTabWelcome = _GUICtrlTab_InsertItem ( $tabWelcome, 0, "工作日志", 0) ; 后于标签卡创建,会在其顶层。注意x、y,否则可能遮挡标签卡 GUICtrlCreateLabel("文本 3", 1, 160, 50, 20) GUISetState(@SW_SHOW, $guiWelcome) GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY") ; 代码未改变 ;-------------------------------- 分割线 ----------------------------- While 1 Sleep(200) WEnd Func Func_GUI_EVENT_CLOSE () Exit EndFunc ; 4.响应标签卡点击事件 Func Func_TabWelcome () $ctrlId = GUICtrlRead (@GUI_CtrlId) ; 获得标签索引。@GUI_CtrlId宏命令,获取刚刚点击的控件ID $itemText = _GUICtrlTab_GetItemText(@GUI_CtrlId, $ctrlId) ; 获得标签标题文本(控件,索引) ConsoleWrite("你点击的标签卡:" & $ctrlId & " - " & $itemText & @CRLF ) EndFunc Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam) ; 未改动,内容略。见《AutoIt上路03-添加工具栏》http://www.gaohaiyan.com/918.html EndFunc ; 2.控制标签卡中标签页的动态显示 Func Func_MenuItem_ShowTab () Switch @GUI_CtrlId ; 菜单栏项目控件ID Case $itemWorkersInMenuTab;~~~~~~~~~~~ 工作日志 Func_Set_MenuItem_Show ("工作日志") Case $itemProductsInMenuTab;~~~~~~~~~~~ 公司产品 Func_Set_MenuItem_Show ("公司产品") Case $itemSchoolsInMenuTab;~~~~~~~~~~~ 学校信息 Func_Set_MenuItem_Show ("学校信息") Case $itemProjectsInMenuTab;~~~~~~~~~~~ 工程外包 Func_Set_MenuItem_Show ("工程外包") Case $itemAssetsInMenuTab;~~~~~~~~~~~ 资产管理 Func_Set_MenuItem_Show ( "资产管理" ) EndSwitch EndFunc ; 切换标签的显示隐藏 Func Func_Set_MenuItem_Show ( $strItemName ) ; 1)通过文本得到是否存在此标签 $id_item = _GUICtrlTab_FindTab ( $tabWelcome, $strItemName, True) ; 2)如果找到了,删除-隐藏 If $id_item <> -1 Then _GUICtrlTab_DeleteItem ( $tabWelcome, $id_item ) Else ; 3)没有找到,则创建 Switch $strItemName Case "工作日志" ; 分配一个固定索引 $id_item = 0 Case "公司产品" $id_item = 1 Case "学校信息" $id_item = 2 Case "工程外包" $id_item = 3 Case "资产管理" $id_item = 4 EndSwitch ; 创建标签 _GUICtrlTab_InsertItem ( $tabWelcome, $id_item, $strItemName, $id_item) EndIf EndFunc |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/933.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设