本文介绍了如何在两个不相邻的区域上画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道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();
}

这篇关于如何在两个不相邻的区域上画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:29
查看更多