Nó không phải dễ dàng, nhưng nó có thể được thực hiện. Nếu bạn cố gắng ẩn thanh cuộn qua ShowScrollBar, ListView sẽ chỉ đơn giản là đưa nó trở lại. Vì vậy, bạn phải làm điều gì đó đáng sợ hơn.
Bạn sẽ phải chặn thông báo WM_NCCALCSIZE và trong đó, tắt kiểu cuộn dọc. Bất cứ khi nào listview cố gắng bật lại, bạn sẽ tắt nó một lần nữa trong trình xử lý này.
public class ListViewWithoutScrollBar : ListView
{
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case 0x83: // WM_NCCALCSIZE
int style = (int)GetWindowLong(this.Handle, GWL_STYLE);
if ((style & WS_VSCROLL) == WS_VSCROLL)
SetWindowLong(this.Handle, GWL_STYLE, style & ~WS_VSCROLL);
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
const int GWL_STYLE = -16;
const int WS_VSCROLL = 0x00200000;
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);
}
[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);
}
Điều này sẽ cung cấp cho bạn một ListView mà không có thanh cuộn vẫn cuộn khi bạn sử dụng các phím mũi tên để thay đổi lựa chọn.
Hoạt động hoàn hảo, cảm ơn bạn! –
Tôi đã thử cùng một mã cho cuộn ngang (tôi đã thay thế 0x00200000 bằng 0x00100000), thanh cuộn được ẩn nhưng tôi không thể cuộn bằng các phím mũi tên. Bất kỳ ý tưởng? –