从最后一个位置设置鼠标位置不正确 vb net [英] Set Mouse Position from the last position not correct vb net

查看:24
本文介绍了从最后一个位置设置鼠标位置不正确 vb net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图片框,我获取鼠标的当前位置并将它们存储在两个单独的标签中.我有一个按钮可以为鼠标设置一个新位置,但我得到的位置不正确.

I have a picture box and i get the current position of the mouse and store them in two separated labels. And I have a button to set a new position to the mouse but the position I get its not correct.

我明白了x = 399 y = 237

I get x = 399 y = 237

但是当我点击按钮将鼠标位置设置到这个位置时,我得到了x = 175 y = 175

But when I click the button to set the mouse position to this location i get x = 175 y = 175

这是我用来获取 x 和 y 的代码:

This is the code I use to get the x and y:

Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
    PPoint = New Point(e.X, e.Y)
    Label8.Text = e.X
    Label9.Text = e.Y
End Sub

这是我用来设置鼠标新位置的代码

And this is the code i use to set the new position to the mouse

Private Sub Button8_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button8.Click
    Cursor.Position = PPoint
End Sub

我尝试了不同的方法,但我继续遇到同样的问题.这是我从 Find Mouse Position<尝试的代码/a>

I have try a different way to do but i continue with the same problem. Tis is the code i have try from Find Mouse Position

推荐答案

这将在您单击的确切点上放置一个红点.我想知道设置光标位置会有多大用处,因为他们几乎肯定会在点击按钮后移动鼠标(无意或无意).

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).

设置光标位置需要在屏幕坐标中 - 这会转换回客户端坐标以进行绘图.我不相信 PointToClient 是光标位置所必需的.在下面的代码中,这是一个不必要的转换,因为您只是返回到客户端坐标.我留下它来展示每个转换的示例,以便您可以对其进行试验.

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.

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

这篇关于从最后一个位置设置鼠标位置不正确 vb net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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