C#在控件级别覆盖WndProc以检测 [英] C# override WndProc in Control level to detect

查看:303
本文介绍了C#在控件级别覆盖WndProc以检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UserControl级别覆盖了WndProc,以检测MouseDown,MouseUp和MouseMove到该UserControl中添加的任何控件.

I have overridden WndProc in UserControl level to detect MouseDown, MouseUp, and MouseMove to any Control added in that UserControl.

protected override void WndProc(ref Message m)
    {
        Point mouseLoc = new Point();

        switch (m.Msg)
        {
            case WM_LBUTTONDOWN:
                System.Diagnostics.Debug.WriteLine("mouse down");
                //this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, mouseLoc.X, mouseLoc.Y, 0));

                break;
            case WM_LBUTTONUP:
                System.Diagnostics.Debug.WriteLine("mouse up");
                //this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, mouseLoc.X,mouseLoc.Y, 0));

                break;
            case WM_MOUSEMOVE:
                int lParam = m.LParam.ToInt32();

                //mouseLoc.X = lParam & 0xFFFF;
                //mouseLoc.Y = (int)(lParam & 0xFFFF0000 >> 16);

                mouseLoc.X = (Int16)m.LParam;
                mouseLoc.Y = (Int16)((int)m.LParam >> 16);

                System.Diagnostics.Debug.WriteLine("mouse move: " + mouseLoc.X + ", " + mouseLoc.Y);

                //this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, mouseLoc.X,mouseLoc.Y, 0));
                break;
        }

        base.WndProc(ref m);
    }

当鼠标指针位于UserControl中时,MouseMove,Down和Up起作用,但是当鼠标指针位于其他控件(在UserControl内)时,则不起作用.

MouseMove, Down, and Up are working when the mouse pointer is in UserControl but when the mouse pointer is on other control (inside my UserControl) it doesn't work.

我做错什么了吗?

当前正在开发轻拂和滚动控件.

Currently developing a flick and scroll control.

推荐答案

这是Windows的工作方式-winforms中的每个控件都是一个窗口,鼠标消息移至它们结束的窗口.如果需要从其他窗口获取鼠标输入,则需要以某种方式进行配合.

This is how windows works - each control in winforms is a window, and mouse messages go to the window they are over. If you need to get the mouse input from other windows you need to cooperate them somehow.

说了这么多,如果您想要的只是轻拂和滚动控件,则应考虑查看

Having said all of that, if all you want is a flick and scroll control, you should consider looking at the WM_GESTURE APIs - that is what they are for, and they will allow you to implement flick and scroll without any cooperation from your child windows.

这篇关于C#在控件级别覆盖WndProc以检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