【发布时间】:2011-05-26 21:40:01
【问题描述】:
有没有办法阻止水平滚动条出现在列表视图中?我希望垂直滚动条在需要时显示,但我希望水平滚动条永远不会出现。
我想这与 WndProc 有关系吗?
谢谢
【问题讨论】:
标签: c# winforms horizontal-scrolling wndproc
有没有办法阻止水平滚动条出现在列表视图中?我希望垂直滚动条在需要时显示,但我希望水平滚动条永远不会出现。
我想这与 WndProc 有关系吗?
谢谢
【问题讨论】:
标签: c# winforms horizontal-scrolling wndproc
currently accepted answer 不安全,因为它使堆栈不平衡。您应该对 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);
要强制它显示而不是隐藏,只需将bShow从0更改为1,因为0等于false和1 等于true。
【讨论】:
有一种更简单的方法可以消除下部滚动条并垂直显示。它包括确保标题,如果没有标题,则行是 listview.Width - 4 的宽度,如果显示垂直滚动条,则 listview.Width - Scrollbar.Width - 4;
以下代码演示了如何:
lv.Columns[0].Width = lv.Width - 4 - SystemInformation.VerticalScrollBarWidth;
【讨论】:
Width 更改为 width 或 widthValue 之类的东西,以澄清答案是' t 指的是 UI 组件/控件类中的this.Width?我几乎自己编辑了这封信的案例,但感觉 OP 应该验证这是意图。
最佳解决方案是此处给出的公认答案:How to hide the vertical scroll bar in a .NET ListView Control in Details mode
它可以完美运行,您不需要调整列宽等技巧。此外,您在创建控件时禁用滚动条。
缺点是您必须创建自己的从System.Windows.Forms.ListView 派生的列表视图类来覆盖WndProc。但这是要走的路。
要禁用水平滚动条,请记住使用WS_HSCROLL 而不是WS_VSCROLL(在链接答案中使用)。 WS_HSCROLL 的值为 0x00100000。
【讨论】:
你可以试试这样的东西,我在一个项目中使用过一次,它成功了:
[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);
}
希望对你有帮助。
【讨论】:
IntPtr hwnd 作为 P/Invoke 方法的第一个参数,而不是 long hwnd。
long 是 8 个字节,而例如对于 32 位进程,所有参数都需要为 4 字节。这只是运气好,因为第二个和第三个参数为零。