I was personally looking for something very similar, but couldn't find out how to remove BOTH scrollbars yet still get the mouse wheel to work for a listview. I understand this doesn't exactly pertain to the original question asked, but if anyone stumbles upon this page looking to remove both scrollbars but still have vertical scrolling functionality, here is the code to do just that! I scoured the net and could not find the answer, and the question asked here is very very similar so please don't hate me for posting this here. And don't freak out with my while (1==1) loop haha! Also this is patch job code from many sources including my own so I cannot take full credit nor do I know the original sources of some of it. Just create a new custom control using this code (change the namespace to what you want). And you can just alter your design.cs file for the form it's on and reference this control instead of the original ListView (2 lines of code). Be sure that Scrollable is set to true but also set ScrollOverride to true otherwise it will act as a normal ListView.
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);
}
}
}
Maybe someone can also add in code for a mouse wheel click and hold to scroll horizontally!