WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Dialog Boxes

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ListView UDFs by Guido


;Listview functions. Only for display data.
;Ver: 1.0
;Ref: http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/listview/reflist.asp

;To know:
;The listview will have at least one column, columns are 0 based.
;The first column can't be deleted.
;The elements of the 1st column are the items and are 0 based.
;Each item can have subitems, that are the elements of following columns,
;subitems are 1 based. 
;Guido 08/03

;---------------------------------------------------------------------
;lvinsertitem(hwlv,i,txt) : Inserts an item in a listview control at
;the specified index.
;---------------------------------------------------------------------
;hwlv : listview handle
;i    : 0 based index of the item. If this value is greater than the 
;       number of items, the new item will be appended to the end
;       and assigned the correct index.
;txt  : item text. A single string will be inserted as an item,
;       if you pass a @tab delimited list, the 1st list element will
;       be the item, and following elements will be inserted as subitems.
;----------------------------------------------------------------------
;Returns : 0 based index of the new inserted item or -1 if it fails
;----------------------------------------------------------------------
#definefunction lvinsertitem(hwlv,i,txt)
	user32=dllload(strcat(dirwindows(1),"user32.dll"))
	kernel32=dllload(strcat(dirwindows(1),"kernel32.dll"))

	LVM_FIRST=4096
	LVM_SETITEM=LVM_FIRST+6
	LVM_INSERTITEM=LVM_FIRST+7

	LVIF_TEXT=1

	LVITEM=binaryalloc(40)
	txtlen=strlen(txt)+1
	btxt=binaryalloc(txtlen)
	
	binarypoke4(LVITEM,0,LVIF_TEXT) ;mask
	binarypoke4(LVITEM,4,i) ;iItem
	
	;1st item
	binarypokestr(btxt,0,itemextract(1,txt,@tab))
  binarypoke4(LVITEM,8,0) ;iSubItem
	binarypoke4(LVITEM,20,intcontrol(42,btxt,0,0,0)) ;pszText
  r=dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_INSERTITEM,long:0,lpbinary:LVITEM)
	;set new item index, in case the LV used some sort style
	binarypoke4(LVITEM,4,r) ;iItem

	;subitems
	nsubit=itemcount(txt,@tab)
	if nsubit>1
		for x=2 to nsubit
			dllcall(kernel32,long:"RtlZeroMemory",lpbinary:btxt,long:txtlen)
			binarypokestr(btxt,0,itemextract(x,txt,@tab))
  		binarypoke4(LVITEM,8,x-1) ;iSubItem
		  dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_SETITEM,long:0,lpbinary:LVITEM)
		next
	endif

	binaryfree(LVITEM)
	binaryfree(btxt)
	dllfree(user32)
	dllfree(kernel32)
	return r
#endfunction

;--------------------------------------------------------------------
;lvsetitemtext(hwlv,item,subitem,txt) : Changes the text of an existing
;item or subitem.
;--------------------------------------------------------------------
;hwlv    : listview handle
;item    : 0 based item index
;subitem : 1 based subitem index
;txt     : item or subitem text
;--------------------------------------------------------------------
;Remarks : To change a full row(items and subitems) pass the item index
;          and 0 as a subitem, and pass a @tab delimited list of the items
;          and subitems, like in lvinsertitem().
;          To change an item pass the item index and 0 for the subitem.
;          To change a subitem pass the item an subitem indexs.
;---------------------------------------------------------------------
;Returns : No return value.
;---------------------------------------------------------------------  
#definefunction lvsetitemtext(hwlv,item,isubitem,txt)
	user32=dllload(strcat(dirwindows(1),"user32.dll"))
	kernel32=dllload(strcat(dirwindows(1),"kernel32.dll"))

	LVM_FIRST=4096
	LVM_SETITEM=LVM_FIRST+6
	
	LVIF_TEXT=1

	LVITEM=binaryalloc(40)
	txtlen=strlen(txt)+1
	btxt=binaryalloc(txtlen)
	
	binarypoke4(LVITEM,0,LVIF_TEXT) ;mask
	binarypoke4(LVITEM,4,item) ;iItem
	
	;1st item
	binarypokestr(btxt,0,itemextract(1,txt,@tab))
  binarypoke4(LVITEM,8,isubitem) ;iSubItem
	binarypoke4(LVITEM,20,intcontrol(42,btxt,0,0,0)) ;pszText
  dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_SETITEM,long:0,lpbinary:LVITEM)
	
	;subitems
	nsubit=itemcount(txt,@tab)
	if nsubit>1
		for x=2 to nsubit
			dllcall(kernel32,long:"RtlZeroMemory",lpbinary:btxt,long:txtlen)
			binarypokestr(btxt,0,itemextract(x,txt,@tab))
  		binarypoke4(LVITEM,8,x-1) ;iSubItem
		  dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_SETITEM,long:0,lpbinary:LVITEM)
		next
	endif

	binaryfree(LVITEM)
	binaryfree(btxt)
	dllfree(user32)
	dllfree(kernel32)
