ListView spaces

Hello.

I have a problem with a ListView. My ListView has a vsIcon style and I want to reduce the space between icons. The default distance is a little bigger for my needs.

Thank you,
Winexec
LVL 1
winexecAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hacibumbalaCommented:
You may set the OwnerDraw property of the ListView to True and draw all the items by yourself on OnCustomDrawItem event.
HypoviaxCommented:
How about you switch to smallicons as the view style. This will reduce he space in between.

Regards,

Hypoviax
winexecAuthor Commented:
@hacibumbala: Do you have a small example on how to do that?

@Hypoviax: Yes, but I want to use Large Icons.

Thx.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

hacibumbalaCommented:
Below code does not work perfectly but may give you an idea of what I am talking about. You have to work more on that. Hope it helps.

DFM Code:

// -----
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object List: TListView
    Left = 8
    Top = 8
    Width = 673
    Height = 393
    Columns = <>
    OwnerDraw = True
    TabOrder = 0
    OnCustomDrawItem = ListCustomDrawItem
  end
  object Button1: TButton
    Left = 8
    Top = 408
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end
// -----

PAS Code:

// -----
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    List: TListView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ListCustomDrawItem(Sender: TCustomListView; Item: TListItem;
      State: TCustomDrawState; var DefaultDraw: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Item: TListItem;
begin
  // Item 1
  Item := List.Items.Add;
  Item.Caption := 'File';
  Item.ImageIndex := 3;
  // Item 2
  Item := List.Items.Add;
  Item.Caption := 'Folder';
  Item.ImageIndex := 4;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  sfi: TSHFileInfo;
begin
  List.LargeImages := TImageList.Create(Self);
  List.LargeImages.Handle := SHGetFileInfo('', 0, sfi, SizeOf(sfi),
    SHGFI_SYSICONINDEX or SHGFI_LARGEICON);
  List.LargeImages.ShareImages := True;
  List.OwnerDraw := True;
end;

procedure TForm1.ListCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  Rect: TRect;
  y: Integer;
begin
  with Sender as TListView do
  begin
    DefaultDraw := False; // Disabled default drawing.
    // Draw image
    Rect := Item.DisplayRect(drIcon);
    if Item.Index > 0 then
    begin
      Dec(Rect.Left, 32);
      Dec(Rect.Right, 32);
    end;
    y := Rect.Top + (Rect.Bottom - Rect.Top) div 2;
    LargeImages.Draw(Canvas, Rect.Left, y - LargeImages.Height div 2,
      Item.ImageIndex, True);
    // Draw caption
    Rect := Item.DisplayRect(drLabel);
    if Item.Index > 0 then
    begin
      Dec(Rect.Left, 32);
      Dec(Rect.Right, 32);
    end;
    Canvas.TextOut(Rect.Left, Rect.Top, Item.Caption);
  end;
end;

end.

// -----

Eddie ShipmanAll-around developerCommented:
FROM MSDN:

LVM_SETICONSPACING Message
--------------------------------------------------------------------------------
Sets the spacing between icons in list-view controls that have the LVS_ICON style.
You can send this message explicitly or by using the ListView_SetIconSpacing macro.

uses ..CommCtrl;

.
.
.
begin
  Listview1.Items.BeginUpdate;
  ListView_SetIconSpacing(ListView1.Handle; 4, 4);
  Listview1.Items.EndUpdate;
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerCommented:
FYI...More from MSDN LVM_SETICONSPACING...

Remarks

Values for cx and cy are relative to the upper-left corner of an icon bitmap. Therefore,
to set spacing between icons that do not overlap, the cx or cy values must include the
size of the icon, plus the amount of empty space desired between icons. Values that
do not include the width of the icon will result in overlaps.

When defining the icon spacing, cx and cy must set to 4 or larger. Smaller values will
not yield the desired layout. To reset cx and cy to the default spacing, set the lParam
value to -1.

winexecAuthor Commented:
@hacibumbala: The approach is interesting, but there are few problems with icons clicking etc.

@EddieShipman: Yes, that worked perfectly.

Thank you both :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.