|
发表于 2020-10-17 19:11:19
|
显示全部楼层
从英文官网搬了两个列子,本质上都是先删除后插入,然后例三,自定义控件,应该符合你的需求
例一:#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <GuiTab.au3>
Global $aItem
$Form1 = GUICreate("Form1", 321, 258, -1, -1)
$Tab1 = GUICtrlCreateTab(16, 16, 289, 177)
GUICtrlCreateTabItem("Default Tab")
GUICtrlCreateTabItem("")
_GUICtrlTab_InsertItem($Tab1, 1, "Tab for Hide/Show")
$Checkbox1 = GUICtrlCreateCheckbox("Hide/Show", 16, 208, 289, 33, BitOR($BS_CHECKBOX,$BS_AUTOCHECKBOX,$BS_PUSHLIKE,$WS_TABSTOP))
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Checkbox1
$read = GUICtrlRead($Checkbox1)
If $read = 1 Then
MsgBox(0,"","Now i want to hide Tab")
_GUICtrlTab_DeleteItem($Tab1, 1)
Else
MsgBox(0,"","Now i want to show Tab")
_GUICtrlTab_InsertItem($Tab1, 1, "Tab for Hide/Show")
EndIf
EndSwitch
WEnd
例二:#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTab.au3>
$Form1 = GUICreate("Form1", 321, 258, -1, -1)
$Tab1 = GUICtrlCreateTab(16, 16, 289, 177)
GUICtrlCreateTabItem("Default Tab")
$button = GUICtrlCreateButton("Dugme",70,50,100,20)
GUICtrlDelete($button)
GUICtrlCreateTabItem("")
$Checkbox1 = GUICtrlCreateCheckbox("Hide/Show", 16, 208, 289, 33, BitOR($BS_CHECKBOX,$BS_AUTOCHECKBOX,$BS_PUSHLIKE,$WS_TABSTOP))
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Checkbox1
$read = GUICtrlRead($Checkbox1)
If $read = 4 Then
GUICtrlSetData($Checkbox1,"Show")
_GUICtrlTab_DeleteItem($Tab1,1)
GUICtrlDelete($button)
Else
GUICtrlSetData($Checkbox1,"Hide")
GUICtrlCreateTabItem("Tab for Hide/Show")
$button = GUICtrlCreateButton("Dugme",70,50,100,20)
GUICtrlCreateTabItem("")
EndIf
Case $button
MsgBox(0,"","Ok")
EndSwitch
WEnd
例三:#include <GUIConstantsEx.au3>
#include <GuiTab.au3>
#include <GuiToolbar.au3>
#include <GuiImageList.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Global $itemInToolbar, $idOfTabItem ; 添加一个全局变量$idOfTabItem记录被右击的标签索引
$guiWelcome = GUICreate("www.cuiweiyou.com", 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")
$menuTab = GUICtrlCreateMenu("窗口 &W")
$itemWorkersInMenuTab = GUICtrlCreateMenuItem("工作日志", $menuTab)
GUICtrlSetOnEvent($itemWorkersInMenuTab, "Func_MenuItem_ShowTab")
$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")
$toolbar = _GUICtrlToolbar_Create($guiWelcome)
_GUICtrlToolbar_AddBitmap($toolbar, 1, -1, $IDB_STD_SMALL_COLOR)
Local Enum $id_New = 1000, $id_Open, $id_Save, $id_Find, $id_Help
_GUICtrlToolbar_AddButton($toolbar, $id_New, $STD_FILENEW)
_GUICtrlToolbar_AddButton($toolbar, $id_Open, $STD_FILEOPEN)
_GUICtrlToolbar_AddButton($toolbar, $id_Save, $STD_FILESAVE)
_GUICtrlToolbar_AddButtonSep($toolbar)
_GUICtrlToolbar_AddButton($toolbar, $id_Find, $STD_FIND)
_GUICtrlToolbar_AddButtonSep($toolbar)
_GUICtrlToolbar_AddButton($toolbar, $id_Help, $STD_HELP)
$tabWelcome = GUICtrlCreateTab(1, 28, 448, 130)
GUICtrlSetOnEvent($tabWelcome, "Func_TabWelcome")
$imgList = _GUIImageList_Create()
_GUIImageList_Add($imgList, _WinAPI_CreateSolidBitmap($guiWelcome, 0x990033, 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)
$itemWorkerInTabWelcome = _GUICtrlTab_InsertItem($tabWelcome, 0, "工作日志", 0)
$mouseMenuTab = GUICtrlCreateContextMenu($tabWelcome)
$mouseMenuItemClose = GUICtrlCreateMenuItem("关闭", $mouseMenuTab)
GUICtrlSetOnEvent($mouseMenuItemClose, "Func_MouseMenuItem")
GUICtrlCreateMenuItem("", $mouseMenuTab)
$mouseMenuItemSave = GUICtrlCreateMenuItem("保存", $mouseMenuTab)
GUICtrlSetOnEvent($mouseMenuItemSave, "Func_MouseMenuItem")
$mouseMenuItemSaveAs = GUICtrlCreateMenuItem("另存为", $mouseMenuTab)
GUICtrlSetOnEvent($mouseMenuItemSaveAs, "Func_MouseMenuItem")
GUICtrlCreateMenuItem("", $mouseMenuTab)
$mouseMenuItemPrint = GUICtrlCreateMenuItem("打印", $mouseMenuTab)
GUICtrlSetOnEvent($mouseMenuItemPrint, "Func_MouseMenuItem")
GUICtrlCreateLabel("文本 3", 1, 160, 50, 20)
; 1.创建标签卡上的控件。在窗体最后创建,避免被遮挡。设置合适的位置和宽高,使之看起来刚好嵌入标签
; 工作日志
$lvInTabWorks = GUICtrlCreateListView("日志编号|时间|工程师|学校|交通|饮食|工作内容|备注", 4, 51, 443, 106)
GUICtrlSetState($lvInTabWorks, $GUI_SHOW) ; 可见
; 公司产品
$lvInTabProduces = GUICtrlCreateListView("产品编号|名称|类型|设计|造价|销售|配置单|备注", 4, 51, 443, 106)
GUICtrlSetState($lvInTabProduces, $GUI_HIDE) ; 隐藏
; 学校信息
$lvInTabSchools = GUICtrlCreateListView("学校编号|学校|校方联系人|我司负责人|合作项目|项目状态|备注", 4, 51, 443, 106)
GUICtrlSetState($lvInTabSchools, $GUI_HIDE) ; 隐藏
; 工程外包
$lvInTabProjects = GUICtrlCreateListView("工程编号|项目|学校|承包商|我司负责人|项目细则|备注", 4, 51, 443, 106)
GUICtrlSetState($lvInTabProjects, $GUI_HIDE) ; 隐藏
; 资产管理
$lvInTabAssets = GUICtrlCreateListView("物资编号|名称|计量单位|类型|购入日期|原价|部门|保管|报废|备注", 4, 51, 443, 106)
GUICtrlSetState($lvInTabAssets, $GUI_HIDE) ; 隐藏
GUISetState(@SW_SHOW, $guiWelcome)
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
While 1
Sleep(200)
WEnd
Func Func_GUI_EVENT_CLOSE()
Exit
EndFunc ;==>Func_GUI_EVENT_CLOSE
Func Func_TabWelcome()
$ctrlId = GUICtrlRead(@GUI_CtrlId)
$itemText = _GUICtrlTab_GetItemText(@GUI_CtrlId, $ctrlId)
ConsoleWrite("你点击的标签卡:" & $ctrlId & " - " & $itemText & @CRLF)
; 2.单击时切换标签的ListView------------------------------------------------------------
GUICtrlSetState($lvInTabWorks, $GUI_HIDE) ; 1)全部隐藏
GUICtrlSetState($lvInTabProduces, $GUI_HIDE)
GUICtrlSetState($lvInTabSchools, $GUI_HIDE)
GUICtrlSetState($lvInTabProjects, $GUI_HIDE)
GUICtrlSetState($lvInTabAssets, $GUI_HIDE)
Switch $itemText ; 2)根据文本显示被选中的
Case "工作日志"
GUICtrlSetState($lvInTabWorks, $GUI_SHOW)
Case "公司产品"
GUICtrlSetState($lvInTabProduces, $GUI_SHOW)
Case "学校信息"
GUICtrlSetState($lvInTabSchools, $GUI_SHOW)
Case "工程外包"
GUICtrlSetState($lvInTabProjects, $GUI_SHOW)
Case "资产管理"
GUICtrlSetState($lvInTabAssets, $GUI_SHOW)
EndSwitch
EndFunc ;==>Func_TabWelcome
Func _WM_NOTIFY($hWndGUI, $MsgID, $wParam, $lParam)
Local $tNMHDR, $hwndFrom, $code, $i_idOld, $i_idNew
Local $tNMTBHOTITEM
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hwndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$code = DllStructGetData($tNMHDR, "Code")
Switch $hwndFrom
Case $toolbar
Switch $code
Case $TBN_HOTITEMCHANGE
$tNMTBHOTITEM = DllStructCreate($tagNMTBHOTITEM, $lParam)
$i_idOld = DllStructGetData($tNMTBHOTITEM, "idOld")
$i_idNew = DllStructGetData($tNMTBHOTITEM, "idNew")
$itemInToolbar = $i_idNew
Case $NM_CLICK
Switch $itemInToolbar
Case 1000
ConsoleWrite("点击的工具条按钮:新建" & @CRLF)
$itemInToolbar = -1
Case $id_Open
ConsoleWrite("点击的工具条按钮:打开" & @CRLF)
$itemInToolbar = -1
Case $id_Save
ConsoleWrite("点击的工具条按钮:保存" & @CRLF)
$itemInToolbar = -1
Case 1003
ConsoleWrite("点击的工具条按钮:查找" & @CRLF)
$itemInToolbar = -1
Case 1004
ConsoleWrite("点击的工具条按钮:帮助" & @CRLF)
$itemInToolbar = -1
EndSwitch
EndSwitch
Case GUICtrlGetHandle($tabWelcome)
Switch $code
Case $NM_RCLICK
Local $x, $y, $aHit
$x = _WinAPI_GetMousePosX(True, GUICtrlGetHandle($tabWelcome))
$y = _WinAPI_GetMousePosY(True, GUICtrlGetHandle($tabWelcome))
$aHit = _GUICtrlTab_HitTest($tabWelcome, $x, $y)
$idOfTabItem = $aHit[0]
_GUICtrlTab_SetCurSel($tabWelcome, $aHit[0])
ConsoleWrite("右击标签卡:" & _GUICtrlTab_GetCurSel($tabWelcome) & @CRLF)
; 3.右击标签时切换ListView-----------------------------------------
; 将以下代码封装到一个方法里调用,不成功
GUICtrlSetState($lvInTabWorks, $GUI_HIDE) ; 1)隐藏
GUICtrlSetState($lvInTabProduces, $GUI_HIDE)
GUICtrlSetState($lvInTabSchools, $GUI_HIDE)
GUICtrlSetState($lvInTabProjects, $GUI_HIDE)
GUICtrlSetState($lvInTabAssets, $GUI_HIDE)
Switch _GUICtrlTab_GetItemText($tabWelcome, $aHit[0]) ; 2)根据标签文本控制
Case "工作日志"
GUICtrlSetState($lvInTabWorks, $GUI_SHOW)
Case "公司产品"
GUICtrlSetState($lvInTabProduces, $GUI_SHOW)
Case "学校信息"
GUICtrlSetState($lvInTabSchools, $GUI_SHOW)
Case "工程外包"
GUICtrlSetState($lvInTabProjects, $GUI_SHOW)
Case "资产管理"
GUICtrlSetState($lvInTabAssets, $GUI_SHOW)
EndSwitch
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_NOTIFY
Func Func_MouseMenuItem()
Switch @GUI_CtrlId
Case $mouseMenuItemClose
ConsoleWrite("鼠标右键菜单点击了:111关闭" & @CRLF)
; 4.右击菜单“关闭”,切换ListView-------------------------------------------------
GUICtrlSetState($lvInTabWorks, $GUI_HIDE)
GUICtrlSetState($lvInTabProduces, $GUI_HIDE)
GUICtrlSetState($lvInTabSchools, $GUI_HIDE)
GUICtrlSetState($lvInTabProjects, $GUI_HIDE)
GUICtrlSetState($lvInTabAssets, $GUI_HIDE)
; 1)鼠标右击的标签ID保存在全局变量$idOfTabItem里,如果是第一个标签
If $idOfTabItem = 0 Then
_GUICtrlTab_SetCurSel($tabWelcome, 1) ; 那么首先切换到下一个标签
Else
_GUICtrlTab_SetCurSel($tabWelcome, $idOfTabItem - 1) ; 否则前移
EndIf
; 2)移除右击的标签
_GUICtrlTab_DeleteItem($tabWelcome, $idOfTabItem)
; 3)然后获取如今最新选中的标签
$strTabItemText = _GUICtrlTab_GetItemText($tabWelcome, _GUICtrlTab_GetCurSel($tabWelcome))
; 4)根据标签切换ListView
Switch $strTabItemText
Case "工作日志"
GUICtrlSetState($lvInTabWorks, $GUI_SHOW)
Case "公司产品"
GUICtrlSetState($lvInTabProduces, $GUI_SHOW)
Case "学校信息"
GUICtrlSetState($lvInTabSchools, $GUI_SHOW)
Case "工程外包"
GUICtrlSetState($lvInTabProjects, $GUI_SHOW)
Case "资产管理"
GUICtrlSetState($lvInTabAssets, $GUI_SHOW)
EndSwitch
$idOfTabItem = -1
Case $mouseMenuItemSave
ConsoleWrite("鼠标菜单点击了:2222222保存" & @CRLF)
Case $mouseMenuItemSaveAs
ConsoleWrite("鼠标菜单点击了:333333333另存为" & @CRLF)
Case $mouseMenuItemPrint
ConsoleWrite("鼠标菜单点击了:44444444444444打印" & @CRLF)
EndSwitch
EndFunc ;==>Func_MouseMenuItem
Func Func_MenuItem_ShowTab()
Switch @GUI_CtrlId
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_MenuItem_ShowTab
Func Func_Set_MenuItem_Show($strItemName)
; 5.菜单栏菜单项切换ListView------------------------------------------------------------
GUICtrlSetState($lvInTabWorks, $GUI_HIDE)
GUICtrlSetState($lvInTabProduces, $GUI_HIDE)
GUICtrlSetState($lvInTabSchools, $GUI_HIDE)
GUICtrlSetState($lvInTabProjects, $GUI_HIDE)
GUICtrlSetState($lvInTabAssets, $GUI_HIDE)
; 1)查找目标标签
$id_item = _GUICtrlTab_FindTab($tabWelcome, $strItemName, True)
; 2)如果找到了
If $id_item <> -1 Then
; 如果已经处于选中状态
If _GUICtrlTab_GetItemState($tabWelcome, $id_item) = $TCIS_BUTTONPRESSED Then
If $id_item = 0 Then ; 同时是第一个标签
_GUICtrlTab_SetCurSel($tabWelcome, 1) ; 那么切换为选中下一个标签
Else
_GUICtrlTab_SetCurSel($tabWelcome, 0) ; 否则选中第一个标签
EndIf
EndIf
; 1> 移除目标标签
_GUICtrlTab_DeleteItem($tabWelcome, $id_item)
; 2> 根据最新选中的标签切换ListView
$strTabItemText = _GUICtrlTab_GetItemText($tabWelcome, _GUICtrlTab_GetCurSel($tabWelcome))
Switch $strTabItemText
Case "工作日志"
GUICtrlSetState($lvInTabWorks, $GUI_SHOW)
Case "公司产品"
GUICtrlSetState($lvInTabProduces, $GUI_SHOW)
Case "学校信息"
GUICtrlSetState($lvInTabSchools, $GUI_SHOW)
Case "工程外包"
GUICtrlSetState($lvInTabProjects, $GUI_SHOW)
Case "资产管理"
GUICtrlSetState($lvInTabAssets, $GUI_SHOW)
EndSwitch
; 3)如果没有找到目标标签,那么创建,然后切换ListView
Else
Switch $strItemName
Case "工作日志"
$id_item = 0
Case "公司产品"
$id_item = 1
Case "学校信息"
$id_item = 2
Case "工程外包"
$id_item = 3
Case "资产管理"
$id_item = 4
EndSwitch
; 1> 创建标签
_GUICtrlTab_InsertItem($tabWelcome, $id_item, $strItemName, $id_item)
; 2> 使刚插入的标签选中
For $i = 0 To _GUICtrlTab_GetItemCount($tabWelcome) - 1 Step 1 ; 遍历标签
If $strItemName = _GUICtrlTab_GetItemText($tabWelcome, $i) Then ; 如果是刚插入的
_GUICtrlTab_SetCurSel($tabWelcome, $i) ; 选中
GUICtrlSetState($lvInTabWorks, $GUI_HIDE)
GUICtrlSetState($lvInTabProduces, $GUI_HIDE)
GUICtrlSetState($lvInTabSchools, $GUI_HIDE)
GUICtrlSetState($lvInTabProjects, $GUI_HIDE)
GUICtrlSetState($lvInTabAssets, $GUI_HIDE)
; 3> 切换ListView
Switch $strItemName
Case "工作日志"
GUICtrlSetState($lvInTabWorks, $GUI_SHOW)
Case "公司产品"
GUICtrlSetState($lvInTabProduces, $GUI_SHOW)
Case "学校信息"
GUICtrlSetState($lvInTabSchools, $GUI_SHOW)
Case "工程外包"
GUICtrlSetState($lvInTabProjects, $GUI_SHOW)
Case "资产管理"
GUICtrlSetState($lvInTabAssets, $GUI_SHOW)
EndSwitch
EndIf
Next
EndIf
EndFunc ;==>Func_Set_MenuItem_Show
代码来源:
https://www.autoitscript.com/for ... ents#comment-571432
http://www.gaohaiyan.com/945.html |
评分
-
查看全部评分
|