10

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

我想这与 WndProc 有关吗?

谢谢

4

4 回答 4

8

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

以下代码演示了如何:

lv.Columns[0].Width = lv.Width - 4 - SystemInformation.VerticalScrollBarWidth;
于 2015-05-04T20:22:45.057 回答
5

当前接受的答案是不安全的,因为它使堆栈不平衡。您应该对 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 在再次查看后在上面的评论中涵盖了这一点,所以我猜这里的格式都很好。


为了使用它:

# Use one of these valued for hwnd
long SB_HORZ = 0;
long SB_VERT = 1;
long SB_BOTH = 3;

# Use the actual name of the ListView control in your code here
# Hides the specified ListView scroll bar
ShowScrollBar(listView1.Handle.ToInt64(), SB_BOTH, 0);

要强制它显示而不是隐藏,只需bShow0to更改1,因为0equates tofalse1equates to true

于 2013-01-31T22:55:58.710 回答
4

你可以尝试这样的事情,我曾经在一个项目中使用过它并且它有效:

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

希望能帮助到你。

于 2010-12-17T02:16:54.967 回答
3

最好的解决方案是这里给出的公认答案:How to hide the vertical scroll bar in a .NET ListView Control in Details mode

它工作得很好,你不需要像列宽调整这样的技巧。此外,您在创建控件时禁用滚动条。

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

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

于 2016-10-19T14:23:14.697 回答