2014-03-06 110 views
0

我有一個項目,嘗試使一個酒吧跟隨我的鼠標左右使用計時器的圖片框。還需要考慮我考慮移動鼠標的速度。我試圖通過我的矩形做下面的代碼不會移動?爲什麼?DrawnObject不會隨着鼠標在計時器內移動

Dim p as rectangle 

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint 
    Dim g As System.Drawing.Graphics 
    g = e.Graphics 

    Dim PaddleSize As New Size(50, 10) 
    Dim MystartingPoint As New Point(225, PictureBox1.Height - 50) 
    p = New Rectangle(MystartingPoint, PaddleSize) 
    g.FillRectangle(Brushes.OliveDrab, p)  
End Sub 

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick 
    p.Location = Me.PointToClient(New Point(MousePosition.X - 25, PictureBox1.Height - 50)) 
    pictureBox1.refresh()  
End Sub 
+0

您的編輯刪除了問題,所以我將其回滾。如果你試圖移動標籤,那將是一個不同的問題。 – LarsTech

回答

0

你的出發點永遠不會改變(它總是在225 x高 - 50),因爲你忽略Tick事件的信息。

我的猜測是,你正試圖使槳來回走,用鼠標在屏幕上移動,所以嘗試這樣的事:

Private p As Point 

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _ 
    Handles PictureBox1.Paint 
    e.Graphics.FillRectangle(Brushes.OliveDrab, New Rectangle(p, New Size(50, 10))) 
End Sub 

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) _ 
    Handles Timer1.Tick 
    p = New Point(PictureBox1.PointToClient(New Point(MousePosition.X - 25, 0)).X, 
       PictureBox1.Height - 50) 
    PictureBox1.Refresh() 
End Sub 

你不希望包括在該PictureBox.Height - 50 PointToClient計算,因爲您確實希望槳從控件的底部始終保持50像素。