我在ListView中想在滚动条滚动时做一些事情,但是该控件没有滚动事件,所以我想通过截获滚动消息来处理,但是不会截获这个消息,有谁可以帮帮我么?

解决方案 »

  1.   

    啊,,,,没有人帮帮我么?难道C#就不能像VC一样可以对这些事件进行底层的处理么?
      

  2.   

    你可以在WndProc中试着拦截WM_HSCROLL和WM_VSCROLL
      

  3.   

    那通过MouseOver,Up,Down这些来仿滚动条事件
      

  4.   

    刚刚测试了一下
    你在Form的WndProc里面是捕捉不到ListView消息的
    你必须封装一下
    可以借助NativeWindow/*
        //调用方法
        ListViewExtender extender = new ListViewExtender();
        extender.BeginHookProc(this.listView1);
        //在窗体关闭的时候调用EndHookProc
    */
    using System;
    using System.Collections;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;namespace testApplication1
    {
    public class ListViewExtender
    {
    /// <summary>
    /// native windows
    /// </summary>
    private Hashtable nativeWindows = new Hashtable(); /// <summary>
    /// Creates a new <see cref="ListViewExtender"/> instance.
    /// </summary>
    public ListViewExtender()
    {
    } /// <summary>
    /// Begins the hook proc.
    /// </summary>
    /// <param name="control">Control.</param>
    public void BeginHookProc(Control control)
    {
    if(nativeWindows.Contains(control)) {
    return;
    } nativeWindows.Add(control, new ListViewNativeWindow(this, control));
    } /// <summary>
    /// Ends the hook proc.
    /// </summary>
    /// <param name="control">Control.</param>
    public void EndHookProc(Control control)
    {
    if(!nativeWindows.Contains(control)) {
    return;
    } ListViewNativeWindow window = nativeWindows[control] as ListViewNativeWindow;
    nativeWindows.Remove(control);
    window.ReleaseHandle();
    window = null;
    } /// <summary>
    /// WNDs the proc.
    /// </summary>
    /// <param name="m">M.</param>
    protected void WndProc(ref Message m) 
    {
                if (m.Msg == 0x114 || m.Msg == 0x115)
                {
                    //滚动条
                }
    } /// <summary>
    /// ListViewNativeWindow
    /// </summary>
    protected class ListViewNativeWindow : NativeWindow 
    {
    private ListViewExtender listViewExtender; /// <summary>
    /// Creates a new <see cref="ListViewNativeWindow"/> instance.
    /// </summary>
    /// <param name="listViewExtender">ListView extender.</param>
    /// <param name="control">Control.</param>
    public ListViewNativeWindow(ListViewExtender listViewExtender, Control control) 
    {
    this.listViewExtender = listViewExtender; if(control.IsHandleCreated) {
    AssignHandle(control.Handle);
    } else {
    control.HandleCreated += new EventHandler(OnControlHandleCreated);
    }

    control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed);
    } /// <summary>
    /// Ons the control handle created.
    /// </summary>
    /// <param name="sender">Sender.</param>
    /// <param name="e">E.</param>
    private void OnControlHandleCreated(object sender, EventArgs e) 
    {
    // Control is now created, assign handle to NativeWindow.
    AssignHandle(((Control)sender).Handle);
    } /// <summary>
    /// Ons the control handle destroyed.
    /// </summary>
    /// <param name="sender">Sender.</param>
    /// <param name="e">E.</param>
    private void OnControlHandleDestroyed(object sender, EventArgs e) 
    {
    // Control was destroyed, release hook.
    ReleaseHandle();
    } /// <summary>
    /// WNDs the proc.
    /// </summary>
    /// <param name="m">M.</param>
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    protected override void WndProc(ref Message m) 
    {
    listViewExtender.WndProc(ref m);
    base.WndProc(ref m);
    }
    }
    }
    }