2016-05-12 7 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の全体のモニタサイズは、フォームサイズ(640x480)のみを使用したいのですが、

ここに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 
関連する問題