I have a System.Windows.Forms.ListView
with icons and empty string labels. Here is the code that creates and fills the list view:
listView1 = new System.Windows.Forms.ListView();
listView1.BackColor = System.Drawing.Color.Blue;
listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
listView1.HideSelection = false;
listView1.Location = new System.Drawing.Point(93, 66);
listView1.MultiSelect = false;
listView1.Name = "listView1";
listView1.ShowItemToolTips = true;
listView1.Size = new System.Drawing.Size(434, 332);
listView1.TabIndex = 0;
listView1.UseCompatibleStateImageBehavior = false;
var images = Directory.GetFiles
(@"my path")
.Where((where) => Path.GetExtension(where)
.Equals(".PNG", StringComparison.CurrentCultureIgnoreCase))
.Select((selected)
=> Image.FromFile(selected));
var il = new ImageList();
il.ImageSize = new Size(75, 75);
il.Images.AddRange(images.ToArray());
listView1.LargeImageList = il;
for (int i = 0; i < il.Images.Count; i++)
{
var lvi = new ListViewItem("", i)
{
ToolTipText = i.ToString()
};
listView1.Items.Add(lvi);
}
Controls.Add(listView1);
Now I'm using the technique described in many places, like here, to set the list view item image spacing:
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
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));
}
The problem is that the hit test area still includes the label, even though there is no label, so the wrong item is returned by the hit test and the wrong tool tip is displayed when the mouse is over where the label of the adjacent item would be:
Here is what the "wrong" hit test looks like.
private void ListView1_MouseDown(object sender, MouseEventArgs e)
{
var hti = listView1.HitTest(e.Location);
}
Is there a way to remove the label from a list view item so the hit test area only includes the image? Or am I forced to go the OwnerDraw = true;
route (which I understand is very tricky and/or impossible)?