Ticket #1019: LVItemStateBug.au3

File LVItemStateBug.au3, 11.9 KB (added by Qsek, 15 years ago)
Line 
1;~ #Include <GuiListView.au3>
2#include <WindowsConstants.au3>
3#Include <GUIConstantsEx.au3>
4#Include <WinAPI.au3>
5#Include <ListViewConstants.au3>
6#Include <GuiImageList.au3>
7#Include <string.au3>
8
9Opt("GUIOnEventMode", 1)
10$Debug_LV = 0
11
12$Gui = GUICreate("Test Alpha", 750, 590, 191, 113, BitOR($WS_MAXIMIZEBOX,$WS_MINIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_SYSMENU,$WS_CAPTION))
13
14    $hImage = _GUIImageList_Create(16, 16, 5, 3)
15    _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 110)
16    _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 131)
17    _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 165)
18    _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 168)
19    _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 137)
20    _GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 146)
21
22
23$ListView = GUICtrlCreateListView("text0                       |text1|text2|text3", 8, 120, 121*3, 431, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($LVS_EX_HEADERDRAGDROP, $LVS_EX_FULLROWSELECT,$LVS_EX_GRIDLINES,$LVS_EX_DOUBLEBUFFER,$LVS_EX_SUBITEMIMAGES))
24
25_GUICtrlListView_InsertItem($ListView, "TestText", 1)
26
27_GUICtrlListView_SetImageList($listview, $hImage, 1)
28
29GUISetState(@SW_SHOW,$GUI)
30
31GUISetOnEvent($GUI_EVENT_CLOSE, "Close",$GUI)
32
33GUICtrlSetState($listview, $GUI_FOCUS)
34Func Close()
35        Exit
36EndFunc
37
38ConsoleWrite("-Start--Original UDF's ----" & @CRLF)
39Sleep(2000)
40_GUICtrlListView_SetItemCut($ListView, 0, True)
41ConsoleWrite("SetItemCut: True, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_CUT) & @CRLF)
42Sleep(2000)
43_GUICtrlListView_SetItemCut($ListView, 0, False)
44ConsoleWrite("SetItemCut: False, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_CUT) & @CRLF)
45Sleep(2000)
46_GUICtrlListView_SetItemDropHilited($ListView, 0, True)
47ConsoleWrite("SetItemDropHilited: True, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_DROPHILITED) & @CRLF)
48Sleep(2000)
49_GUICtrlListView_SetItemDropHilited($ListView, 0, False)
50ConsoleWrite("SetItemDropHilited: False, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_DROPHILITED) & @CRLF)
51Sleep(2000)
52_GUICtrlListView_SetItemFocused($ListView, 0, True)
53ConsoleWrite("SetItemFocused: True, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_FOCUSED) & @CRLF)
54Sleep(2000)
55_GUICtrlListView_SetItemFocused($ListView, 0, False)
56ConsoleWrite("SetItemFocused: False, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_FOCUSED) & @CRLF)
57Sleep(2000)
58ConsoleWrite("-End------------------------" & @CRLF)
59_SendMessage(GUICtrlGetHandle($ListView), $LVM_DELETEALLITEMS)
60Sleep(2000)
61
62ConsoleWrite("-Start--Fixed UDF's ----" & @CRLF)
63_GUICtrlListView_InsertItem($ListView, "TestText", 1)
64Sleep(2000)
65__GUICtrlListView_SetItemCut($ListView, 0, True)
66ConsoleWrite("SetItemCut: True, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_CUT) & @CRLF)
67Sleep(2000)
68__GUICtrlListView_SetItemCut($ListView, 0, False)
69ConsoleWrite("SetItemCut: False, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_CUT) & @CRLF)
70Sleep(2000)
71__GUICtrlListView_SetItemDropHilited($ListView, 0, True)
72ConsoleWrite("SetItemDropHilited: True, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_DROPHILITED) & @CRLF)
73Sleep(2000)
74__GUICtrlListView_SetItemDropHilited($ListView, 0, False)
75ConsoleWrite("SetItemDropHilited: False, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_DROPHILITED) & @CRLF)
76Sleep(2000)
77__GUICtrlListView_SetItemFocused($ListView, 0, True)
78ConsoleWrite("SetItemFocused: True, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_FOCUSED) & @CRLF)
79Sleep(2000)
80__GUICtrlListView_SetItemFocused($ListView, 0, False)
81ConsoleWrite("SetItemFocused: False, Result: "&_GUICtrlListView_GetItemState($ListView, 0, $LVIS_FOCUSED) & @CRLF)
82Sleep(2000)
83ConsoleWrite("-End------------------------" & @CRLF)
84
85
86
87
88While 1
89        Sleep(10)
90WEnd
91
92
93
94
95
96; #########################################################################
97; #### Fixed UDF's using _GUICtrlListView_SetItemState in the right way ###
98; #########################################################################
99
100Func __GUICtrlListView_SetItemCut($hWnd, $iIndex, $fEnabled = True)
101        Local $iState = 0
102
103        If $fEnabled Then $iState = $LVIS_CUT
104        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, $iState, $LVIS_CUT)
105EndFunc   ;==>_GUICtrlListView_SetItemCut
106
107
108Func __GUICtrlListView_SetItemFocused($hWnd, $iIndex, $fEnabled = True)
109        Local $iState = 0
110
111        If $fEnabled Then $iState = $LVIS_FOCUSED
112        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, $iState, $LVIS_FOCUSED)
113EndFunc   ;==>_GUICtrlListView_SetItemFocused
114
115
116Func __GUICtrlListView_SetItemDropHilited($hWnd, $iIndex, $fEnabled = True)
117        Local $iState = 0
118
119        If $fEnabled Then $iState = $LVIS_DROPHILITED
120        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, $iState, $LVIS_DROPHILITED)
121EndFunc   ;==>_GUICtrlListView_SetItemDropHilited
122
123
124; ############################################################################
125; #### Original UDF's using _GUICtrlListView_SetItemState in the wrong way ###
126; ############################################################################
127
128Func _GUICtrlListView_SetItemCut($hWnd, $iIndex, $fEnabled = True)
129        Local $iStateMask = 0
130
131        If $fEnabled Then $iStateMask = $LVIS_CUT
132        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, $LVIS_CUT, $iStateMask)
133EndFunc   ;==>_GUICtrlListView_SetItemCut
134
135
136Func _GUICtrlListView_SetItemFocused($hWnd, $iIndex, $fEnabled = True)
137        Local $iStateMask = 0
138
139        If $fEnabled Then $iStateMask = $LVIS_FOCUSED
140        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, $LVIS_FOCUSED, $iStateMask)
141EndFunc   ;==>_GUICtrlListView_SetItemFocused
142
143
144Func _GUICtrlListView_SetItemDropHilited($hWnd, $iIndex, $fEnabled = True)
145        Local $iStateMask = 0
146
147        If $fEnabled Then $iStateMask = $LVIS_DROPHILITED
148        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, $LVIS_DROPHILITED, $iStateMask)
149EndFunc   ;==>_GUICtrlListView_SetItemDropHilited
150
151
152
153; #################################################################################
154; ### Other original UDF's using _GUICtrlListView_SetItemState in the right way ###
155; #################################################################################
156
157Func _GUICtrlListView_SetItemOverlayImage($hWnd, $iIndex, $iImage)
158        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, _GUICtrlListView_IndexToOverlayImageMask($iImage), $LVIS_OVERLAYMASK)
159EndFunc   ;==>_GUICtrlListView_SetItemOverlayImage
160
161
162Func _GUICtrlListView_SetItemStateImage($hWnd, $iIndex, $iImage)
163        Return _GUICtrlListView_SetItemState($hWnd, $iIndex, BitShift($iImage, -12), $LVIS_STATEIMAGEMASK)
164EndFunc   ;==>_GUICtrlListView_SetItemStateImage
165
166
167
168
169
170
171
172
173
174
175
176; #################################################################################
177; ### Other original UDF's for making this example run ###
178; #################################################################################
179
180Func _GUICtrlListView_SetItemState($hWnd, $iIndex, $iState, $iStateMask)
181        Local $tItem
182
183        $tItem = DllStructCreate($tagLVITEM)
184        DllStructSetData($tItem, "Mask", $LVIF_STATE)
185        DllStructSetData($tItem, "Item", $iIndex)
186        DllStructSetData($tItem, "State", $iState)
187        DllStructSetData($tItem, "StateMask", $iStateMask)
188        Return _GUICtrlListView_SetItemEx($hWnd, $tItem) <> 0
189EndFunc   ;==>_GUICtrlListView_SetItemState
190
191
192Func _GUICtrlListView_SetItemEx($hWnd, ByRef $tItem)
193        If $Debug_LV Then _GUICtrlListView_ValidateClassName($hWnd)
194        Local $iItem, $pItem, $iBuffer, $pBuffer, $pMemory, $tMemMap, $pText, $iResult
195        Local $fUnicode = _GUICtrlListView_GetUnicodeFormat($hWnd)
196
197        $pItem = DllStructGetPtr($tItem)
198        If IsHWnd($hWnd) Then
199                $iItem = DllStructGetSize($tItem)
200                $iBuffer = DllStructGetData($tItem, "TextMax")
201                If $fUnicode Then $iBuffer *= 2
202                $pBuffer = DllStructGetData($tItem, "Text")
203                $pMemory = _MemInit($hWnd, $iItem + $iBuffer, $tMemMap)
204                $pText = $pMemory + $iItem
205                DllStructSetData($tItem, "Text", $pText)
206                _MemWrite($tMemMap, $pItem, $pMemory, $iItem)
207                If $pBuffer <> 0 Then _MemWrite($tMemMap, $pBuffer, $pText, $iBuffer)
208                If $fUnicode Then
209                        $iResult = _SendMessage($hWnd, $LVM_SETITEMW, 0, $pMemory, 0, "wparam", "ptr")
210                Else
211                        $iResult = _SendMessage($hWnd, $LVM_SETITEMA, 0, $pMemory, 0, "wparam", "ptr")
212                EndIf
213                _MemFree($tMemMap)
214        Else
215                If $fUnicode Then
216                        $iResult = GUICtrlSendMsg($hWnd, $LVM_SETITEMW, 0, $pItem)
217                Else
218                        $iResult = GUICtrlSendMsg($hWnd, $LVM_SETITEMA, 0, $pItem)
219                EndIf
220        EndIf
221        Return $iResult <> 0
222EndFunc   ;==>_GUICtrlListView_SetItemEx
223
224
225Func _GUICtrlListView_GetUnicodeFormat($hWnd)
226        If $Debug_LV Then _GUICtrlListView_ValidateClassName($hWnd)
227        If IsHWnd($hWnd) Then
228                Return _SendMessage($hWnd, $LVM_GETUNICODEFORMAT) <> 0
229        Else
230                Return GUICtrlSendMsg($hWnd, $LVM_GETUNICODEFORMAT, 0, 0) <> 0
231        EndIf
232EndFunc   ;==>_GUICtrlListView_GetUnicodeFormat
233
234
235Func _GUICtrlListView_InsertItem($hWnd, $sText, $iIndex = -1, $iImage = -1, $iParam = 0)
236        If $Debug_LV Then _GUICtrlListView_ValidateClassName($hWnd)
237        Local $iBuffer, $pBuffer, $tBuffer, $iItem, $pItem, $tItem, $pMemory, $tMemMap, $pText, $iMask, $iResult
238        If $iIndex = -1 Then $iIndex = 999999999
239        Local $fUnicode = _GUICtrlListView_GetUnicodeFormat($hWnd)
240
241        $tItem = DllStructCreate($tagLVITEM)
242        $pItem = DllStructGetPtr($tItem)
243        DllStructSetData($tItem, "Param", $iParam)
244        If $sText <> -1 Then
245                $iBuffer = StringLen($sText) + 1
246                If $fUnicode Then
247                        $iBuffer *= 2
248                        $tBuffer = DllStructCreate("wchar Text[" & $iBuffer & "]")
249                Else
250                        $tBuffer = DllStructCreate("char Text[" & $iBuffer & "]")
251                EndIf
252                $pBuffer = DllStructGetPtr($tBuffer)
253                DllStructSetData($tBuffer, "Text", $sText)
254                DllStructSetData($tItem, "Text", $pBuffer)
255                DllStructSetData($tItem, "TextMax", $iBuffer)
256        Else
257                DllStructSetData($tItem, "Text", -1)
258        EndIf
259        $iMask = BitOR($LVIF_TEXT, $LVIF_PARAM)
260        If $iImage >= 0 Then $iMask = BitOR($iMask, $LVIF_IMAGE)
261        DllStructSetData($tItem, "Mask", $iMask)
262        DllStructSetData($tItem, "Item", $iIndex)
263        DllStructSetData($tItem, "Image", $iImage)
264        If IsHWnd($hWnd) Then
265                If _WinAPI_InProcess($hWnd, $_lv_ghLastWnd) Or ($sText = -1) Then
266                        If $fUnicode Then
267                                $iResult = _SendMessage($hWnd, $LVM_INSERTITEMW, 0, $pItem, 0, "wparam", "ptr")
268                        Else
269                                $iResult = _SendMessage($hWnd, $LVM_INSERTITEMA, 0, $pItem, 0, "wparam", "ptr")
270                        EndIf
271                Else
272                        $iItem = DllStructGetSize($tItem)
273                        $pMemory = _MemInit($hWnd, $iItem + $iBuffer, $tMemMap)
274                        $pText = $pMemory + $iItem
275                        DllStructSetData($tItem, "Text", $pText)
276                        _MemWrite($tMemMap, $pItem, $pMemory, $iItem)
277                        _MemWrite($tMemMap, $pBuffer, $pText, $iBuffer)
278                        If $fUnicode Then
279                                $iResult = _SendMessage($hWnd, $LVM_INSERTITEMW, 0, $pMemory, 0, "wparam", "ptr")
280                        Else
281                                $iResult = _SendMessage($hWnd, $LVM_INSERTITEMA, 0, $pMemory, 0, "wparam", "ptr")
282                        EndIf
283                        _MemFree($tMemMap)
284                EndIf
285        Else
286                If $fUnicode Then
287                        $iResult = GUICtrlSendMsg($hWnd, $LVM_INSERTITEMW, 0, $pItem)
288                Else
289                        $iResult = GUICtrlSendMsg($hWnd, $LVM_INSERTITEMA, 0, $pItem)
290                EndIf
291        EndIf
292        Return $iResult
293EndFunc   ;==>_GUICtrlListView_InsertItem
294
295
296
297Func _GUICtrlListView_SetImageList($hWnd, $hHandle, $iType = 0)
298        If $Debug_LV Then _GUICtrlListView_ValidateClassName($hWnd)
299        Local $aType[3] = [$LVSIL_NORMAL, $LVSIL_SMALL, $LVSIL_STATE]
300
301        If IsHWnd($hWnd) Then
302                Return _SendMessage($hWnd, $LVM_SETIMAGELIST, $aType[$iType], $hHandle, 0, "wparam", "hwnd", "hwnd")
303        Else
304                Return GUICtrlSendMsg($hWnd, $LVM_SETIMAGELIST, $aType[$iType], $hHandle)
305        EndIf
306EndFunc   ;==>_GUICtrlListView_SetImageList
307
308
309Func _GUICtrlListView_GetItemState($hWnd, $iIndex, $iMask)
310        If $Debug_LV Then _GUICtrlListView_ValidateClassName($hWnd)
311        If IsHWnd($hWnd) Then
312                Return _SendMessage($hWnd, $LVM_GETITEMSTATE, $iIndex, $iMask)
313        Else
314                Return GUICtrlSendMsg($hWnd, $LVM_GETITEMSTATE, $iIndex, $iMask)
315        EndIf
316EndFunc   ;==>_GUICtrlListView_GetItemState