使用自动换行垂直滚动文本(Scroll text vertically with word wrap)

编程入门 行业动态 更新时间:2024-10-17 04:58:06
使用自动换行垂直滚动文本(Scroll text vertically with word wrap)

我有一些可变长度的文本,必要时,我想垂直滚动。 可以在此处看到一个示例: https : //social.msdn.microsoft.com/Forums/vstudio/en-US/77be2b96-b4b4-462c-b6d0-66c2c9739420/scroll-text-vertically此示例中的解决方案存在问题是它不包装文字。 我也看到过在矩形内部绘制文本的示例,然后移动矩形以产生滚动文本的效果,但我不认为这对我有效。 我有上下控件,我想滚动文本,文本高度高于屏幕高度。

我目前的OnPaintEvent:

private void JvsMessageLabelPaint(object sender, PaintEventArgs e) { e.Graphics.DrawString(this.mLabelToScrollVertically.Text, this.mLabelToScrollVertically.Font, new SolidBrush(Color.Black), this.mLabelToScrollVertically.Location.X, yPositionForLabel); yPositionForLabel -= 5; if (yPositionForLabel < this.mLabelToScrollVertically.Location.Y) { yPositionForLabel = this.mLabelToScrollVertically.Height; } }

I have some variable length text that, when necessary, I would like to scroll vertically. An example can be seen here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/77be2b96-b4b4-462c-b6d0-66c2c9739420/scroll-text-vertically The problem with the solution in this example is that it does not word wrap the text. I have also seen examples where text is painted inside of a rectangle and then the rectangle is moved to produce the effect of scrolling text but I don't think this will work for me. I have controls above and below where I want to scroll the text and the text height is taller than the height of the screen.

My current OnPaintEvent:

private void JvsMessageLabelPaint(object sender, PaintEventArgs e) { e.Graphics.DrawString(this.mLabelToScrollVertically.Text, this.mLabelToScrollVertically.Font, new SolidBrush(Color.Black), this.mLabelToScrollVertically.Location.X, yPositionForLabel); yPositionForLabel -= 5; if (yPositionForLabel < this.mLabelToScrollVertically.Location.Y) { yPositionForLabel = this.mLabelToScrollVertically.Height; } }

最满意答案

不确定您是否可以使用它,但这是使用ScrollBars创建Control的常见解决方法:

将Panel 放在另一个Panel 。 使外部Panel AutoScroll=true并将内部的Height更改为您测量要采用的文本的Height 。

这是Paint事件和结果:

private void pan_text_Paint(object sender, PaintEventArgs e) { string text = yourLongText; SizeF size = e.Graphics.MeasureString(text, Font, pan_text.ClientSize.Width); pan_text.Height = (int) size.Height; e.Graphics.DrawString(text, Font, Brushes.Black, new RectangleF(PointF.Empty, size) ); } 如果您想要滚动动画的用户不是ScrollBars ,那么同样的技巧也可以。

只是不要使外Panel AutoScroll并添加一个Timer :

private void StartButton_Click(object sender, EventArgs e) { timer1 = new Timer(); timer1.Interval = 15; timer1.Tick += timer1_Tick; timer1.Start(); } void timer1_Tick2object sender, EventArgs e) { pan_image.Top -= 2; if (pan_image.Top < -pan_image.Height) pan_image.Top = 0; } 事实上,对于一个简单的动画,你根本不需要外Panel !

只需定义一个类变量来保存您想要绘制文本的当前位置并使用此Tick事件:

int curTop = 0; void timer1_Tick(object sender, EventArgs e) { curTop -= 2; if (curTop < -pan_image.Height) curTop = 0; pan_image.Invalidate(); }

现在,如果您在DrawString使用它,将显示相同的效果:

e.Graphics.DrawString(text, Font, Brushes.Black, new RectangleF(new PointF(0, curTop), size) );

(当然,由于Panel不再包含在外部,你不会改变它的Height !)

在此处输入图像描述

Not sure if you can use it but here is a common workaround to create a Control with ScrollBars:

Place the Panel inside another Panel . Make the outer Panel AutoScroll=true and change the Height of the inner one to what you have measured the text to take..

Here is the Paint event and the result:

private void pan_text_Paint(object sender, PaintEventArgs e) { string text = yourLongText; SizeF size = e.Graphics.MeasureString(text, Font, pan_text.ClientSize.Width); pan_text.Height = (int) size.Height; e.Graphics.DrawString(text, Font, Brushes.Black, new RectangleF(PointF.Empty, size) ); } If instead of ScrollBars for the user you want a scrolling animation, the same trick will work.

Just don't make the outer Panel AutoScroll and add a Timer:

private void StartButton_Click(object sender, EventArgs e) { timer1 = new Timer(); timer1.Interval = 15; timer1.Tick += timer1_Tick; timer1.Start(); } void timer1_Tick2object sender, EventArgs e) { pan_image.Top -= 2; if (pan_image.Top < -pan_image.Height) pan_image.Top = 0; } In fact for a simple animation you don't need the outer Panel at all!

Simply define a class variable to hold the current position you want to draw the text at and use this Tick event:

int curTop = 0; void timer1_Tick(object sender, EventArgs e) { curTop -= 2; if (curTop < -pan_image.Height) curTop = 0; pan_image.Invalidate(); }

Now if you use it in the DrawString the same effect will show:

e.Graphics.DrawString(text, Font, Brushes.Black, new RectangleF(new PointF(0, curTop), size) );

(Of course, since the Panel is no longer contained in the outer one you don't change its Height!)

enter image description here

更多推荐

本文发布于:2023-08-06 18:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:换行   文本   Scroll   text   word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!