如何在文本框或标签内移动文本 [英] How do I move text inside the textbox or label

查看:132
本文介绍了如何在文本框或标签内移动文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..



如何在Windows窗体应用程序中的文本框或标签内移动文本。

文本应向上移动它会在新闻标题中向下移动。

请不要告诉html的代码,只告诉c#.net的Windows应用程序表单。

Hi..

How do I move text inside the textbox or label in windows form application.
The text should move up to down like in news headlines it moves.
Please do not tell the code for html, tell only for windows application form of c# .net.

推荐答案

创建一些数据模型,例如,有一些文本和垂直移位像素。此数据的图形呈现应显示垂直移动的文本。你可以循环方式,或添加新文本,你需要的任何东西。



现在,创建一个自定义控件。您需要在其重写方法 System.Windows.Forms.Control.OnPaint 中呈现此图形。对于绘图,请使用此方法的event参数中传递给您的 System.Drawing.Graphics 实例。用字符串数据绘制文本并移动。



现在你需要为它设置动画。创建一个单独的线程。线程应该改变移位,如果需要,还应该改变文本。换句话说,它应该将数据提供给您的UI。如何触发绘画事件?它是通过在自定义控件上调用 System.Windows.Forms.Control.Invalidate 来完成的。但你不能直接这样做。



你不能从非UI线程调用与UI相关的任何东西。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



要防止闪烁,请使用双缓冲



有关更多细节和一些背景知识图形渲染,另见我过去的答案:

什么样的好玩方法是油漆? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



进一步有关使用线程的UI和图形的建议,请参阅我过去的答案

如何加快我的vb.net应用程序? [ ^ ],

在C#.net鼠标滚轮中缩放图像 [ ^ ]。



-SA
Create some data model which has, for example, some text and vertical shift in pixels. Graphical rendering of this data should show the text shifted vertically. You can do it in cyclic manner, or add new text, anything you need.

Now, create a custom control. You need to render this graphics in its overridden method System.Windows.Forms.Control.OnPaint. For drawing, use the instance of System.Drawing.Graphics passed to you in the event argument parameter of this method. Draw the text with string data and shift.

Now you need to animate it all. Create a separate thread. The thread should change the shift and, if required, the text. In other words, it should feed data to your UI. How to trigger painting event? It is done by calling System.Windows.Forms.Control.Invalidate on your custom control. But you cannot do it directly.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

To prevent flicker, use double buffering.

For some further detail and some background on graphics rendering, see also my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

For some further advice on using UI and graphics with threads, please see also my past answers
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

—SA


这篇关于如何在文本框或标签内移动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