zoukankan      html  css  js  c++  java
  • C#如何MeasureString、Graphics获取字符串的像素长度

    1. 当单元格展示的字符串需要自动换行的时候,使用GDI绘制文本信息,需要计算字符串文本的实际高度信息(需要固定宽度

    方法一:代码如下,会出现文本没有挤满当前行,但是文本实际高度已换行。

            private void btnCalc_Click(object sender, EventArgs e)
            {
    
                Graphics g = this.CreateGraphics();
                // 指定字符串格式
                StringFormat sf = new StringFormat();
                //StringFormat sf = StringFormat.GenericTypographic;
                sf.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
                // 1. 计算字符串的高度和宽度(不限定高度和宽度)
                SizeF sizeF = g.MeasureString(txtStr.Text, CreateFont(txtFontName.Text,
                    Convert.ToSingle(txtFontSize.Text)), new PointF(0,0) , sf);
    
                txtStrHeight.Text = sizeF.Height.ToString();
                txtStrWidth.Text = sizeF.Width.ToString();
                // 1. 计算字符串的高度和宽度(限定宽度,则高度存在换行))
                SizeF sizeF2 = g.MeasureString(txtStr.Text, CreateFont(txtFontName.Text,
                    Convert.ToSingle(txtFontSize.Text)),50, sf);
                //txtStrHeight.Text = sizeF.Height.ToString();
                //txtStrWidth.Text = sizeF.Width.ToString();
    
            }
    
            Font CreateFont(string fontName,float fontSize)
            {
                return new Font(fontName, fontSize, GraphicsUnit.Pixel);
            }
    

     修改上面出现的问题:

            private void btnCalc_Click(object sender, EventArgs e)
            {
    
                Graphics g = this.CreateGraphics();
                // 指定字符串格式
                StringFormat sf = StringFormat.GenericTypographic;
                //StringFormat sf = StringFormat.GenericTypographic;
                sf.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
                // 1. 计算字符串的高度和宽度(不限定高度和宽度)
                SizeF sizeF = g.MeasureString(txtStr.Text, CreateFont(txtFontName.Text,
                    Convert.ToSingle(txtFontSize.Text)), new PointF(0,0) , sf);
    
                txtStrHeight.Text = sizeF.Height.ToString();
                txtStrWidth.Text = sizeF.Width.ToString();
                // 1. 计算字符串的高度和宽度(限定宽度,则高度存在换行))
                SizeF sizeF2 = g.MeasureString(txtStr.Text, CreateFont(txtFontName.Text,
                    Convert.ToSingle(txtFontSize.Text)),50, sf);
                //txtStrHeight.Text = sizeF.Height.ToString();
                //txtStrWidth.Text = sizeF.Width.ToString();
    
            }
    
            Font CreateFont(string fontName,float fontSize)
            {
                return new Font(fontName, fontSize, GraphicsUnit.Pixel);
            }
  • 相关阅读:
    1407251735-hd-美素数.cpp
    [Javascript] IO Functor
    [AngularJS] Test an Angular Component with $componentController
    [AngularJS] Isolate State Mutations in Angular Components
    [Jest] Track project code coverage with Jest
    [Javascript] Either Functor
    [Javascript] Maybe Functor
    [AngualrJS] ng-strict-di
    [Ramda] Simple log function for debugging Compose function / Using R.tap for logging
    [Jest] Test JavaScript with Jest
  • 原文地址:https://www.cnblogs.com/runningRain/p/12936475.html
Copyright © 2011-2022 走看看