#endfunction

;--------------------------------------------------------------------
;lvdeleteitem(hwlv,item) : Removes an item(and its subitems if any)
;--------------------------------------------------------------------
;hwlv : listview handle
;item : index of the item to delete
;--------------------------------------------------------------------
;Returns : @true if succesful or @false otherwise
;--------------------------------------------------------------------
#definefunction lvdeleteitem(hwlv,item)
	user32=strcat(dirwindows(1),"user32.dll")
	LVM_DELETEITEM=4096+8
	return dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_DELETEITEM,long:item,long:0)	
#endfunction

;----------------------------------------------------------------------
;lvdeleteall(hwlv) : Removes all items from the listview.
;----------------------------------------------------------------------
;hwlv : listview handle
;----------------------------------------------------------------------
;Returns : @TRUE if successful, or @FALSE otherwise
;----------------------------------------------------------------------
#definefunction lvdeleteall(hwlv)
	user32=strcat(dirwindows(1),"user32.dll")
	LVM_DELETEALLITEMS=4096+9
	return dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_DELETEALLITEMS,long:0,long:0)	
#endfunction

;----------------------------------------------------------------------
;lvgetitemcount(hwlv) : Retrieves the number of items in a listview. 
;----------------------------------------------------------------------
;hwlv : listview handle
;----------------------------------------------------------------------
;Returns : number of items
;----------------------------------------------------------------------
#definefunction lvgetitemcount(hwlv)
	user32=strcat(dirwindows(1),"user32.dll")
	LVM_GETITEMCOUNT=4096+4 
	return dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_GETITEMCOUNT,long:0,long:0)	
#endfunction

;-------------------------------------------------------------------
;lvinsertcolumn(hwlv,ci,cwidth,ctxt) : Inserts a column.
;-------------------------------------------------------------------
;hwlv   : listview handle
;ci     : column index, 1st column is at index 0, the 1st column is
;         automatically inserted by lvcreate() 
;cwidth : column width
;ctxt   : column header text
;-------------------------------------------------------------------
;Returns : the index of the new column or -1 if it fails
;------------------------------------------------------------------- 
#definefunction lvinsertcolumn(hwlv,ci,cwidth,ctxt)
	user32=strcat(dirwindows(1),"user32.dll")	

	LVM_FIRST=4096
	LVM_INSERTCOLUMN=LVM_FIRST+27
	LVCF_FMT=1
  LVCF_WIDTH=2
	LVCF_TEXT=4
  LVCF_SUBITEM=8
	LVCFMT_LEFT=0

	LVCOLUMN=binaryalloc(32)
	bctxt=binaryalloc(strlen(ctxt)+1)
	binarypokestr(bctxt,0,ctxt)

	;column
	binarypoke4(LVCOLUMN,0,LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM) ;mask
	binarypoke4(LVCOLUMN,4,LVCFMT_LEFT) ;fmt
	binarypoke4(LVCOLUMN,8,cwidth) ;cx
	binarypoke4(LVCOLUMN,12,intcontrol(42,bctxt,0,0,0)) ;pszText
	binarypoke4(LVCOLUMN,20,ci) ;iSubItem

	r=dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_INSERTCOLUMN,long:ci,lpbinary:LVCOLUMN)
	
	binaryfree(LVCOLUMN)
	binaryfree(bctxt)
	return r
#endfunction

;---------------------------------------------------------------------
;lvdeletecolumn(hwlv,ci) : Removes a column from a listview.
;---------------------------------------------------------------------
;hwlv : listview handle
;ci   : index of column to delete
;---------------------------------------------------------------------
;Returns : @TRUE if successful, or @FALSE otherwise
;---------------------------------------------------------------------
;Remarks : Column zero of the list-view control cannot be deleted. 
;(msdn)    If you must delete column zero, insert a zero length dummy 
;          column zero and delete column one and above.
;---------------------------------------------------------------------
#definefunction lvdeletecolumn(hwlv,ci)
	user32=strcat(dirwindows(1),"user32.dll")
	LVM_DELETECOLUMN=4096+28
	return dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_DELETECOLUMN,long:ci,long:0)
#endfunction

