winform鼠标滚轴无法滚动滚动条,无法focus

来自MSDN,实现接口,过滤消息只给指定的句柄panel1.Handle使用

Yes, you can give a container control the focus when it has no control on it.  A solution to your requirement is to hijack the WM_MOUSEWHEEL message before it can get to the control that has the focus and pass it directly to the control you want to scroll.  You'd use the IMessageFilter interface.  Something like this, scrolling a panel: 

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

namespace WindowsApplication1 { 
  public partial class Form1 : Form, IMessageFilter { 
    public Form1() { 
      InitializeComponent(); 
      Application.AddMessageFilter(this); 
      this.FormClosing += Form1_Closing; 
    } 
    private void Form1_Closing(object sender, FormClosingEventArgs e) { 
      Application.RemoveMessageFilter(this); 
    } 
    private bool mFiltering; 
    public bool PreFilterMessage(ref Message m) { 
      // Force WM_MOUSEWHEEL message to be processed by the panel 
      if (m.Msg == 0x020a && !mFiltering) { 
        mFiltering = true; 
        SendMessage(panel1.Handle, m.Msg, m.WParam, m.LParam); 
        m.Result = IntPtr.Zero;  // Don't pass it to the parent window 
        mFiltering = false; 
        return true;  // Don't let the focused control see it 
      } 
      return false; 
    } 
    // P/Invoke declarations 
    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); 
  } 
}

posted @ 2013-03-22 19:51  zuizuihao  阅读(1816)  评论(0)    收藏  举报