我有一些可变长度的文本,必要时,我想垂直滚动。 可以在此处看到一个示例: 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!)
更多推荐
发布评论