我是初学程序员,我正在学习使用VB在大学(英国)编写代码。
我在一个图片框的中心绘制了一个位图,这是一个箭头朝右的图像。旋转我用;
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
任何帮助将不胜感激
答案 0 :(得分:2)
在paint事件之外使鼠标变量全局化并以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