C# WinForms 无法使用 MouseMove 连续读取鼠标位置,也无法使用 MouseHover 读取按钮按下

问题描述 投票:0回答:0

在 winForms 中,当我在用户控件上按住鼠标按钮时,我希望能够连续调用一个事件

如果我使用

MouseMove 
事件,我可以获得
MouseEventArgs
我可以读取鼠标按钮按下

如果我使用

MouseHover
,我只能得到
EventArgs
我无法读取鼠标按钮按下

活动代码如下:

    private void Joystick_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            if (MoveorRot == false)
            {
                new_mouseDownLocation_Rot = new Point(e.X, e.Y);
                double angle_old = 57.2957795131 * Math.Atan2(mouseDownLocation_Rot.X - WIDTH, mouseDownLocation_Rot.Y - HEIGHT);
                double angle_new = 57.2957795131 * Math.Atan2(new_mouseDownLocation_Rot.X - WIDTH, new_mouseDownLocation_Rot.Y - HEIGHT);
                double[] move = { 0.0, 0.0, angle_old - angle_new };
                if (stage != null)
                    stage.PointToPoint(move);
            }
            else
            {
                Point new_mouseDownLocation_Move = new Point(e.X, e.Y);

                int move_x = mouseDownLocation_Move.X - new_mouseDownLocation_Move.X;
                int move_y = mouseDownLocation_Move.Y - new_mouseDownLocation_Move.Y;
                joy.X = WIDTH - move_x;
                joy.Y = HEIGHT - move_y;

                double[] move = { (double)move_x, (double)move_y, 0.0 };
                if (stage != null)
                    stage.PointToPoint(move);
            }
            this.Refresh();
        }
    }

问题是当鼠标停止移动时它也停止移动,因为事件没有被调用

我希望只要鼠标悬停在控件上就可以随时调用该事件,但代码仅在按下鼠标按钮时执行

当我使用 MouseHover 事件时,

   private void Joystick_MouseMove(object sender, EventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)  
        {
        ....

'e.Button' 在

EventArgs
中不可用,我无法将
EventArgs
转换为
MouseEventArgs

我尝试在

MouseUp
MouseDow
n 上设置一个布尔值,但它有时会出现故障并且
MouseUp
并不总是被调用。

我该如何解决这个问题?

c# winforms
© www.soinside.com 2019 - 2024. All rights reserved.