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); // 隱藏
        }
    }
}


運行效果圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章