如何显式地触发"MouseMove".事件 [英] How to explicitly trigger "MouseMove" Event

查看:305
本文介绍了如何显式地触发"MouseMove".事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我仍然遇到另一个小问题.

情况::doh:
有一个面板,并且如果panel(myPanel)上发生MouseDown 事件,则会在Mouse Button按下的位置上将user control (myShape)添加到面板控件中.现在,我需要在新添加的控件myShapeMouseMove 事件内执行一些任务.

问题::((
即使添加的控件myShape 出现在mouseDown 事件上的鼠标指针的位置上,但是将鼠标移至myShape上(在不按下鼠标按钮的情况下且未按下)不会触发myShape的MouseMove 事件.
但是,按下按钮,然后移动鼠标会触发MouseMove Event.

需要帮助::confused:
有没有一种方法可以触发新添加的控件的MouseMove事件,而无需取消鼠标按钮.

简化代码如下:

Hello Everyone,
I am stuck with yet another small problem.

Situation: :doh:
There is a panel, and if MouseDown event occurs on the panel(myPanel), a user control (myShape) will be added to the controls of panel on the location where the Mouse Button was down. Now i need to perform some task inside the MouseMove event of newly added Control myShape.

Problem: :((
Even though the added control myShape shows up on the place of mouse pointer on mouseDown event, but moving the mouse (without unpressing the mouse button) over myShape doesnot trigger the myShape''s MouseMove Event.
However unpressing the button, and then moving the mouse triggers the MouseMove Event.

Help required: :confused:
Is there a way to trigger the MouseMove event of the newly added control, without unpressing the Mouse Button.

simplified code is as below:

public class myForm : form
{
    private void Intialize()
    {
        this.Controls.Add(this.myPanel);
    }
    private void myPanel_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
            return;
        MyShape myShape = new MyShape();
        listOfShape.Add(myShape);
        this.Controls.Add(myShape);
        myShape.Location = e.Location;
        myShape.BringToFront();
    }
}

public class MyShape : UserControl
{
    // I need to jump to this function without UnPressing the mouse button //
    private void MyShape_MouseMove(object sender, MouseEventArgs e)
    {
        // Do some Task //
    }
}



已经谢谢了.



Thanks already.

推荐答案

问题是:

MouseMove事件被调用,但是它在面板上的鼠标移动事件!不在形状上!

解决此问题的最佳方法是简单地处理面板的MouseMove事件并按以下方式移动控件:

注意:必须使myShape变量具有类范围,而不是方法范围.

The issue is:

The MouseMove Event is being called, however, its the mouse move event on the Panel! not on the shape!

The best way to fix this problem is simply to handle the MouseMove Event of the panel and move the control like this:

note: you have to make myShape variable have class scope, rather than method scope.

private void myShape_MouseMove(object sender, MouseEventArgs e)
{
  myShape.Location = e.Location();
}



同样,您必须保护自己,因为在面板上可能没有控件之前,MouseMove会被调用很多,更重要的是,因此您需要编写一些代码来确保MouseMove在实际放置控件之前不执行任何操作表格.一种可能就是使用标志为false,并在MouseDown事件中将其更改为true.确保使用锁防止线程问题.

戴夫



also, you have to protect yourself because MouseMove is going to get called a lot and more importantly BEFORE the control may be on the panel, so you need to write some code to make sure the MouseMove does nothing until the control is actually been put on the form. One possibility is just to use a flag to be false and make it change to true in the MouseDown event. Make sure you protect against threading issues by using a lock.

Dave


这篇关于如何显式地触发"MouseMove".事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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