C# - ListView Item Spacing (Padding)

I'm perplexed at the reasoning that the ListView control, has no direct spacing/padding property. I am utilizing the ListView in the capacity of images, using the View state, View.LargeIcon. There is a method to modify the spacing/padding between the ListViewItem(s).

Initially, you'll need to add:
1
using System.Runtime.InteropServices;
Next, you'll need to import the DLL, so that you can utilize SendMessage, to modify the ListView parameters.
1
2
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
Once completed, create the following two functions/methods:
1
2
3
4
5
6
7
8
9
10
11
public int MakeLong(short lowPart, short highPart)
{
     return (int)(((ushort)lowPart) | (uint)(highPart << 16));
}
  
public void ListViewItem_SetSpacing(ListView listview, short leftPadding, short topPadding)
{    
    const int LVM_FIRST = 0x1000;    
    const int LVM_SETICONSPACING = LVM_FIRST + 53;    
    SendMessage(listview.Handle, LVM_SETICONSPACING, IntPtr.Zero, (IntPtr)MakeLong(leftPadding, topPadding));     
}
Then to use the function, just pass in your ListView, and set the values. In the example, 64 pixels is the image width, and 32 pixels is my horizontal spacing/padding, 100 pixels is the image height, and 16 pixels is my vertical spacing/padding, and both parameters require a minimum of 4 pixels.
1
ListViewItem_SetSpacing(this.listView, 64 + 32, 100 + 16);

Comments

Anonymous said…
thnx u
Anonymous said…
concise and useful
Anonymous said…
Worked like a charm.
Anonymous said…
Greate!
Anonymous said…
nice

Popular posts from this blog

C# - Performance Counters

ASP.NET MVC - Disabling Browser Link