Solved

ListView spaces

Posted on 2003-11-22
7
1,479 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:winexec
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:hacibumbala
ID: 9807517
You may set the OwnerDraw property of the ListView to True and draw all the items by yourself on OnCustomDrawItem event.
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 9807843
How about you switch to smallicons as the view style. This will reduce he space in between.

Regards,

Hypoviax
0
 
LVL 1

Author Comment

by:winexec
ID: 9808685
@hacibumbala: Do you have a small example on how to do that?

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

Thx.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 1

Expert Comment

by:hacibumbala
ID: 9809580
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.

// -----

0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 50 total points
ID: 9811174
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;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9811189
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.

0
 
LVL 1

Author Comment

by:winexec
ID: 9816092
@hacibumbala: The approach is interesting, but there are few problems with icons clicking etc.

@EddieShipman: Yes, that worked perfectly.

Thank you both :)
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question