2016-05-12 104 views
0

我是一名初学者程序员,我正在学习使用VB在大学(英国)编码。使用鼠标位置旋转图像 - Visual Studio 2015

我有一个位图在一个图片框的中心绘制,这是一个箭头朝右的图像。要旋转我正在使用;

RAD = Math.Atan2(MOUSE_Y - CENTRE_Y, MOUSE_X - CENTRE_X) 
ANG = RAD * (180/Math.PI) 

正如你可以看到我想要使用MousePosition.Y & X上使用鼠标的位置,以便朝着老鼠,但箭头的角度箭头指向是关闭的,因为它是使用旋转图像X和Y的整个显示器尺寸,但我希望它只使用表格尺寸(640 x 480)

这是Picturebox1_Paint子;

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint 
    Dim MOUSE_X As Integer 
    Dim MOUSE_Y As Integer 
    Dim CENTRE_X As Integer 
    Dim CENTRE_Y As Integer 
    Dim BMP As Bitmap 
    Dim ANG As Integer = 0 
    Dim RAD As Double 
    Dim GFX As Graphics = e.Graphics 
    BMP = New Bitmap(My.Resources.ARROWE) 

    MOUSE_X = (MousePosition.X) 
    MOUSE_Y = (MousePosition.Y) 
    CENTRE_X = PictureBox1.Location.X + PictureBox1.Width/2 
    CENTRE_Y = PictureBox1.Location.Y + PictureBox1.Height/2 

    RAD = Math.Atan2(MOUSE_Y - CENTRE_Y, MOUSE_X - CENTRE_X) 
    ANG = RAD * (180/Math.PI) 

    GFX.TranslateTransform(PictureBox1.Height/2, PictureBox1.Width/2) 
    GFX.RotateTransform(ANG) 
    GFX.DrawImage(BMP, -30, -30, 60, 60) 
    GFX.ResetTransform() 

End Sub 

任何帮助,将不胜感激

回答

2

使鼠标变量油漆事件之外的全球和捕捉它们的形式MouseMove事件:

Private MOUSE_X As Integer 
Private MOUSE_Y As Integer 

Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove 
    MOUSE_X = e.X 
    MOUSE_Y = e.Y 
    PictureBox1.Refresh() 
End Sub  

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint 
    Dim CENTRE_X As Integer 
    Dim CENTRE_Y As Integer 
    Dim BMP As Bitmap 
    Dim ANG As Integer = 0 
    Dim RAD As Double 
    Dim GFX As Graphics = e.Graphics 
    BMP = New Bitmap(My.Resources.ARROWE) 

    CENTRE_X = PictureBox1.Location.X + PictureBox1.Width/2 
    CENTRE_Y = PictureBox1.Location.Y + PictureBox1.Height/2 

    RAD = Math.Atan2(MOUSE_Y - CENTRE_Y, MOUSE_X - CENTRE_X) 
    ANG = RAD * (180/Math.PI) 

    GFX.TranslateTransform(PictureBox1.Height/2, PictureBox1.Width/2) 
    GFX.RotateTransform(ANG) 
    GFX.DrawImage(BMP, -30, -30, 60, 60) 
    GFX.ResetTransform() 

End Sub