Solved

ListView spaces

Posted on 2003-11-22
7
1,458 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

12 Experts available now in Live!

Get 1:1 Help Now