Chris Wearn wrote:
I have a need to insert images into subitems of a listview control in columns other than the first.

It is relatively easy to add images to the Listview column header using:

$lvwCtrl->InsertColumn(-index => 0, -subitem => $i, -text => “”, -image => 0); # where -image references an imagelist item

However there seems to be no provision to add images to InsertItem that I can find;

Or is this already possible and I have misinterpreted the documentation for Win32-GUI?

Not something I had looked at before with Win32::GUI, but knew it was possible with a standard listview control.

The sample below shows it working, although the Win32::GUI::Listview API is a bit hit or miss in this respect. (you may need to tweak it a bit to run on older Win32::GUI versions)

(1) You need to add the -subitemimages => 1 option to the Listview constructor. (2) You need to insert the main items using InsertItem - don't try to use the arrayref syntax for the text option, unless you want your subitems to have the same images as the 'main' item. (3) Then use SetItem, using -index and -subitem options to insert the text and set the imagelist reference for each subitem (column).

Good luck. I guess this is another area to add to my ever growing list of bits that need looking at.

Regards,
Rob.

#!perl -w
use strict;
use warnings;

use Win32::GUI 1.03_04, qw(ILC_COLOR ILC_COLOR24 ILC_MASK);
use Win32::GUI::BitmapInline();

# the image list created has the following indexes:
#  0 - red icon
#  1 - green icon
#  2 - blue icon
my $il = create_image_list();

my $mw = Win32::GUI::Window->new(
    -title => "ListView - Subitem icons",
    -size  => [400,300],
);

$mw->AddListView(
    -name   => "LV",
    -width  => $mw->ScaleWidth(),
    -height => $mw->ScaleHeight(),
    -report => 1,
    -subitemimages => 1,
    -imagelist => $il,
);

for my $col_name ("Title", "Card Number", "Shelf Location") {
    $mw->LV->InsertColumn(
        -text  => $col_name,
        -width => 100,
    );
}

my @items = (
    [ "Green Eggs and Ham",              [ "JF SEU",  "Children's" ], ],
    [ "A Brief History of the Universe", [ "897.112", "2nd Floor"  ], ],
    [ "Your Book Title Here",            [ "501.2",   "Vault"      ], ],
);

foreach my $item (@items) {
    my $index = $mw->LV->InsertItem(
        -text => $item->[0],
        -image => 0,
    );
    my $subindex = 1;
    foreach my $subtext (@{$item->[1]}) {
            $mw->LV->SetItem(
                -index   => $index,
                -text    => $subtext,
                -subitem => $subindex++,
                -image   => int(rand(2)) + 1,
            );
    }
}

$mw->Show();
Win32::GUI::Dialog();
exit(0);

sub create_image_list
{

    my $red_icon = Win32::GUI::BitmapInline->newIcon( q(
AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAAAAAAEgAAABIAAAAEAAA
AAAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAICAgAAAAP8AAP8AAAD//wD/AAAA
/wD/AP//AAD///8AAAAAAAAAAAAAAAAJkAAAAAAAmZmZmQAAAAmZmZmZkAAAmZmZmZmZAACZmZmZ
mZkACZmZmZmZmZAJmZmZmZmZkAmZmZmZmZmQAJmZmZmZmQAAmZmZmZmZAACZmZmZmZkAAAmZmZmZ
kAAAAJmZmZkAAAAAAAmZAAAAAAAAAAAAAAD+fwAA8A8AAMADAADAAwAAgAEAAIABAAAAAAAAAAAA
AAAAAACAAQAAgAEAAIABAADAAwAAwAMAAPAPAAD+PwAA
) );


    my $green_icon = Win32::GUI::BitmapInline->newIcon( q(
AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAAAAAAEgAAABIAAAAEAAA
AAAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAICAgAAAAP8AAP8AAAD//wD/AAAA
/wD/AP//AAD///8AAAAAAAAAAAAAAAAKoAAAAAAAqqqqqgAAAAqqqqqqoAAAqqqqqqqqAACqqqqq
qqoACqqqqqqqqqAKqqqqqqqqoAqqqqqqqqqgAKqqqqqqqgAAqqqqqqqqAACqqqqqqqoAAAqqqqqq
oAAAAKqqqqoAAAAAAAqqAAAAAAAAAAAAAAD+fwAA8A8AAMADAADAAwAAgAEAAIABAAAAAAAAAAAA
AAAAAACAAQAAgAEAAIABAADAAwAAwAMAAPAPAAD+PwAA
) );

    my $blue_icon = Win32::GUI::BitmapInline->newIcon( q(
AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAAAAAAEgAAABIAAAAEAAA
AAAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAICAgAAAAP8AAP8AAAD//wD/AAAA
/wD/AP//AAD///8AAAAAAAAAAAAAAAAMwAAAAAAAzMzMzAAAAAzMzMzMwAAAzMzMzMzMAADMzMzM
zMwADMzMzMzMzMAMzMzMzMzMwAzMzMzMzMzAAMzMzMzMzAAAzMzMzMzMAADMzMzMzMwAAAzMzMzM
wAAAAMzMzMwAAAAAAAzMAAAAAAAAAAAAAAD+fwAA8A8AAMADAADAAwAAgAEAAIABAAAAAAAAAAAA
AAAAAACAAQAAgAEAAIABAADAAwAAwAMAAPAPAAD+PwAA
) );

    my $image_list = Win32::GUI::ImageList->new(
        16, 16,
        ILC_COLOR|ILC_COLOR24|ILC_MASK,
        3, 0
    );

    $image_list->AddIcon($red_icon);
    $image_list->AddIcon($green_icon);
    $image_list->AddIcon($blue_icon);

    return $image_list;
}
__END__

Reply via email to