您的位置:首页 > 编程语言 > C#

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

2011-11-21 10:48 423 查看
小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); // 隐藏
}
}
}


运行效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: