MouseEventArgs& EventArgs的 [英] MouseEventArgs & EventArgs

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

问题描述

嗨好人,



我正在开发一个包含两个pictureBox的小程序,其中第一个,pictureBox1,显示原始图像,另一个,pictureBox2,将显示由放大矩形包围的pictureBox1的选定部分的缩放版本。我在CP中找到了一个解决方案,我找到了下面的链接:



找到文章链接



但是,这个解决方案对我不起作用,因为事件处理程序传递了EventArgs而不是MouseEventArgs方法(上面的解决方案中的UpdateZoomedImage)使用。





我如何使用该方法处理EventArgs的任何想法或变通办法到下面显示的事件处理程序?



  private   void  pictureBox1_MouseHover( object  sender,EventArgs e)
{
UpdateZoomedImage(e);
}





我得到的错误是这样的:



参数1:无法从'System.EventArgs'转换为'System.Windows.Forms.MouseEventArgs'



谢谢!

解决方案

很明显, System.EventHandler 类型的对象无法转换为 System.Windows.Forms.MouseEventArgs 。 (你没有在你的问题中正确指定任何内容,没有引用该事件,但它很可能是指事件 System.Windows.Forms.Control.MouseHover 这是使用事件参数类型 System.EventHandler 。下次,提供相关类型的全名,指明您正在使用的UI库。)



请参阅: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs%28v=vs.110%29.aspx [ ^ ]。



从一开始你的方法就错了。您无法从此事件中获取所有信息,例如鼠标位置。你需要使用另外一个或代替这个,使用类 System.Windows.Forms.MouseEventArgs 的其他鼠标事件,例如 MouseMove 。您有责任提供适当的逻辑 - 您没有显示 UpdateZoomedImage 的内容。



但是你正在制作更糟糕的设计错误:使用 PictureBox 进行缩放等操作。这种控制是多余的,浪费的,根本没用,除非你使用if更简单的东西,比如显示一些静态图像。我详细解释了这一切,并建议在我过去的答案中做些什么;第一个专注于缩放。请参阅:

放大图片C#.net鼠标滚轮 [ ^ ];

为什么不使用 PictureBox 而是做什么:

在图片框中附加图片 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ];

图形渲染:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



-SA


如果您需要随时在WinForms中获取鼠标位置,您可以使用:

 Point currentPoint = MousePosition; 

这将返回鼠标在屏幕中的位置协调。



从屏幕坐标到当前的表格坐标:

 Point currentFormPoint =  this  .PointToClient(MousePosition)

获取特定Control中的坐标,如PictureBox:

 Point currentControlPoint = pictureBox1。 PointToClient(MousePosition); 


试试这个:

  private   void  pictureBox1_MouseHover( object  sender,MouseEventArgs e)
{
UpdateZoomedImage(e);
}



您可能没有为鼠标事件使用正确类型的事件参数,因此系统会引发它给您的错误。


Hi good people,

I am working on a little program that contains two pictureBoxes where the first, pictureBox1, shows an original image and the other, pictureBox2, will show a zoomed version of a selected portion of pictureBox1 that is enclosed by a magnifying rectangle. I looked for a solution in CP and I found the one linked below:

Found Article Link

However, this solution didn't work for me as the event handler passes EventArgs not a MouseEventArgs that the method (UpdateZoomedImage) in the above solution uses.


Any ideas or workarounds of how I can get that method to work with EventArgs being passed to the event handler shown below?

private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            UpdateZoomedImage(e);
        }



Error I am getting is this:

Argument 1: cannot convert from 'System.EventArgs' to 'System.Windows.Forms.MouseEventArgs'

Thanks!

解决方案

Is it apparent that an object of the type System.EventHandler cannot be cast to System.Windows.Forms.MouseEventArgs. (You did not specify anything properly in your question, did not reference the event, but it's most likely that you mean the event System.Windows.Forms.Control.MouseHover which is uses with the event arguments type System.EventHandler. Next time, provide full names of relevant types, indicate what UI library you are using.)

Please see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs%28v=vs.110%29.aspx[^].

Your approach is wrong from the very beginning. You cannot get all the information, say, one the mouse position, from this event. You need to use, additionally or instead of this one, other mouse events working with the class System.Windows.Forms.MouseEventArgs, such as MouseMove. It's your responsibility to provide appropriate logic — you did not show what your UpdateZoomedImage does.

But you are making much worse design mistake: the use of PictureBox for something like zooming. This control is redundant, wasteful and not useful at all, unless you use if for something much simpler, such as showing some static images. I explain it all in detail and suggest what to do in my past answer; the first one is specifically focused on zooming. Please see:
Zoom image in C# .net mouse wheel[^];
why not using PictureBox and what to do instead:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^];
graphics rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

—SA


If you need to get the Mouse Position at any time in WinForms, you can use:

Point currentPoint = MousePosition;

That will return where the Mouse is in screen co-ordinates.

To get from screen co-ordinates to current Form co-ordinates:

Point currentFormPoint = this.PointToClient(MousePosition)

To get the co-ordinates in a specific Control, like a PictureBox:

Point currentControlPoint = pictureBox1.PointToClient(MousePosition);


try this:

private void pictureBox1_MouseHover(object sender, MouseEventArgs e)
{
    UpdateZoomedImage(e);
}


you probably aren't using the right type of event args for the mouse event, thus the system raises the error it gives you.


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

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