类之间的鼠标事件 [英] Mouse event between classes

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

问题描述

我有一个非常简单的mousemove事件,只显示图片框图像上的鼠标位置X,Y



**图像处理类

i have a pretty simple mousemove event which just shows the mouse position X,Y on a picturebox image

**image manipulation class

public void move(MouseEventArgs e)
        {
            double x = e.X - xOffset;
            double y = e.Y - yOffset;
            double worldx, worldy;
            worldx = DPM.xlo + (x-DPM.hll)*(DPM.xhi-DPM.xlo)/(DPM.hur-DPM.hll);
            worldy = DPM.ylo + (y-DPM.vll)*(DPM.yhi-DPM.ylo)/(DPM.vur-DPM.vll);
            f1.labX.Text = worldx.ToString("F");
            f1.labY.Text = worldy.ToString("F");
        }



我正在尝试将所有图像修改内容移动到图像类,但是从主类调用mousemove不起作用 - 我认为这是因为每次我实例化类我刷新变量(我使用xOffset和yOffset这是光标的最后已知位置)。我也有一些基于点击的东西,它们工作正常,这证实了我的怀疑。



我一直在读关于单身人士和更复杂的事件处理,但是我的经验是null(我无法理解API文档,因为我不是开发者),所以我非常感谢有关如何处理这个问题的任何提示。



如果我将代码保留在主窗体中,一切正常,所以我知道底层的东西是好的



我尝试了什么:



**在主表格类中


i'm trying to move all of my image modifying stuff to an image class, but calling the mousemove from the main class doesn't work - i think it's because each time i instantiate the class i refresh the variables (and i use xOffset and yOffset which are the last known position of the cursor). I also have some click based stuff and they work fine, which confirms my suspicions i think.

i've been reading about both singletons and more complex event handling, but my experience with either is null (and i have trouble comprehending the API docs as i'm not a dev), so i really appreciate any hints on how to handle this.

if i keep the code in the main form, everything works fine so i know the underlying stuff is ok

What I have tried:

**in the main form class

private void evImage_MouseMove(object sender, MouseEventArgs e)
        {
            EvImage ev1 = new EvImage(this);
            ev1.move(e);
        }



每次实例化一个新课程,我想我明白为什么它不起作用




that instantiates a new class each time, and i think i get why it isn't working

public EvImage ev;
        public Form1(EvImage form)
        {
            this.ev = form;
        }

private void evImage_MouseMove(object sender, MouseEventArgs e)
        {           
            ev.move(e);
        }



i认为这会给我一个实例,但它不会编译


i thought this would give me a single instance but it won't compile

推荐答案

嗨会员12815488,你的一些代码令人困惑。我猜你的类名是 EvImage 所有的图像处理代码都在哪里?然后再次 evImage_MouseMove()看起来您的表单名称是 evImage ,但您的表单名称实际上是 Form1



请在主表单上尝试以下操作。此代码猜测您的表单名称是 Form1 ,您的类名是 EvImage

Hi Member 12815488, some of your code is confusing. I guess your classname is EvImage where all the image manipulation code resides? Then again evImage_MouseMove() looks like your form name is evImage, but your form name is actually Form1.

Please try the following on the main form. This code is on the guess that your form name is Form1, your class name is EvImage:
EvImage ev1;
private void Form1_Load(object sender, EventArgs e)
{
    ev1 = new EvImage(this);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    ev1.move(e);
}

这里做的是 EvImage 类只被设置一次。

What is being done here is the EvImage class is instatiated only once.


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

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