Solved

ListView spaces

Posted on 2003-11-22
7
1,469 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filemaker 14 vs Delphi Embarcadero 7 186
Virtuailstring tree add node to another virtuailstring tree list 4 106
find a node in VST 2 64
Working with hours 3 46
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now