vb.net 查找鼠标相对于控件的位置,而不是屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28531058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find position of mouse relative to control, rather than screen
提问by 08robertsj
I have a Picture Box called BGImage. I hope that when the user clicks on this I can capture the position of the mouse relative to BGImage.
我有一个名为BGImage. 我希望当用户点击这个时我可以捕捉到鼠标相对于BGImage.
I've tried using MousePosition, only to find it gives the mouse location on the screen, not on the PictureBox.
我试过使用MousePosition,却发现它在屏幕上提供了鼠标位置,而不是在图片框上。
So I also tried using PointToClient:
所以我也尝试使用PointToClient:
Dim MousePos As Point = Me.PointToClient(MousePosition)
But this gives me the location {X=1866,Y=55}whereas I actually clicked on the PictureBox at around {X=516,Y=284}.
但这给了我位置,{X=1866,Y=55}而我实际上点击了大约{X=516,Y=284}.
I think the problem arises because I have full-screened my program and set the position of the PictureBox to be at the centre of the screen (BGImage.Location = New Point((My.Computer.Screen.WorkingArea.Width / 2) - (1008 / 2), ((My.Computer.Screen.WorkingArea.Height / 2) - (567 / 2))))
我认为问题出现是因为我已经全屏显示了我的程序并将PictureBox的位置设置在屏幕中心(BGImage.Location = New Point((My.Computer.Screen.WorkingArea.Width / 2) - (1008 / 2), ((My.Computer.Screen.WorkingArea.Height / 2) - (567 / 2))))
I should also mention that the size of the PictureBox is 1008 By 567 pixels and my screen resolution is 1366 by 768.
我还应该提到,PictureBox 的大小是 1008 x 567 像素,我的屏幕分辨率是 1366 x 768。
Is there any way I can get the mouse position relativeto BGImage's position?
有什么办法可以获得相对于 BGImage 位置的鼠标位置吗?
采纳答案by Creator
Add a mouse click event to your picture box
Then use the MouseEventArgs to get the mouse position inside the picture box.
This will give you the X and the Y location inside the picture box.
将鼠标单击事件添加到图片框
然后使用 MouseEventArgs 获取图片框内的鼠标位置。
这将为您提供图片框内的 X 和 Y 位置。
Dim PPoint As Point
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
PPoint = New Point(e.X, e.Y)
MsgBox(Convert.ToString(PPoint))
End Sub
回答by Helder da silveira ventura
I have before the same problem and just solved with the help of some friends. Give a look Here mouse position is not correctHere its the code that give you the correct position of the Mouse Based On A Picture. Tanks to @Aaron he have give a final solution to this problem.
我以前也遇到过同样的问题,只是在一些朋友的帮助下解决了。看看这里鼠标位置不正确这里是根据图片为您提供鼠标正确位置的代码。Tanks to @Aaron 他已经给出了这个问题的最终解决方案。
This will put a red dot on the exact point you click. I wonder how useful setting the cursor position will be though, as they will almost certainly move the mouse after clicking the button (inadvertently or not).
这将在您单击的确切点上放置一个红点。我想知道设置光标位置会有多大用处,因为它们几乎肯定会在单击按钮后移动鼠标(无意或无意)。
Setting the Cursor position needs to be in Screen coordinates - this converts back to client coordinates for drawing. I don't believe the PointToClient is necessary for the cursor position. In the below code, it is an unnecessary conversion, as you just go back to client coordinates. I left it in to show an example of each conversion, so that you can experiment with them.
设置光标位置需要在屏幕坐标中 - 这会转换回客户端坐标以进行绘图。我不相信 PointToClient 是光标位置所必需的。在下面的代码中,这是一个不必要的转换,因为您只是返回到客户端坐标。我留下它来显示每个转换的示例,以便您可以试验它们。
Public Class Form1
Private PPoint As Point
Public Sub New()
' This call is required by the designer.
InitializeComponent()
PictureBox1.BackColor = Color.White
PictureBox1.BorderStyle = BorderStyle.Fixed3D
AddHandler PictureBox1.MouseClick, AddressOf PictureBox1_MouseClick
AddHandler Button8.Click, AddressOf Button8_Click
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs)
Dim g As Graphics = PictureBox1.CreateGraphics()
Dim rect As New Rectangle(PictureBox1.PointToClient(PPoint), New Size(1, 1))
g.DrawRectangle(Pens.Red, rect)
End Sub
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs)
PPoint = PictureBox1.PointToScreen(New Point(e.X, e.Y))
Label8.Text = PPoint.X.ToString()
Label9.Text = PPoint.Y.ToString()
End Sub
End Class
回答by and his dog
Instead of using:
而不是使用:
Dim MousePos As Point = Me.PointToClient(MousePosition)
You should be using:
你应该使用:
Dim MousePos As Point = BGImage.PointToClient(MousePosition)
It will give you mouse position in BGImage coordinates, whereas the first code gives you the mouse position in the Form's coordinates.
它将为您提供 BGImage 坐标中的鼠标位置,而第一个代码为您提供表单坐标中的鼠标位置。

