如何反转列表框中的选定项?

时间:2015-03-26 06:43:39

标签: user-interface listbox autohotkey

我在AutoHotkey中创建了一个带GUI的简单脚本。有一个包含长项目列表的多选列表框。我想在那里创建“反转选择按钮”。

我找到了以下消息:

PostMessage, 0x185, 1, -1, ListBox1  ; Select all items. 0x185 is LB_SETSEL.
PostMessage, 0x185, 0, -1, ListBox1  ; Deselect all items.

“反转选择”有类似之处吗? 如果没有,我怎么能在AutoHotkey中编写这样的东西?

1 个答案:

答案 0 :(得分:0)

我在这里提供了一些符合您要求的代码。 我一直在研究GUI函数库, 并刚刚完成了一些ListBox函数的工作, 包括JEE_ListBoxInvSel(hCtl)

以下是一些示例代码,具有以下功能。 我已经彻底测试了一切。请发表评论 如果有任何问题。

^q::
ControlGet, hCtl, Hwnd, , ListBox1, A
vList = 1,3,7
JEE_ListBoxSetSel(hCtl, vList, "ds")
Sleep 2000

MsgBox % JEE_ListBoxGetSel(hCtl)
Sleep 2000

JEE_ListBoxInvSel(hCtl)
Sleep 2000
JEE_ListBoxInvSel(hCtl)
Sleep 2000
JEE_ListBoxInvSel(hCtl)
Sleep 2000
MsgBox done
Return

;==================================================

;selected GUI functions by jeeswg

;JEE_ListBoxInvertSelection
;JEE_ListBoxSetSelInv
JEE_ListBoxInvSel(hCtl)
{
SendMessage, 0x18B, , , , ahk_id %hCtl% ;LB_GETCOUNT
vCount := ErrorLevel
SendMessage, 0x190, , , , ahk_id %hCtl% ;LB_GETSELCOUNT
vCountSel := ErrorLevel

if (vCount-vCountSel > vCountSel)
vList := JEE_ListBoxGetSel(hCtl, 1), JEE_ListBoxSetSel(hCtl, vList, "sd")
else
vList := JEE_ListBoxGetSel(hCtl, 0), JEE_ListBoxSetSel(hCtl, vList, "ds")
Return
}

;==================================================

;returns 1-based index of focused item
JEE_ListBoxGetFocus(hCtl)
{
;LB_GETCARETINDEX := 0x19F
SendMessage, 0x19F, , , , ahk_id %hCtl% ;LB_GETCARETINDEX
Return ErrorLevel+1
}

;sets 1-based index of focused item
JEE_ListBoxSetFocus(hCtl, vNum, vOpt=0)
{
;LB_SETCARETINDEX := 0x19E
SendMessage, 0x19E, vNum-1, 0, , ahk_id %hCtl% ;LB_SETCARETINDEX
Return
}

;==================================================

;returns a comma-separated list of 1-based indexes for selected items
;JEE_ListBoxGetSelectedItems
JEE_ListBoxGetSel(hCtl, vOpt=1)
{
;LB_GETCOUNT := 0x18B
;LB_GETSELCOUNT := 0x190
;LB_GETSELITEMS := 0x191

SendMessage, 0x190, , , , ahk_id %hCtl% ;LB_GETSELCOUNT
vCountSel := ErrorLevel
VarSetCapacity(vBuf, 32*vCountSel)
SendMessage, 0x191, vCountSel, &vBuf, , ahk_id %hCtl% ;LB_GETSELITEMS
vRet := ErrorLevel

Loop, %vRet%
vOutput .= (NumGet(vBuf, A_Index*4-4, "UInt")+1) ","
vOutput := SubStr(vOutput, 1, -1) ;trim right

if (vOpt = 0)
{
SendMessage, 0x18B, , , , ahk_id %hCtl% ;LB_GETCOUNT
vCount := ErrorLevel
Loop, %vCount%
if A_Index not in %vOutput%
vOutput2 .= A_Index ","
vOutput2 := SubStr(vOutput2, 1, -1) ;trim right
Return vOutput2
}

Return vOutput
}

;==================================================

;description:
;hCtl: handle to ListBox control

;vList: comma-separated list of items (1-based index)
;e.g. '1,3,5' (select the 1st,3rd,5th items)
;e.g. '3' (select the 3rd item)

;vOpt: s/d/sd/ds/sa/da
;s select items in list
;d deselect items in list
;sd select all items and deselect items in list
;ds deselect all items and select items in list
;sa select all items
;da deselect all items

;JEE_ListBoxSelectItems
JEE_ListBoxSetSel(hCtl, vList, vOpt="s")
{
;LB_SETSEL := 0x185

if vOpt in sa,sd
SendMessage, 0x185, 1, -1, , ahk_id %hCtl% ;LB_SETSEL
if vOpt in da,ds
SendMessage, 0x185, 0, -1, , ahk_id %hCtl% ;LB_SETSEL
if vOpt in sa,da
Return

if vOpt in s,ds
vState := 1
if vOpt in d,sd
vState := 0

Loop, Parse, vList, `,
SendMessage, 0x185, vState, A_LoopField-1, , ahk_id %hCtl% ;LB_SETSEL
Return
}

;==================================================
相关问题