如何知道UserControl内部控件的鼠标位置 [英] How to know the mouse position for control inside UserControl

查看:208
本文介绍了如何知道UserControl内部控件的鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我的Form
中有UserControlUserControl内有一个PictureBox

Form中,我控制PictureBox size对其进行缩放,从而使UserControl滚动

现在我想要一种方法来了解PictureBox或其中的图像的鼠标位置

我的应用是Windows窗体


注意:如果情况不明确,请询问我

Hello
I have UserControl in my Form
inside the UserControl there is a PictureBox

in the Form i control the PictureBox size to zoom it, that make the UserControl scroll

Now i want a way to know the Mouse position for PictureBox or for the image inside it

my app is windows form


Note: if the Scenario isn''t clear, please ask me

推荐答案

通常,您仅需要在处理鼠标事件时才知道鼠标坐标,但是随后需要知道这些坐标通过处理程序的event arguments参数提供给您.请参阅:
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.mouseeventhandler%28v = vs.110%29.aspx [ http://msdn.microsoft.com/zh-cn/library/system.windows.forms.mouseeventargs%28v = vs.110%29.aspx [ http://msdn.microsoft.com /en-us/library/system.windows.forms.control.pointtoclient(v=vs.110).aspx [ http://msdn.microsoft.com /en-us/library/system.windows.forms.control.pointtoscreen(v=vs.110).aspx [ http://msdn.microsoft.com/zh-CN/library/system.windows.forms.control.mouseposition(v = vs.110).aspx [ ^ ].

这就是鼠标坐标的全部内容.

但是您的主要问题有所不同.您不应该使用PictureBox;它不会帮助你.即使您可以使用PictureBox解决问题,它也没有任何实际意义,只会浪费您的开发工作和一些额外的资源.我会解释该怎么做.请查看我过去的答案:
在图片框内添加图片 [在C#中绘制矩形 [如何从旧图纸中清除面板 [ ^ ].

这些是我过去解释图形渲染的答案:
Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在面板上捕获图形 [在mdi子表单之间画线 [在C#中缩放图像.净鼠标滚轮 [ ^ ].

请注意,如果使用像素图形,则缩放表示图像的重新采样.或者,您可以使用方法System.Drawing.Graphics.DrawImage之一来执行此操作,该方法接受指定目标矩形的参数,因此当新图像大小不同时(它将保持宽高比),它将对图像进行重新采样: http://msdn.microsoft.com/zh-我们/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx [ http://msdn.microsoft.com/zh-cn/library/system.drawing.graphics.interpolationmode%28v = vs.110%29.aspx [ http://msdn.microsoft.com/zh-cn/library/system.drawing.drawing2d.interpolationmode%28v = vs.110%29.aspx [
Usually, you need to know mouse coordinates only when you handle mouse events, but then these coordinates are given to you through the event arguments parameter of your handler. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventhandler%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.X and Y represent the coordinates in the control''s coordinate system.

By the way, you can always do coordinate transform from screen coordinates to the coordinate system of your control and back:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen(v=vs.110).aspx[^].

This static method gives you mouse position is screen coordinates at any time, regardless of your event handling: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseposition(v=vs.110).aspx[^].

That''s all on the topic of mouse coordinates.

But your major problem is different. You should not use PictureBox; it won''t help you. Even though you could solve the problem with PictureBox, it makes no practical sense at all, would only waste your development effort and some extra resources. I''ll explain what to do instead. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

And these are my past answers explaining graphics rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
and this one is related specifically to zoom: Zoom image in C# .net mouse wheel[^].

Note that zooming means re-samling of your image, if you use a pixel graphics. Alternatively, you can do it using one of the methods System.Drawing.Graphics.DrawImage which accept the parameter specifying the target rectangle, so it will re-sample the image when the new image size is different (take care to preserve the aspect ratio): http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

Before all such operations, don''t forget to set appropriate re-sampling quality using this property:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode%28v=vs.110%29.aspx[^].

You get the best re-sampling (interpolation) quality when you use System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx[^].

Now, this is a big warning for you: you can easily re-sample the image down, but if you want do increase the size, you can do it only slightly, otherwise your final image quality would be unacceptable.

—SA


这篇关于如何知道UserControl内部控件的鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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