当前位置:网站首页>将单行文字自动适应到目标矩形框内
将单行文字自动适应到目标矩形框内
2022-04-23 06:26:00 【大可山人】
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace JohnsonJu.CodeSystem.Helper
{
/// <summary>
/// 将单行文字自动适应到目标矩形框内
/// </summary>
public class FittingSingleLineWordsInRectF
{
string _words;
Font _fontWords;
RectangleF _destRectF = RectangleF.Empty;
/// <summary>
/// 获取或设置要自动适应的文字
/// </summary>
public string Words
{
get { return this._words; }
set { this._words = value; }
}
/// <summary>
/// 获取或设置文字的字体
/// </summary>
public System.Drawing.Font FontWords
{
get { return this._fontWords; }
set { this._fontWords = value; }
}
/// <summary>
/// 获取或设置目标矩形区
/// </summary>
public System.Drawing.RectangleF DestRectF
{
get { return this._destRectF; }
set { this._destRectF = value; }
}
/// <summary>
/// 构造器
/// </summary>
public FittingSingleLineWordsInRectF()
{
}
/// <summary>
/// 将单行文字自动适应到目标矩形框内的构造器
/// </summary>
/// <param name="words">单行文字</param>
/// <param name="font">文字的字体</param>
/// <param name="destRectF">目标矩形区</param>
public FittingSingleLineWordsInRectF(string words, Font font, RectangleF destRectF)
{
this._words = words;
this._fontWords = font;
this._destRectF = destRectF;
}
private GraphicsPath RetrieveWordsPath()
{
return this.RetrieveWordsPath(this._words, this._fontWords);
}
/// <summary>
/// 获取文字路径
/// </summary>
/// <param name="words">文字</param>
/// <param name="font">字体</param>
/// <returns>图形路径</returns>
private GraphicsPath RetrieveWordsPath(string words, Font font)
{
Rectangle rect = new Rectangle(0, 0, 9999, font.Height);
GraphicsPath gp = new GraphicsPath();
gp.AddString(words, font.FontFamily, (int)font.Style, font.Size, rect, null);
return gp;
}
/// <summary>
/// 得到返回图形路径
/// </summary>
/// <returns></returns>
public GraphicsPath RetrievePath()
{
return RetrievePath(this._destRectF, this.RetrieveWordsPath());
}
/// <summary>
/// 将图形路径自动适应到目标矩形区域
/// </summary>
/// <param name="destRectF">目标矩形区域</param>
/// <param name="gp">原来的图形路径</param>
/// <returns>自动适应后的图形路径</returns>
public GraphicsPath RetrievePath(RectangleF destRectF, GraphicsPath gp)
{
if (destRectF == RectangleF.Empty) return null;
PointF[] destPoints = new PointF[]{
new PointF(destRectF.Left, destRectF.Top),
new PointF(destRectF.Right, destRectF.Top),
new PointF(destRectF.Left, destRectF.Bottom),
new PointF(destRectF.Right, destRectF.Bottom)
};
RectangleF srcRect = gp.GetBounds();
gp.Warp(destPoints, srcRect);
GraphicsPath gpNew = (GraphicsPath)gp.Clone();
return gpNew;
}
public RectangleF MeasureTextRectangle(Graphics g, string measureString, Font font)
{
SizeF sizeF = g.MeasureString(measureString, font);
RectangleF rectF = new RectangleF(new PointF(0, 0), sizeF);
return rectF;
}
/// <summary>
/// 测量单行文本所占的矩形区域
/// </summary>
/// <param name="g"></param>
/// <param name="measureString"></param>
/// <param name="stringFont"></param>
/// <returns></returns>
public RectangleF MeasureCharacterRangesRegions(Graphics g, string measureString, Font stringFont)
{
CharacterRange[] characterRanges ={
new CharacterRange(0, measureString.Length)
};
float width = 99999F;
float height = stringFont.Height;
RectangleF layoutRect = new RectangleF(0, 0, width, height);
StringFormat stringFormat = new StringFormat();
//stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
stringFormat.SetMeasurableCharacterRanges(characterRanges);
// 绘制文字到画板
// g.DrawString(measureString, stringFont, Brushes.Black, 0, 0, stringFormat);
// 测量
Region[] stringRegions = new Region[1];
stringRegions = g.MeasureCharacterRanges(measureString, stringFont, layoutRect, stringFormat);
//绘制首次测量的矩形
RectangleF measureRect = stringRegions[0].GetBounds(g);
//g.DrawRectangle( new Pen(Color.Red, 1), Rectangle.Round(measureRect1));
return measureRect;
}
}
}
调用方法:
// rect: 您的矩形(区域) text: 您的文字 font:文字字体 g:您的画板(Graphics)
FittingSingleLineWordsInRectF fsw = new FittingSingleLineWordsInRectF();
fsw.DestRectF = new RectangleF(rect.X, rect.Y, rect, rect);
fsw.Words = text;
fsw.FontWords = font;
GraphicsPath gpText =fsw.RetrievePath();
g.FillPath(brushForColorBar, gpText);
版权声明
本文为[大可山人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/johnsuna/article/details/120382174
边栏推荐
- SAP PI/PO登录使用及基本功能简介
- SAP pi / PO rfc2soap publishes RFC interface as WS example
- Custom time format (yyyy-mm-dd HH: mm: SS week x)
- js之作用域、作用域链、全局变量和局部变量
- 驼峰命名对像
- 【自我激励系列】你永远不会准备好
- Learn to use search engines
- Moment. Format of format method function in JS
- 系统与软件安全研究(三)
- SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
猜你喜欢

SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)

SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)

Simple random roll call lottery (written under JS)

How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)

配置npm

MySQL index

SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)

SAP pi / PO rfc2soap publishes RFC interface as WS example

SAP 导出Excel文件打开显示:“xxx“的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?

Configure NPM
随机推荐
SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)
Authorization+Token+JWT
What are the total number of all courses of Mr. Tang Xiaoyang, who is very popular in CSDN (question mark)
MySQL storage engine
王者荣耀-unity学习之旅
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
12.约束
Understanding of STL container
[self motivation series] what really hinders you?
面经总结2
3. Sort statement
SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
手游性能优化
SAP PI/PO功能运行状态监控检查
Django uses MySQL database to solve error reporting
Implementation of MySQL persistence
Django使用mysql数据库报错解决
js中对象的三种创建方式
The difference and application of VR, AR and MR, as well as some implementation principles of AR technology
学会使用搜索引擎