AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[AHK_L 60+] ListView handle library 1.01
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Solar



Joined: 03 May 2009
Posts: 345
Location: OH, USA

PostPosted: Fri Jun 12, 2009 10:06 pm    Post subject: [AHK_L 60+] ListView handle library 1.01 Reply with quote

This is for those people that use more than 1 ListView control and wish to control them using handles to avoid problems caused by thread interruptions. It is still incomplete but it has enough to manage a basic ListView in report mode. All of these functions use/return 1 based indices for AHK loop compatibility.

The built-in functions are much faster so I don't recommend using this library for time critical applications.

1.0 -> 1.01 Changes:
- added unicode support for LVM_GetText.
- now using "A_IsUnicode" variable to make sure we use the appropriate messages in LVM_GetText, LVM_Modify, and LVM_Insert.

0.92 -> 1.0 Changes:
- library now requires AutoHotkey_L 60+.
- changed the modify and insert functions to use a variadic parameter for data fields.
- removed the "end column" parameter from the modify and insert functions. You must now only supply the start column. This is thanks to the variadic parameter. However, due to this change, you may need to update your function calls.

0.91 -> 0.92 Changes:
- library now requires AutoHotkey_L 42+.
- added A_PtrSize variable for optional 64-bit support.
- changed LVM_SETITEM to LVM_SETITEMW for unicode support.
- now using "ptr" type on various dllcalls for pointer sized integers.

0.90 -> 0.91 Changes:
- added "LVM_GetColOrder" function.
- fixed "LVM_Insert" function not correctly adding rows to the bottom of the LV.
- fixed comments for "LVM_Modify" function.


Download

Code:
/*
  ListView function library by Solar. Version 1.01
 
  Flag reference:
   flag  constant  description
   -
   LVIS_FOCUSED         1       The item has the focus, so it is surrounded by a standard focus rectangle. Although more than one item may be selected, only one item can have the focus.
   LVIS_SELECTED        2       The item is selected. The appearance of a selected item depends on whether it has the focus and also on the system colors used for selection.
   LVIS_CUT             4       The item is marked for a cut-and-paste operation.
   LVIS_DROPHILITED     8       The item is highlighted as a drag-and-drop target.
   LVIS_ACTIVATING      0x20    Not currently supported.
   LVIS_UNCHECKED       0x1000  Undocumented.
   LVIS_CHECKED         0x2000  Undocumented.
   LVIS_OVERLAYMASK     0xF00   Use this mask to retrieve the item's overlay image index.
   LVIS_STATEIMAGEMASK  0xF000  Use this mask to retrieve the item's state image index.
   -
   LVNI_ALL            0      Searches for a subsequent item by index, the default value.
   LVNI_FOCUSED        1      The item has the LVIS_FOCUSED state flag set.
   LVNI_SELECTED       2      The item has the LVIS_SELECTED state flag set.
   LVNI_CUT            4      The item has the LVIS_CUT state flag set.
   LVNI_DROPHILITED    8      The item has the LVIS_DROPHILITED state flag set
   LVNI_STATEMASK      0xf    Microsoft Windows Vista and later: A state flag mask with value as follows: LVNI_FOCUSED | LVNI_SELECTED | LVNI_CUT | LVNI_DROPHILITED.
   LVNI_VISIBLEORDER   0x10   Searches for a subsequent item by index, the default value.
   LVNI_PREVIOUS       0x20   Microsoft Windows Vista and later: Searches for an item that is ordered before the item specified in plvii. The LVNI_PREVIOUS flag is not directional (LVNI_ABOVE will find the item positioned above, while LVNI_PREVIOUS will find the item ordered before.) The LVNI_PREVIOUS flag basically reverses the logic of the search performed by the LVM_GETNEXTITEM or LVM_GETNEXTITEMINDEX messages.
   LVNI_VISIBLEONLY    0x40   Microsoft Windows Vista and later: Search the visible items.
   LVNI_SAMEGROUPONLY  0x80   Microsoft Windows Vista and later: Search the current group.
   LVNI_ABOVE          0x100  Searches for an item that is above the specified item.
   LVNI_BELOW          0x200  Searches for an item that is below the specified item.
   LVNI_TOLEFT         0x400  Searches for an item to the left of the specified item.
   LVNI_TORIGHT        0x800  Searches for an item to the right of the specified item.
   LVNI_DIRECTIONMASK  0xf00  Microsoft Windows Vista and later: A directional flag mask with value as follows: LVNI_ABOVE | LVNI_BELOW | LVNI_TOLEFT | LVNI_TORIGHT.
*/


