本文介绍了查找鼠标相对于控件的位置,而不是屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 BGImage 的图片框.我希望当用户点击这个时我可以捕捉到鼠标相对于 BGImage 的位置.

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.

我尝试过使用 MousePosition,结果发现它在屏幕上提供了鼠标位置,而不是在图片框上.

I've tried using MousePosition, only to find it gives the mouse location on the screen, not on the PictureBox.

所以我也尝试使用 PointToClient:

Dim MousePos As Point = Me.PointToClient(MousePosition)

但这给了我位置 {X=1866,Y=55} 而我实际上在 {X=516,Y=284} 附近点击了图片框.

But this gives me the location {X=1866,Y=55} whereas I actually clicked on the PictureBox at around {X=516,Y=284}.

我认为问题出现是因为我已经全屏显示了我的程序并将PictureBox的位置设置为屏幕中心(BGImage.Location = New Point((My.Computer.Screen.WorkingArea.Width/2) - (1008/2), ((My.Computer.Screen.WorkingArea.Height/2) - (567/2))))

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 的大小是 1008 x 567 像素,我的屏幕分辨率是 1366 x 768.

I should also mention that the size of the PictureBox is 1008 By 567 pixels and my screen resolution is 1366 by 768.

有什么办法可以让鼠标位置相对 BGImage的位置?

Is there any way I can get the mouse position relative to BGImage's position?

推荐答案

向图片框添加鼠标点击事件
然后使用 MouseEventArgs 获取图片框内的鼠标位置.
这将为您提供图片框内的 X 和 Y 位置.

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.

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

这篇关于查找鼠标相对于控件的位置,而不是屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:10