用mouse_event发送鼠标位移,位移几次以后程序卡死。关闭不了

功能是按下鼠标左键以后鼠标位移:
private void mh_MouseDownEvent(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//timer1.Enabled = true;
//timer1.Interval = Mv_data.PL;
if (MainOFF == true && TIMEOT_TAB == true && RESET == false) //单点位移
{
mouse_event(MOUSEEVENTF_MOVE, 0, Mv_data.DD, 0, 0);

            }
            //MSCK.NMouseMoveR(Mv_data.DD);
            LeftTag = true;


        }
                    }

                    鼠标钩子初始化:
                            private void MouseHookInit()
    {
        mh = new MouseHook();
        mh.SetHook();
        mh.MouseDownEvent += mh_MouseDownEvent;
        mh.MouseUpEvent += mh_MouseUpEvent;

        //mh.MouseMoveEvent += mh_MouseMoveEvent;
    }


            鼠标钩子
             class MouseHook
{
    private Point point;
    private Point Point
    {
        get { return point; }
        set
        {
            if (point != value)
            {
                point = value;
                if (MouseMoveEvent != null)
                {
                    var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
                    MouseMoveEvent(this, e);
                }
            }
        }
    }
    private int hHook;
    private static int hMouseHook = 0;
    private const int WM_MOUSEMOVE = 0x200;
    private const int WM_LBUTTONDOWN = 0x201;
    private const int WM_RBUTTONDOWN = 0x204;
    private const int WM_MBUTTONDOWN = 0x207;
    private const int WM_LBUTTONUP = 0x202;
    private const int WM_RBUTTONUP = 0x205;
    private const int WM_MBUTTONUP = 0x208;
    private const int WM_LBUTTONDBLCLK = 0x203;
    private const int WM_RBUTTONDBLCLK = 0x206;
    private const int WM_MBUTTONDBLCLK = 0x209;

    public const int WH_MOUSE_LL = 14;
    public Win32Api.HookProc hProc;
    public MouseHook()
    {
        this.Point = new Point();
    }
    public int SetHook()
    {
        hProc = new Win32Api.HookProc(MouseHookProc);
        hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
        return hHook;
    }
    public void UnHook()
    {
        Win32Api.UnhookWindowsHookEx(hHook);
    }
    private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
    {
        Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));
        if (nCode < 0)
        {
            return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
        }
        else
        {
            MouseButtons button = MouseButtons.None;
            int clickCount = 0;
            switch ((Int32)wParam)
            {
                case WM_LBUTTONDOWN:
                    button = MouseButtons.Left;
                    clickCount = 1;
                    MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                    break;
                case WM_RBUTTONDOWN:
                    button = MouseButtons.Right;
                    clickCount = 1;
                    MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                    break;
                case WM_MBUTTONDOWN:
                    button = MouseButtons.Middle;
                    clickCount = 1;
                    MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                    break;
                case WM_LBUTTONUP:
                    button = MouseButtons.Left;
                    clickCount = 1;
                    MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                    break;
                case WM_RBUTTONUP:
                    button = MouseButtons.Right;
                    clickCount = 1;
                    MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                    break;
                case WM_MBUTTONUP:
                    button = MouseButtons.Middle;
                    clickCount = 1;
                    MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
                    break;
            }

            this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
            return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
        }
    }

    public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
    public event MouseMoveHandler MouseMoveEvent;

    public delegate void MouseClickHandler(object sender, MouseEventArgs e);
    public event MouseClickHandler MouseClickEvent;

    public delegate void MouseDownHandler(object sender, MouseEventArgs e);
    public event MouseDownHandler MouseDownEvent;

    public delegate void MouseUpHandler(object sender, MouseEventArgs e);
    public event MouseUpHandler MouseUpEvent;

2个回答

没有解释为什么卡死的问题。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