C# ListView 禁用水平滚动条

问题描述 投票:0回答:7

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

我想这可能与 WndProc 有关?

谢谢

c# winforms horizontal-scrolling wndproc
7个回答
9
投票

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

listview.Width - 4
,如果显示垂直滚动条,则
listview.Width - Scrollbar.Width - 4;

以下代码演示了如何操作:

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

6
投票

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

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

缺点是您必须创建自己的列表视图类,该类派生自

System.Windows.Forms.ListView
来覆盖
WndProc
。但这就是要走的路。

要禁用水平滚动条,请记住使用

WS_HSCROLL
而不是
WS_VSCROLL
(在链接的答案中使用)。
WS_HSCROLL
的值为
0x00100000


6
投票

当前接受的答案是不安全的,因为它会使堆栈不平衡。您应该使用以下代码来代替 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
    


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

希望有帮助。


0
投票

左对齐


0
投票

using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.InteropServices; using System.Windows.Forms; namespace LancourWestbrook.Controls { public partial class TPListView : ListView { [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)] public static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)] public static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, int dwNewLong); const int GWL_STYLE = -16; const int WS_VSCROLL = 0x00200000; const int WS_HSCROLL = 0x00100000; const int WM_MOUSEWHEEL = 0x20a; const int WM_NCCALCSIZE = 0x83; public TPListView() { InitializeComponent(); } public TPListView(IContainer container) { container.Add(this); InitializeComponent(); } private int? LastItemIndexInView { get { if (this.Items == null || this.Items.Count <= 0) { return null; } List<int> items = new List<int>(); int topIndex = this.TopItem.Index; int currentIndex = topIndex; items.Add(topIndex); while (1 == 1) { currentIndex++; if (this.Items.Count - 1 < currentIndex) { break; } if (this.Items[currentIndex].Bounds.IntersectsWith(this.ClientRectangle)) { items.Add(currentIndex); } else { break; } } return currentIndex; } } public bool ScrollOverride { get; set; } protected override void WndProc(ref Message m) { if (ScrollOverride == false) { base.WndProc(ref m); return; } switch (m.Msg) { case WM_MOUSEWHEEL: if (this.Items == null || this.Items.Count <= 0) { break; } var zDelta = (short)HIWORD(m.WParam); if (zDelta < 0) { //Scroll Downwards int? lastItemInView = LastItemIndexInView; if (lastItemInView.HasValue && this.Items.Count > lastItemInView.Value + 1) { this.Items[lastItemInView.Value + 1].EnsureVisible(); } else if (this.Items.Count > 0) { this.Items[this.Items.Count - 1].EnsureVisible(); } } else if (zDelta > 0) { //Scroll Upwards int topItemInView = this.TopItem.Index; if (topItemInView > 0) { this.Items[topItemInView - 1].EnsureVisible(); } } break; case WM_NCCALCSIZE: int style = (int)GetWindowLong(this.Handle, GWL_STYLE); if ((style & WS_VSCROLL) == WS_VSCROLL) SetWindowLong(this.Handle, GWL_STYLE, style & ~WS_VSCROLL); if ((style & WS_HSCROLL) == WS_HSCROLL) SetWindowLong(this.Handle, GWL_STYLE, style & ~WS_HSCROLL); base.WndProc(ref m); break; default: base.WndProc(ref m); break; } } public static int GetWindowLong(IntPtr hWnd, int nIndex) { if (IntPtr.Size == 4) return (int)GetWindowLong32(hWnd, nIndex); else return (int)(long)GetWindowLongPtr64(hWnd, nIndex); } public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong) { if (IntPtr.Size == 4) return (int)SetWindowLongPtr32(hWnd, nIndex, dwNewLong); else return (int)(long)SetWindowLongPtr64(hWnd, nIndex, dwNewLong); } internal static ushort HIWORD(IntPtr dwValue) { return (ushort)((((long)dwValue) >> 0x10) & 0xffff); } } }

也许有人还可以添加鼠标滚轮单击并按住以水平滚动的代码!


0
投票

public void SetItems(string[] itemNames) { int columnWidth = 0; this.Items.Clear(); for (int i = 0; i < itemNames.Length; i++) { string text = itemNames[i]; this.Items.Add(new ListViewItem(text)); columnWidth = Math.Max(columnWidth, TextRenderer.MeasureText(text, this.Font).Width); } this.Columns[0].Width = columnWidth + 20; // + 20: I have set CheckBoxes property to true }

© www.soinside.com 2019 - 2024. All rights reserved.