简体   繁体   English

找到鼠标相对于控件的位置,而不是屏幕

[英]Find position of mouse relative to control, rather than screen

I have a Picture Box called BGImage . 我有一个名为BGImage的Picture Box。 I hope that when the user clicks on this I can capture the position of the mouse relative to BGImage . 我希望当用户点击它时我可以捕获相对于BGImage的鼠标位置。

I've tried using MousePosition , only to find it gives the mouse location on the screen, not on the PictureBox. 我尝试过使用MousePositionMousePosition发现它在屏幕上显示鼠标位置,而不是在PictureBox上。

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=1866,Y=55}而我实际上点击了{X=516,Y=284}左右的PictureBox。

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乘567像素,我的屏幕分辨率是1366乘768。

Is there any way I can get the mouse position relative to BGImage's position? 有没有办法让我对于BGImage的位置获得鼠标位置?

Add a mouse click event to your picture box 将鼠标单击事件添加到图片框中
Then use the MouseEventArgs to get the mouse position inside the picture box. 然后使用MouseEventArgs获取图片框内的鼠标位置。
This will give you the X and the Y location inside the picture box. 这将为您提供图片框内的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

I have before the same problem and just solved with the help of some friends. 我有同样的问题,只是在一些朋友的帮助下解决了。 Give a look Here mouse position is not correct Here 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. 坦克到@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. 我不相信PointToClient是光标位置所必需的。 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. 我把它留下来展示每个转换的示例,以便您可以试验它们。

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

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坐标中的鼠标位置,而第一个代码为您提供Form坐标中的鼠标位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当它位于splittercontainer中时,找到相对于父窗体的控件位置 - Find control position relative to the parent form when it is inside a splittercontainer 相对于VB.net的控制位置 - Control Position Relative to Container VB.net 如何从鼠标的屏幕位置推导出形状的滑动ppt位置 - how to deduce the slide ppt position of a shape from the screen position of the mouse 如何将控件(Windows控件)的位置设置为其他控件相对位置 - How to set control (windows control) position to some other control relative postion 矩形孔的尺寸不等于鼠标在计算机屏幕上的真实位置 - Rectangular hole dimensions isn't equals to real mouse position on my computer screen 使用RotateTransformed DrawString定位鼠标 - Position mouse with RotateTransformed DrawString 我们能否将所有控制事件都写在不同层的单独类文件中,而不是写在网页的代码隐藏中? - Can we write all the control events in a seperate class file in different layer rather than in code-behind of web-page? 鼠标单击事件上文本框结尾处的鼠标位置 - Mouse position at end of textbox on Mouse Click event 相对于窗体或屏幕定位UserControl - Positioning UserControl relative to Form or Screen 自定义大小调整为鼠标位置与光标位置不同 - Custom resizing to mouse position different then cursor position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM