帮助鼠标事件 [英] Help with mouse events

查看:71
本文介绍了帮助鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主窗体,有一个自定义控件,想法是在点击鼠标时通过鼠标的X和Y pos并将此控件保持为主窗体,这很容易,但我不知道为什么有时X和Y有负值...



<前lang =cs> 公众 partial class NavigatorControl:UserControl
{
public delegate void EventHandlerNavigateControl( int x, int y);
public event EventHandlerNavigateControl EventNavigate;
private bool IsMouseDown = false ;

public NavigatorControl()
{
InitializeComponent();
}

void OnNavigate( int x, int y)
{
if (EventNavigate!= null
EventNavigate(x,y);
}

// 这里工作正常,当鼠标离开控件时它停止调用OnNavigate ...
// private void NavigatorControl_MouseMove(object sender,MouseEventArgs e)
// {
// OnNavigate(eX,eY);
// }

private void NavigatorControl_MouseLeave( object sender,EventArgs e)
{
IsMouseDown = false ;
}

private void NavigatorControl_MouseDown( object sender,MouseEventArgs e)
{
IsMouseDown = true ;
}

private void NavigatorControl_MouseUp( object sender,MouseEventArgs e)
{
IsMouseDown = false ;
}

private void NavigatorControl_MouseMove( object sender,MouseEventArgs e)
{
if (IsMouseDown)
{
OnNavigate(eX,eY);
}
}
}

解决方案

您不在MethodX中共享代码,但是你似乎想要移动你的自定义控件(一个UserControl?),并将其保持在它所包含的控件/表格的边界内。



那里在您的代码中使用+ =和 - =来安装和卸载EventHandlers是没有错误!



什么是有问题的现在正在做的是:



1.你在每个MouseDown和MouseLeave事件中实例化 new EventHandlers;没有必要这样做(见下面的代码)。每次使用new MouseEventHandler(...)时,您都要分配一个 new EventHandler对象!使用 - =删除EventHandler的一个有趣的事情是,如果您尝试删除的EventHandler在存储在事件的Object中的Multicast委托中不存在,它将不会抛出错误。所以你的代码只是堆积了多个EventHandler!



2.你没有实现......我们可以看到...当鼠标实际移动自定义控件 的代码已经失败。



这里的另一个问题是如何将自定义控件保持在其容器的边界内:为此,您需要为自定义设置剪切矩形在MouseDown事件中控制,并在MouseUp事件中撤消剪切矩形。我们将会这样做。

  //  您需要一些实用程序变量: 
private bool IsMouseDown = false ;
private int mouseDownX;
private int mouseDownY;

private void CustomControl_MouseDown( object sender,MouseEventArgs e)
{
IsMouseDown = true ;
mouseDownX = e.X;
mouseDownY = e.Y;
}

private void CustomControl_MouseUp( object sender,MouseEventArgs e)
{
IsMouseDown = false ;
}

private void CustomControl_MouseMove( object sender,MouseEventArgs e)
{
if (IsMouseDown)
{
CustomControl.Left + = eX - mouseDownX;
CustomControl.Top + = e.Y - mouseDownY;
}
} < / pre >

这是非常标准的代码,我猜你已经熟悉它了。如果您希望打开并关闭您的代码中的CustomControl 可移动,您将需要实现以下内容:

  private   void  SetCustomControlMovable( bool  isMovable)
{
if (isMovable)
{
CustomControl.MouseDown + = CustomControl_MouseDown;
CustomControl.MouseUp + = CustomControl_MouseUp;
CustomControl.MouseMove + = CustomControl_MouseMove;
}
else
{
CustomControl.MouseDown - = CustomControl_MouseDown;
CustomControl.MouseUp - = CustomControl_MouseUp;
CustomControl.MouseMove - = CustomControl_MouseMove;
}
}

因此,在这种情况下,您将在设计时将EventHandlers分配给您的CustomControl。



现在,我们可以解决如何让Control受限制在其容器Control(或Form,或其他)的边界内移动的问题。在这篇文章中:[ ^ ],我将展示如何做到这一点。


请查看我的评论并相应澄清,以获得进一步的帮助。同时,让我告诉你一个关于它的主要内容:使用负鼠标坐标没有任何问题



-SA

I have a main form, that have a custom control, the idea is pass the X and Y pos of the mouse when the mouse is clicked and keep in this control to main form, it's easy, but i don't know why sometimes X and Y have a negative value...

public partial class NavigatorControl : UserControl
    {
        public delegate void EventHandlerNavigateControl(int x, int y);
        public event EventHandlerNavigateControl EventNavigate;
        private bool IsMouseDown = false;

        public NavigatorControl()
        {
            InitializeComponent();
        }

        void OnNavigate(int x, int y)
        {
            if (EventNavigate != null)
                EventNavigate(x, y);
        }
        
        // Here works fine, when mouse leave the control it stop to call OnNavigate...
        //private void NavigatorControl_MouseMove(object sender, MouseEventArgs e)
        //{
        //    OnNavigate(e.X, e.Y);
        //}

        private void NavigatorControl_MouseLeave(object sender, EventArgs e)
        {
            IsMouseDown = false;
        }

        private void NavigatorControl_MouseDown(object sender, MouseEventArgs e)
        {
            IsMouseDown = true;
        }

        private void NavigatorControl_MouseUp(object sender, MouseEventArgs e)
        {
            IsMouseDown = false;
        }

        private void NavigatorControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown)
            {
                OnNavigate(e.X, e.Y);
            }
        }
    }

解决方案

You don't share the code in MethodX, but it seems clear you want to move your Custom Control (a UserControl ?) around, and keep it within the boundaries of the Control/Form it is contained within.

There is nothing wrong with using += and -= in your code to install, and uninstall, EventHandlers !

What is questionable about what you are doing now is:

1. you are instantiating new EventHandlers in every MouseDown and MouseLeave Event; there's no need to do that (see code below). Every time you use "new MouseEventHandler(...)" you are allocating a new EventHandler object ! An interesting thing about using -= to remove an EventHandler is that it will not throw an error if the EventHandler you try to remove does not exist in the Multicast delegate that is stored in the Object's for the Event. So your code just "piles on" multiple EventHandlers !

2. you are not implementing ... that we can see ... code that actually moves the Custom Control only when the mouse is down.

The other issue here is how to keep the Custom Control inside the boundaries of its container: to do that you need to set the clipping Rectangle for the Custom Control in the MouseDown Event, and "undo" that clipping Rectangle in the MouseUp Event. We'll get to that.

// You'll need some utility variables:
private bool IsMouseDown = false;
private int mouseDownX;
private int mouseDownY;

private void CustomControl_MouseDown(object sender, MouseEventArgs e)
{
    IsMouseDown = true;
    mouseDownX = e.X;
    mouseDownY = e.Y;
}

private void CustomControl_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseDown = false;
}

private void CustomControl_MouseMove(object sender, MouseEventArgs e)
{
    if(IsMouseDown)
    {
        CustomControl.Left += e.X - mouseDownX;
        CustomControl.Top += e.Y - mouseDownY;
    }
}</pre>

This is all very standard code, and I would guess you are already familiar with it. If you wish to "turn on" and "turn off" the CustomControl being movable in your code you are going to need to implement something like this:

private void SetCustomControlMovable(bool isMovable)
{
    if (isMovable)
    {
        CustomControl.MouseDown += CustomControl_MouseDown;
        CustomControl.MouseUp += CustomControl_MouseUp;
        CustomControl.MouseMove += CustomControl_MouseMove;
    }
    else
    {
        CustomControl.MouseDown -= CustomControl_MouseDown;
        CustomControl.MouseUp -= CustomControl_MouseUp;
        CustomControl.MouseMove -= CustomControl_MouseMove;
    }
}

So, in this case, you would not assign the EventHandlers to your CustomControl at design-time.

Now, we can address the issue of how to keep your Control constrained to move inside the boundaries of its container Control (or Form, or whatever). In this post: [^], I show how to do that.


Please see my comment the the question and clarify accordingly, to get further help. In the meanwhile, let me tell you one main thing about it: there is nothing wrong with having negative mouse coordinate.

—SA


这篇关于帮助鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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