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

 

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

 

演示代码:

 

 

  1. using System;   
  2. using System.Drawing;   
  3. using System.Windows.Forms;   
  4. namespace WindowsApplication1   
  5. {   
  6.     public partial class Form1 : Form   
  7.     {   
  8.         /// <summary>   
  9.         /// 子类化ListView,在View属性是List的时候出垂直滚动条   
  10.         /// by jinjazz   
  11.         /// http://blog.csdn.net/jinjazz   
  12.         /// </summary>   
  13.         public class ListViewEx : ListView   
  14.         {   
  15.             [System.Runtime.InteropServices.DllImport("user32.dll")]   
  16.             public static extern int ShowScrollBar(IntPtr hWnd, int iBar, int bShow);   
  17.             const int SB_HORZ = 0;   
  18.             const int SB_VERT = 1;   
  19.             protected override void WndProc(ref Message m)   
  20.             {   
  21.                 if (this.View == View.List)   
  22.                 {   
  23.                     ShowScrollBar(this.Handle, SB_VERT, 1);   
  24.                     ShowScrollBar(this.Handle, SB_HORZ, 0);   
  25.                 }   
  26.                 base.WndProc(ref m);   
  27.             }   
  28.         }   
  29.            
  30.         /// <summary>   
  31.         /// 测试代码   
  32.         /// </summary>   
  33.         public Form1()   
  34.         {   
  35.             InitializeComponent();   
  36.             //测试普通listview   
  37.             ListView list = new ListView();   
  38.             list.View = View.List;   
  39.             this.Controls.Add(list);   
  40.             list.Size = new Size(100, 100);   
  41.             list.Location = new Point(100, 100);   
  42.             for (int i = 0; i < 100; i++)   
  43.             {   
  44.                 list.Items.Add(new ListViewItem(Guid.NewGuid().ToString()));   
  45.             }   
  46.             //测试子类化的listview   
  47.             list = new ListViewEx();   
  48.             list.View = View.List;   
  49.             this.Controls.Add(list);   
  50.             list.Size = new Size(100, 100);   
  51.             list.Location = new Point(300, 100);   
  52.             for (int i = 0; i < 100; i++)   
  53.             {   
  54.                 list.Items.Add(new ListViewItem(Guid.NewGuid().ToString()));   
  55.             }   
  56.         }   
  57.           
  58.     }   
  59. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值