[英]C# ListView Disable Horizontal Scrollbar
有沒有辦法阻止水平滾動條出現在列表視圖中? 我希望垂直滾動條在需要時顯示,但我希望水平滾動條永遠不會出現。
我想這與 WndProc 有關嗎?
謝謝
有一種更簡單的方法來消除下部滾動條並垂直顯示。 它包括確保標題,如果沒有標題,則行是listview.Width - 4
的寬度,如果顯示垂直滾動條,則listview.Width - Scrollbar.Width - 4;
以下代碼演示了如何:
lv.Columns[0].Width = lv.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 在再次查看后在上面的評論中涵蓋了這一點,所以我想這里的格式都很好。
你可以嘗試這樣的事情,我在一個項目中使用過一次,它奏效了:
[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);
}
希望它有幫助。
最佳解決方案是此處給出的公認答案: 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
。
如果您想要一個將圖標或值顯示為水平的列表視圖,只需將對齊設置為左
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.