[ListView.View=List]的垂直滚动条

该问题来自论坛提问,当ListView.View=List时,滚动条只能是水平的,解决这个问题需要子类化ListView处理WndProc消息,通过Windows API函数来设置它的滚动条。

演示代码:

using System; using System.Drawing; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { ///

/// 子类化ListView,在View属性是List的时候出垂直滚动条 /// by jinjazz /// http://blog.csdn.net/jinjazz /// public class ListViewEx : ListView { [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern int ShowScrollBar(IntPtr hWnd, int iBar, int bShow); const int SB_HORZ = 0; const int SB_VERT = 1; protected override void WndProc(ref Message m) { if (this.View == View.List) { ShowScrollBar(this.Handle, SB_VERT, 1); ShowScrollBar(this.Handle, SB_HORZ, 0); } base.WndProc(ref m); } } /// /// 测试代码 /// public Form1() { InitializeComponent(); //测试普通listview ListView list = new ListView(); list.View = View.List; this.Controls.Add(list); list.Size = new Size(100, 100); list.Location = new Point(100, 100); for (int i = 0; i < 100; i++) { list.Items.Add(new ListViewItem(Guid.NewGuid().ToString())); } //测试子类化的listview list = new ListViewEx(); list.View = View.List; this.Controls.Add(list); list.Size = new Size(100, 100); list.Location = new Point(300, 100); for (int i = 0; i < 100; i++) { list.Items.Add(new ListViewItem(Guid.NewGuid().ToString())); } } } }

你可能感兴趣的:([ListView.View=List]的垂直滚动条)