;  h = ListView handle.
LVM_GetCount(h)
{
   Return DllCall("SendMessage", "uint", h, "uint", 4100, "uint", 0, "uint", 0) ; LVM_GETITEMCOUNT
}

; h = ListView handle.
LVM_GetColOrder(h)
{
   hdrH := DllCall("SendMessage", "uint", h, "uint", 4127) ; LVM_GETHEADER
   hdrC := DllCall("SendMessage", "uint", hdrH, "uint", 4608) ; HDM_GETITEMCOUNT
   VarSetCapacity(o, hdrC * A_PtrSize)
   DllCall("SendMessage", "uint", h, "uint", 4155, "uint", hdrC, "ptr", &o) ; LVM_GETCOLUMNORDERARRAY
   Loop, % hdrC
      result .= NumGet(&o, (A_Index - 1) * A_PtrSize) + 1 . ","
   StringTrimRight, result, result, 1
   Return result
}

; h = ListView handle.
; c = 1 based indexed comma delimited list of the new column order.
LVM_SetColOrder(h, c)
{
   StringSplit, c, c, `,
   VarSetCapacity(c, c0 * A_PtrSize)
   Loop, % c0
      NumPut(c%A_Index% - 1, c, (A_Index - 1) * A_PtrSize)
   Return DllCall("SendMessage", "uint", h, "uint", 4154, "uint", c0, "ptr", &c) ; LVM_SETCOLUMNORDERARRAY
}

; h = ListView handle.
; c = 1 based column index to get width of.
LVM_GetColWidth(h, c)
{
   Return DllCall("SendMessage", "uint", h, "uint", 4125, "uint", c-1, "uint", 0) ; LVM_GETCOLUMNWIDTH
}

; h = ListView handle.
; c = 1 based column index to get width of.
; w = New width of the column in pixels. Defaults to -1. The following values are supported in report-view mode:
;   "-1" - Automatically sizes the column.
;   "-2" - Automatically sizes the column to fit the header text. If you use this value with the last column, its width is set to fill the remaining width of the list-view control.
LVM_SetColWidth(h, c, w=-1)
{
   Return DllCall("SendMessage", "uint", h, "uint", 4126, "uint", c-1, "int", w) ; LVM_SETCOLUMNWIDTH
}

; h = ListView handle.
; r = 1 based index of the starting row for the flag search. Omit or 0 to find first occurance specified flags.
; o = Combination of one or more LVNI flags. See reference above.
LVM_GetNext(h, r=0, o=0)
{
   Return DllCall("SendMessage", "uint", h, "uint", 4108, "uint", r-1, "uint", o) + 1 ; LVM_GETNEXTITEM
}

; h = ListView handle.
; r = 1 based index of the row to retrieve the text from.
; c = 1 based index of the column to retrieve the text from.
LVM_GetText(h, r, c=1)
{
   r -= 1 ; convert to 0 based index
   VarSetCapacity(t, 511, 1)
   VarSetCapacity(lvItem, A_PtrSize * 7)
   NumPut(1, lvItem, "uint") ; mask
   NumPut(r, lvItem, A_PtrSize, "int") ; iItem
   NumPut(c-1, lvItem, A_PtrSize * 2, "int") ; iSubItem
   NumPut(&t, lvItem, A_PtrSize * 5, "ptr") ; pszText
   NumPut(512, lvItem, A_PtrSize * 6) ; cchTextMax
   If (A_IsUnicode)
      DllCall("SendMessage", "uint", h, "uint", 4211, "uint", r, "ptr", &lvItem) ; LVM_GETITEMTEXTW
   Else
      DllCall("SendMessage", "uint", h, "uint", 4141, "uint", r, "ptr", &lvItem) ; LVM_GETITEMTEXTA
   Return t
}

; h = ListView handle.
; i = 1 based index of item to delete. Omit to delete all items.
LVM_Delete(h, i=0)
{
   If (i)
      DllCall("SendMessage", "uint", h, "uint", 4104, "uint", i-1, "uint", 0) ; LVM_DELETEITEM
   Else
      DllCall("SendMessage", "uint", h, "uint", 4105, "uint", 0, "uint", 0) ; LVM_DELETEALLITEMS
}

; h = ListView handle.
; r = 1 based index of the row to be modified.
; c = 1 based index of the starting column.
; o = Combination of LVIS flags (see above).
; f* = Fields to modify each column. Each field will be modified in subsequent order starting with the c param. This param is variadic. See: http://www.autohotkey.net/~Lexikos/AutoHotkey_L/docs/Functions.htm#Variadic
LVM_Modify(h, r, c=1, o=0, f*)
{
   c -= 1 ; convert to 0 based index
   VarSetCapacity(lvItem, A_PtrSize * 6, 0)
   NumPut(9, lvItem, "uint")   ; mask
   NumPut(r-1, lvItem, A_PtrSize, "int") ; iItem
   NumPut(o, lvItem, A_PtrSize * 3, "uint") ; state
   NumPut(1, lvItem, A_PtrSize * 4, "uint") ; stateMask
   For index,field in f
   {
      NumPut(c + A_Index - 1, lvItem, A_PtrSize * 2, "int") ; iSubItem
      NumPut(&field, lvItem, A_PtrSize * 5, "ptr") ; pszText
      If (A_IsUnicode)
         DllCall("SendMessage", "uint", h, "uint", 4172, "uint", 0, "ptr", &lvItem) ; LVM_SETITEMW
      Else
         DllCall("SendMessage", "uint", h, "uint", 4171, "uint", 0, "ptr", &lvItem) ; LVM_SETITEMA
   }
}

; h = ListView handle.
; r = 1 based index position of the newly inserted row. Omit or 0 to add to bottom of list.
; c = 1 based index of the starting column.
; o = Multiple of one or more of the LVIS flags listed above.
; f* = Fields to fill each column. Each field will be inserted in subsequent order starting with the c param. This param is variadic. See: http://www.autohotkey.net/~Lexikos/AutoHotkey_L/docs/Functions.htm#Variadic
LVM_Insert(h, r=0, c=1, o=0, f*)
{
   c -= 1 ; convert to 0 based index
   If (r = 0)
      r := (DllCall("SendMessage", "uint", h, "uint", 4100, "uint", 0, "uint", 0) = -1 ? 0) + 1 ; LVM_GETITEMCOUNT
   VarSetCapacity(lvItem, A_PtrSize * 6, 0)
   NumPut(9, lvItem, "uint")   ; mask
   NumPut(r-1, lvItem, A_PtrSize, "int") ; iItem
   NumPut(o, lvItem, A_PtrSize * 3, "uint")  ; state
   NumPut(1, lvItem, A_PtrSize * 4, "uint")  ; stateMask
   DllCall("SendMessage", "uint", h, "uint", 4103, "uint", 0, "uint", &lvItem)   ; LVM_INSERTITEM
   For index,field in f
   {
      NumPut(c + A_Index - 1, lvItem, A_PtrSize * 2, "int") ; iSubItem
      NumPut(&field, lvItem, A_PtrSize * 5, "ptr") ; pszText
      If (A_IsUnicode)
         DllCall("SendMessage", "uint", h, "uint", 4172, "uint", 0, "ptr", &lvItem) ; LVM_SETITEMW
      Else
         DllCall("SendMessage", "uint", h, "uint", 4171, "uint", 0, "ptr", &lvItem) ; LVM_SETITEMA
   }
}


Last edited by Solar on Sat Mar 12, 2011 10:56 pm; edited 7 times in total
Back to top
View user's profile Send private message
Solar



Joined: 03 May 2009
Posts: 345
Location: OH, USA

PostPosted: Sun Sep 27, 2009 7:54 am    Post subject: Reply with quote

Bump for new version.

I recently needed this library again so I decided to add a new function and fix a couple problems.
Back to top
View user's profile Send private message
rootey



Joined: 06 Sep 2009
Posts: 20

PostPosted: Sun Sep 27, 2009 12:01 pm    Post subject: Reply with quote

Hi Solar, Could you also post a simple complete example, please?

Is this ListView handle library some that could help me with my current ListView problem here?:
http://www.autohotkey.com/forum/viewtopic.php?t=49305

For newbies like me, getting some advice from you more experienced guys, can save hours of pain/torture.
Back to top
View user's profile Send private message
Solar



Joined: 03 May 2009
Posts: 345
Location: OH, USA

PostPosted: Mon Sep 28, 2009 5:07 pm    Post subject: Reply with quote

I will post examples shortly. Until then, perhaps if I tried to better explain it:

I created this library to add direct control over a specific listview when using more than 1 listview control in your gui. If you haven't noticed while using the built-in functions, to function a listview control that wasn't the most recently added, you must first change the listview control to the "most recently added" before the built-in functions will work with it:

AHK Help wrote:
If the window has more than one ListView control, by default the functions operate upon the one most recently added. To change this, specify Gui, ListView, ListViewName, where ListViewName is the name of the ListView's associated variable or its ClassNN as shown by Window Spy.


The problem with this method occurs when you have thread interruptions causing the wrong listview to become the "most recently added" and writing data to the wrong control.

With my library, by passing the listview control's handle directly to each function, you can operate on your listview controls without worrying about interrupt problems.
Back to top
View user's profile Send private message
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

PostPosted: Sat Dec 26, 2009 7:38 pm    Post subject: Reply with quote

bump!

are there any updates or examples for this?
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
vinzz



Joined: 12 Jun 2009
Posts: 4

PostPosted: Sun Dec 27, 2009 11:59 am    Post subject: Reply with quote

this is nice.

I've made an custom xml editor in ahk for my webbpage and it has more than 1 listview.
My big problem was that some text / settings came in the wrong listviews with this code:
Code:
Gui, ListView, lCat
  LV_ModifyCol(1, 145)
  LV_ModifyCol(2, 0)
Gui, Listview, lProd
  lv_modifycol(4,0)
  lv_modifycol(2, "right")
  lv_modifycol(2, "AutoHdr")
  lv_modifycol(1, "AutoHdr")


I've solved that by using the line 'Gui, listview, Some_LV' before each executing line.
Code:
    Gui, ListView, lCat
    LV_ModifyCol(1, 145)
    Gui, ListView, lCat
    LV_ModifyCol(2, 0)
  Gui, Listview, lProd
  lv_modifycol(4,0)
  Gui, Listview, lProd
  lv_modifycol(2, "right")
  Gui, Listview, lProd
  lv_modifycol(2, "AutoHdr")
  Gui, Listview, lProd
  lv_modifycol(1, "AutoHdr")

I'll check out your functions and hopefully they work...
Back to top
View user's profile Send private message
kirak
Guest





PostPosted: Sat Mar 06, 2010 1:53 pm    Post subject: Reply with quote

i was searching for a library to handle more than one listview and came upon this

are there any updates for this or will there be any?
Back to top
Solar



Joined: 03 May 2009
Posts: 345
Location: OH, USA

PostPosted: Fri Mar 11, 2011 2:56 am    Post subject: Reply with quote

Recently needed this script again. Decided to update it to support AutoHotkey_L. See OP for changes.

I apologize for not making an example script for anyone interested in this. The truth is that I'm just too lazy. Sad
Back to top
View user's profile Send private message
shajul



Joined: 15 Sep 2006
Posts: 564

PostPosted: Fri Mar 11, 2011 11:44 am    Post subject: Reply with quote

making the last two functions variadic can make it sweeter
ref: Variadic Functions
_________________
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Fri Mar 11, 2011 2:28 pm    Post subject: Reply with quote

shajul wrote:
making the last two functions variadic can make it sweeter...


What's any less sweet about a variadic function call, which you can already use without the author re-inventing the wheel?

Code:
LVM_Modify(h,r,Object("o",0xF000,"f2","Lions","f3","Tigers","f5","Bears")*)

_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Solar



Joined: 03 May 2009
Posts: 345
Location: OH, USA

PostPosted: Fri Mar 11, 2011 5:19 pm    Post subject: Reply with quote

shajul wrote:
making the last two functions variadic can make it sweeter
ref: Variadic Functions


I was unaware of this functionality as I have just come back to AHK (and transitioned into AHK_L) again. This is a great idea and will most likely be implemented today.
Back to top
View user's profile Send private message
shajul



Joined: 15 Sep 2006
Posts: 564

PostPosted: Fri Mar 11, 2011 5:28 pm    Post subject: Reply with quote

sinkfaze wrote:
..

cool
_________________
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Solar



Joined: 03 May 2009
Posts: 345
Location: OH, USA

PostPosted: Fri Mar 11, 2011 6:14 pm    Post subject: Reply with quote

Updated to 1.0. See changes in OP.

Due to the removal of the "end column", your modify and insert function calls may need to be updated to only supply the starting column. This is the reason I made it version 1.0.
Back to top
View user's profile Send private message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Sat Mar 12, 2011 9:51 am    Post subject: Reply with quote

I want to make the insert function as fast as the build-in function. mcode may be a good idea, but I have not learned C or C++, who can help me?
Back to top
View user's profile Send private message
fragman



Joined: 13 Oct 2009
Posts: 1193

PostPosted: Sat Mar 12, 2011 3:13 pm    Post subject: Reply with quote

Please keep x64 compatibility in mind when using mcode.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group