;----------------------------------------------------------------------
;lvcreate(hdlg,x,y,w,h,coltxt,colwidth,style,exstyle) : Creates a listview
;control.
;----------------------------------------------------------------------
;hdlg : dialog handle
;x,y  : position in pixels inside the dialog
;w,h  : width and height in pixels
;style    : listview style, look at msdn, some values:
;   				LVS_SHOWSELALWAYS=8
;					  LVS_REPORT=1
;           LVS_SORTASCENDING=16
;exstyle  : extended listview style look at msdn for this values
;           some values: LVS_EX_GRIDLINES=1
;                        LVS_EX_INFOTIP=1024
;                        LVS_EX_FULLROWSELECT=32                                  
;----------------------------------------------------------------------
;Returns : listview handle
;----------------------------------------------------------------------
#definefunction lvcreate(hdlg,x,y,w,h,style,exstyle)
	user32=strcat(dirwindows(1),"user32.dll")
	WS_CHILD=1073741824
	WS_VISIBLE=268435456
	WS_EX_CLIENTEDGE=512

	LVM_FIRST=4096
	LVM_SETEXTENDEDLISTVIEWSTYLE=LVM_FIRST+54

	hwlv=dllcall(user32,long:"CreateWindowExA",long:WS_EX_CLIENTEDGE,lpstr:"SysListView32",lpstr:"",long:WS_CHILD|WS_VISIBLE|style,long:x,long:y,long:w,long:h,long:hdlg,long:0,long:dllhinst(""),long:0)
	;extended style
	if exstyle then dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_SETEXTENDEDLISTVIEWSTYLE,long:exstyle,long:exstyle)
	
	return hwlv
#endfunction

;---------------------------------------------------------------------
;lvsetextendedstyle(hwlv,style,styleval) : Sets extended styles.
;---------------------------------------------------------------------
;hwlv  : listview handle
;style : specifies which styles in styleval are to be affected. 
;        This parameter can be a combination of Extended List-View Styles.
;				 Only the extended styles in styleval will be changed.
;        All other styles will be maintained as they are. If this 
;        parameter is 0, all of the styles in styles will be affected.
;styleval : value that specifies the extended listview control style.
;----------------------------------------------------------------------
;Returns : the previous extended list-view control styles.
;----------------------------------------------------------------------  
#definefunction lvsetextendedstyle(hwlv,style,styleval)
	user32=strcat(dirwindows(1),"user32.dll")
	LVM_SETEXTENDEDLISTVIEWSTYLE=4096+54
	return dllcall(user32,long:"SendMessageA",long:hwlv,long:LVM_SETEXTENDEDLISTVIEWSTYLE,long:style,long:styleval)
#endfunction	

;---------------------------------------------------------
;TEST
;--------------------------------------------------------- 
;DIALOG CALLBACK
#definesubroutine dlgproc(hdlg,msg,id,p4,p5)
	select msg	
		case 0 ;Init	
			;api const
			LVS_EX_GRIDLINES=1
			LVS_EX_INFOTIP=1024
 
			LVS_REPORT=1
      LVS_SORTASCENDING=16

			;create listview
			hwlv=lvcreate(hdlg,10,10,500,300,LVS_REPORT|LVS_SORTASCENDING,LVS_EX_GRIDLINES|LVS_EX_INFOTIP)
			for x=0 to 9 ;10 columns 
				lvinsertcolumn(hwlv,x,50,"Col %x%")
			next

			;50 items
			for x=0 to 49
				;make item
				subs=""
				for y=1 to 9
					subs=iteminsert("sub%x%-%y%",-1,subs,@tab)
				next
				item=iteminsert("item%x%",0,subs,@tab)
				lvinsertitem(hwlv,x,item)
			next
			
			DialogProcOptions(hdlg,2,1) ;Push buttons
  		break

		case 2 ;button pushed
			select id
				case 1 ;Ok , remove grid
					lvsetextendedstyle(hwlv,LVS_EX_GRIDLINES,0)
					;lvsetitemtext(hwlv,3,3,"hello")
					return -2

				case 2 ;Cancel , exit
					return -1
			endselect
	endselect
	return -2
#endsubroutine

MyDialogFormat=`WWWDLGED,6.1`

MyDialogCaption=`ListView`
MyDialogX=-1
MyDialogY=-1
MyDialogWidth=318
MyDialogHeight=184
MyDialogNumControls=002
MyDialogProcedure=`dlgproc`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=0

MyDialog001=`081,163,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`201,163,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("MyDialog")
;END SCRIPT

Article ID:   W16232
File Created: 2004:03:30:15:43:30
Last Updated: 2004:03:30:15:43:30