平滑滚动文本(如突发新闻...) [英] Smoothly scroll text (like breaking news...)
问题描述
注意:创建这个问题是为了帮助其他人找到我找不到的东西,说过欢迎任何人提出比我想出的更好的解决方案。
Note: This question is created to help others find what I could not find, having said that anyone is welcome to propose a better solution than I came up with.
我的挑战是在Windows窗体应用程序上生成平滑的滚动文本流,此外,在窗体上绘制了其他项目,因此我必须避免在此过程中销毁它们。
My challenge was to produce a smooth scrolling text stream on a windows form application, in addition the form had other items drawn on it so I had to avoid destroying them in the process.
我尝试使用计时器和 do ... loops
等移动文本框来递增它,等等,还尝试使用 Graphics.DrawString
再次增加位置。
他们所有人都产生了一个生涩的输出,并且随着文本的移动而闪烁。
I tried moving a text box incrementing it with timers and do...loops
and so on, also tried using Graphics.DrawString
again incrementing the position.
All of them produced a jerky output with the text flashing as it moved.
我花了几个小时在谷歌上搜索和浏览大约50种建议的解决方案
I Spent several hours googling and browsing probably in the region of 50 different proposed solutions including many in other languages in the hope I might get a clue!
我无法确定是哪一个给了我线索,但是我看到了一些对<$ c $的引用c> Doublebuffer ,所以我用谷歌搜索了一下(没有成功就尝试了!),最后从此处是一个手动缓冲区,因此我最终得到了...。(请参见下面的答案)
I can't identify which one gave me the clue but I saw several references to Doublebuffer
so I googled that (and tried it without sucess!) and finally came up with the idea from here of a manual buffer so I ended up with....(see answer below)
推荐答案
Do
Dim currentContext As BufferedGraphicsContext
Dim myBuffer As BufferedGraphics
' Gets a reference to the current BufferedGraphicsContext.
currentContext = BufferedGraphicsManager.Current
' Creates a BufferedGraphics instance associated with Form1, and with
' dimensions the same size as the drawing surface of Form1.
myBuffer = currentContext.Allocate(BNP1.CreateGraphics, BNP1.DisplayRectangle)
'update test
With myBuffer
.Graphics.Clear(Panel.BackColor)
.Graphics.DrawString("Some Text", [Your Font], Brushes.Blue, xpos, ypos)
End With
myBuffer.Render()
myBuffer.Dispose()
'This bit will need adjusting to suit your layout!
If xpos + Panel.Width < Panel.Left Then xpos = Panel.Right Else xpos -= 1
Thread.Sleep(1)
Loop
使用睡眠时间和xpos减少功能,可以实现完全平滑的滚动速度和完全可调的滚动速度。
This produced a perfectly smooth scroll with a fully adjustable scroll speed using the sleep time and xpos reduction.
您可能会注意到我做了在单独的线程中使用 Do ... Loop
也可以在计时器中使用,但是由于某些原因(有人可能会解释),滚动速度要慢得多。
As you will probably notice I did this with a Do...Loop
in a separate thread it also works in a timer but the scroll speed for some reason (someone can probably explain) is much slower.
免责声明:我不是专业的编码人员,所以如果有人对我所做的工作有所改进,我将不胜感激(避免使用诸如 Option Strict之类的泛型
-已经开启!)
Disclaimer: I am not a professional coder so if anyone has suggestions to improve what I have done I will greatly appreciate it (avoid generics such as Option Strict
- already on!)
这篇关于平滑滚动文本(如突发新闻...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!