C#ListView禁用水平滚动条

有没有办法阻止水平滚动条出现在列表视图中? 我希望垂直滚动条在需要时显示,但我希望水平滚动条永远不会显示。

我想这会和WndProc有关吗?

谢谢

你可以尝试这样的东西,我在一个项目中使用过一次,它有效:

[DllImport ("user32")] private static extern long ShowScrollBar (long hwnd , long wBar, long bShow); long SB_HORZ = 0; long SB_VERT = 1; long SB_BOTH = 3; private void HideHorizontalScrollBar () { ShowScrollBar(listView1.Handle.ToInt64(), SB_HORZ, 0); } 

希望能帮助到你。

有一种更简单的方法来消除下滚动条并具有垂直显示。 它包括确定标题,如果没有标题,则行是listview.Width - 4的宽度listview.Width - 4 ,如果显示垂直滚动条,则listview.Width - Scrollbar.Width - 4;

以下代码演示了如何:

 lv.Columns[0].Width = Width - 4 - SystemInformation.VerticalScrollBarWidth; 

@ bennyyboi的答案是不安全的,因为它会使堆栈失衡。 您应该使用以下代码代替DllImport:

 [System.Runtime.InteropServices.DllImport("user32", CallingConvention=System.Runtime.InteropServices.CallingConvention.Winapi)] [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] private static extern bool ShowScrollBar(IntPtr hwnd, int wBar, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] bool bShow); 

Andreas Reiff在再看一遍之后在上面的评论中介绍了这一点,所以我猜这里的格式很好。

最佳解决方案是此处给出的接受答案: 如何在详细信息模式下隐藏.NET ListView控件中的垂直滚动条

它完美地工作,你不需要一些技巧,如列宽调整。 此外,您在创建控件时右键禁用滚动条。

缺点是您必须创建自己的列表视图类,该类派生自System.Windows.Forms.ListView以覆盖WndProc 。 但这是要走的路。

要禁用水平滚动条,请记住使用WS_HSCROLL而不是WS_VSCROLL (在链接的答案中使用)。 WS_HSCROLL值为0x00100000