Solved

ListView spaces

Posted on 2003-11-22
7
1,501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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