如何在两个不相邻的区域上画线

编程入门 行业动态 更新时间:2024-10-18 03:36:57
本文介绍了如何在两个不相邻的区域上画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不知道GDI32中是否有任何方法可以在表格的两个不相邻区域(例如矩形)上绘制字符串?.

I can not figure out if there is there any way in GDI32 to draw a string on two not adjacent areas (let's say Rectangles) on a form?.

如果到达第一个矩形的边缘,则文本应自动分割,如示例图片所示. 感谢您的帮助.

The text should automatically split if the edge of the first rectangle is reached as shown in the example picture. Thanks for your help.

推荐答案

一个示例,使用两个Labels和一个TrackBar. TrackBar确定一个标签内的字符串位置,每当该字符串移动时,该位置就会在第二个Label上反映出来. 此级联效果是使用.Invalidate()方法的第二个标签,该方法从第一个标签 Paint()事件.

An example, using two Labels and a TrackBar. The TrackBar determines a string position inside one Label, which reflects on the second Label each time the string is moved. This cascading effect is generated using the .Invalidate() method of the second Label, which is called from the first label Paint() event.

我只是使用 Graphics.MeasureString()和 Graphics.DrawString()此处. 您还可以使用相关的 TextRenderer 方法,但是在Label中,度量是相同的.

I'm just using Graphics.MeasureString() and Graphics.DrawString() here. You could also use the related TextRenderer methods, but in a Label the measures are the same.

结果的视觉表示:

A visual representaton of the result:

float stringLength = 0F; string loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; StringFormat MarqueeFormat = new StringFormat(StringFormatFlags.NoWrap | StringFormatFlags.FitBlackBox) { Alignment = StringAlignment.Near, Trimming = StringTrimming.None }; private void lblMarquee1_Paint(object sender, PaintEventArgs e) { SizeF stringSize = e.Graphics.MeasureString(loremIpsum, ((Control)sender).Font, -1, MarqueeFormat); PointF stringLocation = new PointF(trackBar1.Value, (((Control)sender).Height - stringSize.Height) / 2); stringLength = ((Control)sender).ClientRectangle.Width - stringLocation.X; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; e.Graphics.DrawString(loremIpsum, ((Control)sender).Font, Brushes.Black, stringLocation, MarqueeFormat); lblMarquee2.Invalidate(); } private void lblMarquee2_Paint(object sender, PaintEventArgs e) { SizeF stringSize = e.Graphics.MeasureString(loremIpsum, ((Control)sender).Font, -1, MarqueeFormat); PointF stringLocation = new PointF(-stringLength, (((Control)sender).Height - stringSize.Height) / 2); e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; e.Graphics.DrawString(loremIpsum, ((Control)sender).Font, Brushes.Black, stringLocation, MarqueeFormat); } private void trackBar1_ValueChanged(object sender, EventArgs e) { lblMarquee1.Invalidate(); }

更多推荐

如何在两个不相邻的区域上画线

本文发布于:2023-11-29 21:31:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区域   画线   两个   如何在

发布评论

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

>www.elefans.com

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