Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear BG and longer labels for ListView Icons

Activex

Clear BG and longer labels for ListView Icons

by  wgcs  Posted    (Edited  )
The ListView control is great for displaying items much like windows explorer.

However, it suffers from some limitations that Windows Explorer also has such as:
The background for unselected icon labels is never clear.
The length of the labels on a listview icon in "Icon View" is limited by the system-wide "Icon spacing" setting in Windows.

ListView was designed so that these can both be changed, but the Common Controls ActiveX wrapper doesn't provide an interface for changing them.
You can provide your own interface in VFP7+ without much trouble, if you know how...

Here's How:
[ol][li]Create a subclass of the ListView ActiveX control:[/li]
[ul][li]Command Prompt: CREATE CLASS ?[/li]
[LI]give the class a name (say: myListBox)[/li]
[LI]select it to be based on "OLEControl"[/li]
[li]select a vcx class library file to store it in (or create a new one by entering a name that doesn't exist)[/li]
[li]click "ok"[/li]
[li]in the "Insert Object" dialog, select "Insert Control" and "Microsoft ListView Control 6.0", click "OK" [/li][/ul]
[li]In the Class Designer for the control you just created:[/li]
[ul][li]Choose "New Method" from the "Class" menu[/li]
[li]Enter "GetHWnd" for the new method name, Click "Add" then "Close"[/li]
[li]Open the "GetHwnd" method from the properties list, Enter this for the method code:
Code:
LOCAL lcHwnd1, lcHwnd2
lcHwnd2 = 0
DECLARE INTEGER FindWindowEx in win32api as apiFindWindowEx ;
    INTEGER nParent, INTEGER nChildAfter, ;
    STRING cClass, STRING cName
*If you're on VFP6, you'll have to find THISFORM's hWnd value a different way:
lcHwnd1 = apiFindWindowEx( THISFORM.HWnd, 0, 'CtlFrameWork_ReflectWindow', 0 )
IF lcHWnd1>0
  lcHwnd2 = apiFindWindowEx( lcHwnd1, 0, 'ListView20WndClass', 0 )
ENDIF
RETURN lcHwnd2
[/li]
[li]Using the steps above, add a method called "SetIconSpacing" and put this as its code:
Code:
LPARAMETERS tnCX, tnCY
*!*	LVM_FIRST=0x1000
*!*	LVM_SETICONSPACING = LVM_FIRST + 53  = 4149

LOCAL lnHWND, lnLPrm
lnHWND = THIS.GetHwnd()
IF lnHWND>0
  LVM_SETICONSPACING = 4149
  DECLARE LONG SendMessage IN WIN32API as apiSendMessage ;
    LONG whnd, LONG Msg, SHORT WParam, LONG LParam
  lnLPrm = BITLSHIFT(tnCX,16) + tnCY
  apiSendMessage( lnHWND, LVM_SETICONSPACING, 0, lnLPrm )
ENDIF
[/li]
[li]Using the steps above again, add a method called "SetTextBkColor" and put this as its code:
Code:
LPARAMETERS tnBkClr
*!*	LVM_FIRST=0x1000
*!*	LVM_SETTEXTBKCOLOR = LVM_FIRST + 38  = 4134

LOCAL lnHWND, lnLPrm, LVM_SETTEXTBKCOLOR, lnBkClr
lnHWND = THIS.GetHwnd()
IF lnHWND>0
  * If no color spec'd, default to clear:
  lnBkClr = IIF(VARTYPE(tnBkClr)='N', tnBkClr, 0xFFFFFFFF )
  LVM_SETTEXTBKCOLOR = 4134
  DECLARE LONG SendMessage IN WIN32API as apiSendMessage ;
    LONG whnd, LONG Msg, SHORT WParam, LONG LParam
  lnLPrm = BITLSHIFT(tnCX,16) + tnCY
  apiSendMessage( lnHWND, LVM_SETTEXTBKCOLOR, 0, lnBkClr )
ENDIF
[/li]
[/ul]
[/ol]

Now, you can use this control on a form. After putting it on the form, put calls to those two methods in the INIT event method of the form or of the listview control, something like this:
Code:
THISFORM.lstView.SetIconSpacing(100,100)
THISFORM.lstView.SetTextBkColor()  && Defaults to clear
You can also set a graphic for the "Picture" property to provide wallpaper on the listview, and to show that the background of the captions really is clear. Because of setting the Icon Spacing so big, the icons will space out better, and the text in the caption will be allowed to be longer before it gets truncated, ending with "...".

You can also set up properties for these settings on your ListView class which would be read and interpreted in the .Init event of the ListView, but that takes more code and is too complicated for illustrative purposes.

The same method can be used to toy with the Windows Desktop, to make those icons space out differently, or to make the Text Captions' backgrounds transparent.

Have Fun!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top