Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

[VB6] Dynamic Resize: Use a slider to change ListView icon/thumbnail size on the fly

$
0
0

One of the features of the Vista and newer Explorer views is the icon size slider; you can do more than just pick between a couple sizes- you can set it to any value in the icon range. Previously to do this in VB was quite a lot of work; you'd have to manually resize each image and rebuild each ImageList since you can't scale up without quality loss... so it's not something that could be rapidly changed without any lag. This project, however, takes advantage of a feature of the new IImageList2 COM interface: it has a .Resize command that can scale down the entire ImageList at once with the speed of Windows API. To avoid quality loss, we load the maximum size images into a primary ImageList, then we dynamically generate the API-made duplicate in the smaller size that the user is looking for, always scaling down instead of up.

Right now this project is focused on standard image file thumbnails; really small images that need to be grey-boxed and standard file icons will be addressed in a future version of this demo.

Here's the key function:
Code:

Private Sub ResizeThumbView(cxNew As Long)
ImageList_Destroy himl
himl = ImageList_Duplicate(himlMax)
HIMAGELIST_QueryInterface himl, IID_IImageList2, pIML
If (pIML Is Nothing) = False Then
    pIML.Resize cxNew, cxNew
End If
himl = ObjPtr(pIML)
bSetIML = True
ListView_SetImageList hLVS, himl, LVSIL_NORMAL
bSetIML = False
ListView1.Refresh
End Sub

While it's certainly possible to forgo the standard HIMAGELIST and entirely use IImageList, I wanted to retain some (hopefully) more familiar territory by using that and the 5.0 VB ListView control. As the API HIMAGELIST_QueryInterface indicates, they're pretty much interchangable anyway, as the ObjPtr returns the same handle as when we made it with ImageList_Create.

Requirements
-Windows Vista or newer
-Common Controls 6.0 Manifest - The demo project has a manifest built into its resource file. Your IDE may have to be manifested to run it from there. If you need to manifest your IDE or a new project, see LaVolpe's Manifest Creator
-oleexp.tlb v4.0 or newer - Only needed in the IDE; not needed once compiled.
-oleexp addon mIID.bas - Included in the oleexp download. Must be added to the demo project the first time you open it.

Scrolling
To make it truly like Explorer, where it sizes while you move the mouse, you can move the code in Slider1_Change over to Slider1_Scroll:
Code:

Private Sub Slider1_Change()
'cxThumb = Slider1.Value
'Label1.Caption = cxThumb & "x" & cxThumb
'ResizeThumbView cxThumb
End Sub

Private Sub Slider1_Scroll()
cxThumb = Slider1.Value
Label1.Caption = cxThumb & "x" & cxThumb
ResizeThumbView cxThumb
End Sub

It works perfectly with the small number of images currently there, but I'm hesitant to trust the stability if there's hundreds or thousands of list items, at least without it being a virtual ListView. I'll take a look at it for future versions; if anyone experiments with it before then let me know! :)
Attached Files

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>