Search code examples
c#.netwinformslistviewlistviewitem

How can I remove the selection border on a ListViewItem


I'm using SetWindowTheme and SendMessage to make a .net listview look like a vista style listview, but the .net control still has a dotted selection border around the selected item:

listview

Selected items in the explorer listview don't have that border around them. How can I remove it?

Windows Explorer:

windows explorer

Edit: Solution:

public static int MAKELONG(int wLow, int wHigh)
{
    int low = (int)LOWORD(wLow);
    short high = LOWORD(wHigh);
    int product = 0x00010000 * (int)high;
    int makeLong = (int)(low | product);
    return makeLong;
}

SendMessage(olv.Handle, WM_CHANGEUISTATE, Program.MAKELONG(UIS_SET, UISF_HIDEFOCUS), 0);

Solution

  • Telanors solution worked for me. Here's a slightly more self-contained version.

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    public class MyListView : ListView
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
        private const int WM_CHANGEUISTATE = 0x127;
        private const int UIS_SET = 1;
        private const int UISF_HIDEFOCUS = 0x1;
    
        public MyListView()
        {
            this.View = View.Details;
            this.FullRowSelect = true;
    
            // removes the ugly dotted line around focused item
            SendMessage(this.Handle, WM_CHANGEUISTATE, MakeLong(UIS_SET, UISF_HIDEFOCUS), 0);
        }
    
        private int MakeLong(int wLow, int wHigh)
        {
            int low = (int)IntLoWord(wLow);
            short high = IntLoWord(wHigh);
            int product = 0x10000 * (int)high;
            int mkLong = (int)(low | product);
            return mkLong;
        }
    
        private short IntLoWord(int word)
        {
            return (short)(word & short.MaxValue);
        }
    }