我正在尝试制作一些图像来做我见过很多 Microsoft 应用程序使用的漂亮的滑动的东西。运动开始缓慢的地方在中途加速,然后在它的新位置缓慢停止。我已经弄清楚了所有的计算,获取和设置图片框位置,使用console.writeline
图像位置正确的确认,甚至以简化格式运行的测试运行。
但在完整版本中,它不会重新绘制图像。事实上,在脚本运行时,似乎什么都没有发生。我试过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