Ozz*_*zzy 9 c# wndproc horizontal-scrolling winforms
有没有办法阻止水平滚动条出现在列表视图中?我希望垂直滚动条在需要时显示,但我希望水平滚动条永远不会显示.
我想这会和WndProc有关吗?
谢谢
小智 9
有一种更简单的方法可以消除下滚动条并具有垂直显示.它包括确定标题,如果没有标题,则行是宽度,listview.Width - 4如果显示垂直滚动条listview.Width - Scrollbar.Width - 4;
以下代码演示了如何:
lv.Columns[0].Width = Width - 4 - SystemInformation.VerticalScrollBarWidth;
Run Code Online (Sandbox Code Playgroud)
@ 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);
Run Code Online (Sandbox Code Playgroud)
Andreas Reiff在再看一遍之后在上面的评论中介绍了这个,所以我猜这里的格式很好.
最好的解决方案是此处给出的已接受答案:How to hide the Vertical Scroll Bar in a .NET ListView Control in Details mode
它工作得很好,你不需要像调整列宽这样的技巧。此外,您可以在创建控件时禁用滚动条。
缺点是您必须创建自己的列表视图类,该类派生自System.Windows.Forms.ListViewoverride 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);
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
23336 次 |
| 最近记录: |