我正在嘗試製作一些圖片,以便我看到許多Microsoft應用程序使用的很好的幻燈片。移動開始的那一個在那裏中途加速,然後在新的位置進入一個很好的緩慢停止。我已經計算出所有計算結果,獲取並設置圖片框位置,使用console.writeline
確認圖片位置正確,甚至可以使用簡化格式進行測試。Picturebox的位置每秒改變20次不會重新繪製
但在完整版本中它不重繪圖像。事實上,腳本運行時看起來沒有任何事情發生。我試過Me.Refresh()
,Invalidate()
,Timer.Enabled = True/False
和Me.Update()
。這些都沒有工作。最後一步是最令人沮喪的:我最後打電話給我的SetPanelLocation()
方法,以確保面板最終位於最終位置,無論運動是否奏效。這個調用也沒有發生,即使在這個例程失敗之後,我可以從另一個用戶事件調用相同的方法,並且它再次開始工作,因爲沒有任何錯誤。
我創建了自己的PictureBox類,名爲clsFeedImageBox
,它繼承了PictureBox
,它包含此功能(以及其他功能)。每張圖片只有300x225像素,因此它們不是大量圖片,需要大量時間進行重繪。這個類的每個實例都在一個共同的Forms.SplitterPanel
中。我習慣使用很多評論,所以我把它們留在這裏,也許他們會添加一些光。
Public Class clsFeedImgBox
Inherits PictureBox
Private iRank As Integer 'rank in whatever feed this file gets put in
Private iRankTarget As Integer 'rank to move to when rank feed event starts
Private iTopStart As Integer 'starting top location before feed event
Private iTopTarget As Integer 'final Top location after feed event
Private WithEvents tMyTimer As New System.Timers.Timer
Private WithEvents oParent As FeedBase 'splitter panel, all location info comes from the parent
Public Sub New(ByRef sender As FeedBase, ByVal rank as Integer)
'set objects
oParent = sender
'set .Image property to pre-made thumbnail
Image.FromFile(ThumbPath) 'ThumbPath is a property which is set by this point (some code has been removed)
'setup initial position
setPanelLocation(rank)
'set autosize
Me.SizeMode = PictureBoxSizeMode.StretchImage
'set Image Scroll timer interval to 20 fps (1000/20 = 50)
tMyTimer.Interval = 50
End Sub
Public Sub scroll(ByVal newRank As Integer)
'setPanelLocation(newRank) <== this works, timed movements don't
iRankTarget = newRank
iTopStart = Me.Top
iTopTarget = oParent.ImgTop(newRank) 'gets an integer for the new Top location
tMyTimer.Start()
End Sub
Private Sub myScrollStep() Handles tMyTimer.Elapsed
'tMyTimer.Enabled = False 'this idea with the enabled = True at the end didn't work
iTickCount += 1
Dim iScrollPerc As Integer 'scroll % between Start and End * 100
iScrollPerc = oParent.ScrollStep(iTickCount, Rank) 'this part works
Console.WriteLine(strThumbName & " scrollPerc: " & iScrollPerc.ToString)
If iScrollPerc >= 100 Then
'scroll event complete
Console.WriteLine(strThumbName & " SetFinalLocation")
Me.setPanelLocation(iRankTarget) '<== This line doesn't work here, but works when called by other means
'stop Feed updates
tMyTimer.Stop()
'reset iTickCount for next movement
iTickCount = 0
Else
'scrolling still going
Dim newTop As Integer
newTop = Math.Round(iTopTarget - (((100 - iScrollPerc) * (iTopTarget - iTopStart))/100)) 'this part works
'Console.WriteLine(strThumbName & " TopTarget: " & newTop)
Me.Top = newTop 'Nothing happens here
End If
'Me.Left = oParent.ImgLeft
'Me.Width = oParent.ImgWidth
'Me.Height = oParent.ImgHeight 'that didn't work
'Me.Refresh() 'this didn't work
'Invalidate() 'this didn't do much good either
'Me.Update() 'Aaaaand no cigar, time for StackOverflow
'tMyTimer.Enabled = True
End Sub
Public Sub setPanelLocation(ByVal rank As Integer)
iRank = rank
Me.MyRePaint()
End Sub
Public Sub MyRePaint()
'repaint image box with everything in it's current rank
Me.Left = oParent.ImgLeft
Me.Top = oParent.ImgTop(iRank)
Me.Width = oParent.ImgWidth
Me.Height = oParent.ImgHeight
End Sub
End Class
什麼給了?必須有一些VB.NET的內部工作,這將幫助我弄清楚這一點。我使用VS 2012和Win8的