如何删除ListViewItem上的选择边框

我正在使用SetWindowTheme和SendMessage使.net listview看起来像vista样式列表视图,但.net控件仍然在所选项目周围有一个虚线选择边框:

列表显示

资源管理器列表视图中的选定项目周围没有该边框。 我该如何删除它?

Windows资源管理器:

Windows资源管理器

编辑:解决方案:

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); 

Telanors解决方案为我工作。 这是一个稍微更自包含的版本。

 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); } } 

将ListView.ShowFocusCues属性设置为false有帮助吗?

将HotTracking属性设置为true会隐藏焦点矩形。 这在我的Win7机器上重新编写了Explorer风格:

 using System; using System.Windows.Forms; using System.Runtime.InteropServices; class MyListView : ListView { public MyListView() { this.HotTracking = true; } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); SetWindowTheme(this.Handle, "explorer", null); } [DllImport("uxtheme.dll", CharSet = CharSet.Auto)] public extern static int SetWindowTheme(IntPtr hWnd, string appname, string subidlist); } 

请注意,将项目加下划线是一种副作用。

这样做NON P / Invoke方式……

覆盖ListView控件并添加以下内容:

 protected override void OnSelectedIndexChanged(EventArgs e) { base.OnSelectedIndexChanged(e); Message m = Message.Create(this.Handle, 0x127, new IntPtr(0x10001), new IntPtr(0)); this.WndProc(ref m); } protected override void OnEnter(EventArgs e) { base.OnEnter(e); Message m = Message.Create(this.Handle, 0x127, new IntPtr(0x10001), new IntPtr(0)); this.WndProc(ref m); } 

似乎没有使用Windows窗体更改ListViewItem样式的特定方法。

有时无法使用托管代码更改某些Win32控件行为。 唯一的方法是做一些P / Invoke来修改特定的行为。 我觉得这很棘手但你别无选择。 在开发Windows Mobile UI时(通常使用ListView),我经常遇到这种情况。

所以我没有直接回答你的问题,但我很确定如果不能使用Windows Forms,你肯定可以使用P / Invoke。 我能给你的唯一线索:

  • 平台调用教程
  • 列表视图文档

我知道这是相当陈旧的,Windows Forms现在已经过时,但它仍然在使用,它仍然是一个问题。 更糟糕的是,这些解决方案都不优雅,有些甚至根本不起作用。

这是一个非常简单的解决方案,当您创建自己的inheritanceListView的控件时,只需覆盖WndProc以永不允许焦点。 它摆脱了所有与焦点相关的虚线选择框,项目选择,子项选择等…

 using System.Windows.Forms; public partial class NoSelectionListView : ListView { public NoSelectionListView() { InitializeComponent(); } protected override void WndProc(ref Message m) { if (m.Msg == 0x0007) //WM_SETFOCUS { return; } base.WndProc(ref m); } }