C#调用P/Invoke显示、隐藏和移动ListView 控件中的滚动条

小Demo演示如何调用P/Invoke显示、隐藏和移动ListView 控件中的滚动条

 C#代码

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ProgramticallyScrollListView
{
    public partial class frmScrollListView : Form
    {
        public frmScrollListView()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        static public extern bool ShowScrollBar(System.IntPtr hWnd, int wBar, bool bShow);

        [DllImport("user32.dll")]
        static public extern bool EnableScrollBar(System.IntPtr hWnd, uint wSBflags, uint wArrows);

        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool BringWindowToTop(IntPtr hWnd);

        private const uint SB_VERT = 1;

        private const uint ESB_DISABLE_BOTH = 0x3;

        private const uint ESB_ENABLE_BOTH = 0x0;

        private const int WM_VSCROLL = 0x115;

        private void frmScrollListView_Load(object sender, EventArgs e)
        {
            for (int j = 0; j < 300; j++)
            {
                this.listView1.Items.Add(j.ToString());
            }
            EnableScrollBar(this.listView1.Handle, (int)SB_VERT, ESB_ENABLE_BOTH);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 下移
            SendMessage(this.listView1.Handle, (uint)WM_VSCROLL, (System.UIntPtr)ScrollEventType.SmallIncrement, (System.IntPtr)0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 上移
            SendMessage(this.listView1.Handle, (uint)WM_VSCROLL, (System.UIntPtr)ScrollEventType.SmallDecrement, (System.IntPtr)0);
        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            ShowScrollBar(this.listView1.Handle, (int)SB_VERT, true); // 显示
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ShowScrollBar(this.listView1.Handle, (int)SB_VERT, false); // 隐藏
        }
    }
}


运行效果图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值