6

フォーカスを奪うことなく、マウスでこのコントロールを操作しているときに、メッセージ(WM_MOUSEWHEELなど)を転送したいと思います。この問題は、IMessageFilter(アプリケーションメッセージポンプに追加される)でメッセージをインターセプトし、P / Invoke(d)SendMessage()で転送することで簡単に解決できます。問題は、P / Invokeを使用せずに同じことを実行できますか(StackOverflowで見つけたソリューションはP / Invokeを使用します)?そうでない場合、なぜですか?

以下のコードは、P/Invokeを使用した私のソリューションです。だけで使っていますnew MessageForwarder(control, 0x20A)

/// <summary>
/// This class implements a filter for the Windows.Forms message pump allowing a
/// specific message to be forwarded to the Control specified in the constructor.
/// Adding and removing of the filter is done automatically.
/// </summary>
public class MessageForwarder : IMessageFilter
{
#region Fields

private Control _Control;
private Control _PreviousParent;
private HashSet<int> _Messages;
private bool _IsMouseOverControl;

#endregion // Fields

#region Constructors

public MessageForwarder(Control control, int message)
    : this(control, new int[] { message }) { }

public MessageForwarder(Control control, IEnumerable<int> messages)
{
    _Control = control;
    _Messages = new HashSet<int>(messages);
    _PreviousParent = control.Parent;
    _IsMouseOverControl = false;

    control.ParentChanged += new EventHandler(control_ParentChanged);
    control.MouseEnter += new EventHandler(control_MouseEnter);
    control.MouseLeave += new EventHandler(control_MouseLeave);
    control.Leave += new EventHandler(control_Leave);

    if (control.Parent != null)
        Application.AddMessageFilter(this);
}

#endregion // Constructors

#region IMessageFilter members

public bool PreFilterMessage(ref Message m)
{
    if (_Messages.Contains(m.Msg) && _Control.CanFocus && !_Control.Focused
        && _IsMouseOverControl)
    {
        SendMessage(_Control.Handle, m.Msg, m.WParam, m.LParam);
        return true;
    }

    return false;
}

#endregion // IMessageFilter

#region Event handlers

void control_ParentChanged(object sender, EventArgs e)
{
    if (_Control.Parent == null)
        Application.RemoveMessageFilter(this);
    else
    {
        if (_PreviousParent == null)
            Application.AddMessageFilter(this);
    }
    _PreviousParent = _Control.Parent;
}

void control_MouseEnter(object sender, EventArgs e)
{
    _IsMouseOverControl = true;
}

void control_MouseLeave(object sender, EventArgs e)
{
    _IsMouseOverControl = false;
}

void control_Leave(object sender, EventArgs e)
{
    _IsMouseOverControl = false;
}

#endregion // Event handlers

#region Support

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

#endregion // Support

}

編集:私の答えの完全な解決策

4

3 に答える 3

7

メソッドを見つけました:継承NativeWindowし、選択したコントロールのハンドルをそれに割り当てて、任意の方法でメッセージをインターセプトした後、保護されたWndProcを呼び出す必要があります(私の場合、継承されたクラスはIMessageFilterであるため、簡単にできますアプリケーションに接続します)。new MessageForwarder(anycontrol, 0x20A)マウスホイールをリダイレクトするために使用します。

したがって、p / invokeを使用せずに、メッセージをインターセプトして任意のコントロールに転送することができます。しかし、それはよく隠されていました。

/// <summary>
/// This class implements a filter for the Windows.Forms message pump allowing a
/// specific message to be forwarded to the Control specified in the constructor.
/// Adding and removing of the filter is done automatically.
/// </summary>
public class MessageForwarder : NativeWindow, IMessageFilter
{
    #region Fields

    private Control _Control;
    private Control _PreviousParent;
    private HashSet<int> _Messages;
    private bool _IsMouseOverControl;

    #endregion // Fields

    #region Constructors

    public MessageForwarder(Control control, int message)
        : this(control, new int[] { message }) { }

    public MessageForwarder(Control control, IEnumerable<int> messages)
    {
        _Control = control;
        AssignHandle(control.Handle);
        _Messages = new HashSet<int>(messages);
        _PreviousParent = control.Parent;
        _IsMouseOverControl = false;

        control.ParentChanged += new EventHandler(control_ParentChanged);
        control.MouseEnter += new EventHandler(control_MouseEnter);
        control.MouseLeave += new EventHandler(control_MouseLeave);
        control.Leave += new EventHandler(control_Leave);

        if (control.Parent != null)
            Application.AddMessageFilter(this);
    }

    #endregion // Constructors

    #region IMessageFilter members

    public bool PreFilterMessage(ref Message m)
    {
        if (_Messages.Contains(m.Msg) && _Control.CanFocus && !_Control.Focused
            && _IsMouseOverControl)
        {
            m.HWnd = _Control.Handle;
            WndProc(ref m);
            return true;
        }

        return false;
    }

    #endregion // IMessageFilter

    #region Event handlers

    void control_ParentChanged(object sender, EventArgs e)
    {
        if (_Control.Parent == null)
            Application.RemoveMessageFilter(this);
        else
        {
            if (_PreviousParent == null)
                Application.AddMessageFilter(this);
        }
        _PreviousParent = _Control.Parent;
    }

    void control_MouseEnter(object sender, EventArgs e)
    {
        _IsMouseOverControl = true;
    }

    void control_MouseLeave(object sender, EventArgs e)
    {
        _IsMouseOverControl = false;
    }

    void control_Leave(object sender, EventArgs e)
    {
        _IsMouseOverControl = false;
    }

    #endregion // Event handlers
}
于 2011-05-23T00:35:56.627 に答える
3

転送しようとしているメッセージに対応するイベントがある場合にのみ適用できる、はるかに簡単な解決策を見つけました。たとえば、mousewheelイベントの場合:

// Redirect the mouse wheel event from panel1 to panel2.
// When the panel1 is focused and the mouse wheel is used the panel2 will scroll.
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
   // Get the MouseWheel event handler on panel2
   System.Reflection.MethodInfo onMouseWheel = 
       panel2.GetType().GetMethod("OnMouseWheel", 
                                   System.Reflection.BindingFlags.NonPublic | 
                                   System.Reflection.BindingFlags.Instance);

   // Call the panel2 mousehwweel event with the same parameters
   onMouseWheel.Invoke(panel2, new object[] { e });
}
于 2013-01-25T11:13:40.560 に答える
0

それは本当にイベントの種類とその数に依存します。マウスの動きなどのイベントを親コントロールに渡すだけではどうでしょうか(たとえば、コントロールを「透過的」に動作させるため)。

コントロール内のイベントハンドラーの1つは、次のようになります(テストせずに頭からコードを書きます)。

private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
    if(Parent == null)
        return;

    // add this control's offsets first so the coordinates fit to the parent control
    e.X += this.Top;
    e.Y += this.Left;
    if(parent.MouseMove != null)
        parent.MouseMove(sender, e);
}
于 2011-05-18T00:00:37.250 に答える