我现在在写一个控件要实现AutoSize功能,
用了系统自带的graphics.MeasureString和
TextRenderer.MeasureText
得到的字符串的宽都比实际的小的?
有什么方法可以精确的获取字符串的宽?

解决方案 »

  1.   

    GDI
    这个跟屏幕分辨率有关系的,楼主是否考虑了显示器的分辨率?
    对此不甚了解,等待dylike来解决... ...
      

  2.   

    http://www.cnblogs.com/nicch/archive/2008/11/05/1326754.html
    楼主参考一下。
      

  3.   

    多谢提醒,我在年初写的一个东西.现在顺便也发到我博客上去了./// <summary> 
    /// 获取字符串所需大小 
    /// </summary> 
    /// <param name="S"> 
    /// 指定的字符串 
    /// </param> 
    /// <param name="TarGetControl"> 
    /// 该字符串所在控件 
    /// </param> 
    /// <returns></returns> 
    /// <res></res> 
    public Size GetStringSize(string S, Control TarGetControl) 

        using (Graphics G = this.CreateGraphics) { 
            return new Size(G.MeasureString(S, new Font("宋体", 9, FontStyle.Regular, GraphicsUnit.Point), TarGetControl.Size).Width, G.MeasureString(S, new Font("宋体", 9, FontStyle.Regular, GraphicsUnit.Point), TarGetControl.Size).Height); 
        } 
      

  4.   

    設置一個Label控件的AutoSize為True,當你把你設置到你控件上的文本內容同時給此Label控件,Label的寬度,就基本上是你控件需要的寬度。這種方法比較簡單,不需要考慮其它特殊因素.
      

  5.   

    TitleFont=new Font("黑体",12);
                SolidBrush titleBrush = new SolidBrush(Color.Black);
                Point drawPoint = new Point();//标题字符串的左上点的位置            
                SizeF titleStr = tempRec.MeasureString(TitleName, TitleFont);
                StringFormat titleStrDrec = new StringFormat(StringFormatFlags.DirectionRightToLeft);//标题文本格式化
                int TitleWidth = (int)Math.Ceiling(titleStr.Width);//获取标题字符串的宽度
      

  6.   

    用这种方法,可以得到准确值:要考虑单位换算和控件内部边界单位换算问题:(1)字体大小的单位: pt(point)。 
        1磅 = 0.3527 毫米 
        1磅 = 1/72  英寸 
    (2)控件大小的单位:px(Pixel) 
        1像素 = 1/96  英寸(我们平时显示卡正常显示模式) C#控件    默认字体  9pt 
    Button控件 默认宽度 75 Button控件最多显示:75/96*72/9*2=12.5字符 实际去掉两边界空白各约6pt,实际能显示10字符(5个汉字). 如果控件确定、字体确定,边界是固定值,也可以得到。
      

  7.   


    public   void   MeasureStringMin(PaintEventArgs   e)   
      {   
      //   Set   up   string.   
      string   measureString   =   "Measure   String";   
      Font   stringFont   =   new   Font("Arial",   16);   
      //   Measure   string.   
      SizeF   stringSize   =   new   SizeF();   
      stringSize   =   e.Graphics.MeasureString(measureString,   stringFont);   
      //   Draw   rectangle   representing   size   of   string.   
      e.Graphics.DrawRectangle(   
      new   Pen(Color.Red,   1),   
      0.0F,   0.0F,   stringSize.Width,   stringSize.Height);   
      //   Draw   string   to   screen.   
      e.Graphics.DrawString(   
      measureString,   
      stringFont,   
      Brushes.Black,   
      new   PointF(0,   0));   
      }   
      

  8.   

    Brush foreBrush = Brushes.Blue;
    Font font = new Font("幼圆", Convert.ToInt16(40), FontStyle.Regular);
    Graphics g = this.CreateGraphics();
    string text = "";
    SizeF size = g.MeasureString(text, font);
    Single posX = (this.Width - Convert.ToInt16(size.Width)) / 2;
    Single posY = (this.Height - Convert.ToInt16(size.Height)) / 2;
    g.TranslateTransform(posX, posY);
    g.DrawString(text, font, foreBrush, 0, 0);
      

  9.   

    http://blog.csdn.net/coconutyf/archive/2009/05/11/4168545.aspx
      

  10.   

    我目前就是这个来完成的
    不知道它是怎么实现的MeasureString用这个函数特别是有中文+英文等情况计算出来的都是不准确的,
      

  11.   


    这种方法,只能用于同种控件做参照。如: label 和label , TextBox 和 TextBox ,Cell之间,同种字体,同种内容 label 与 TextBox 的显示宽度不同
      

  12.   

    我写了一个条码设计的程序我要获取输入文本的宽来写一个类似label功能的控件
      

  13.   

    在控件的padding留一定的宽度不就可以了嘛?做控件应该不要精确到几pt吧。
      

  14.   

    在有中文和英文的情况下,
    字体用的是Verdana
    字符串长一些的话误差就比较大了不知道label控件是怎么实现的!
      

  15.   

    用TextRenderer.MeasureText,字体从控件中取。
      

  16.   

    我用过的取得字符串的准确宽度的方法,稍做修改,还可以取得DataGridView中的列中的显示宽度。//返回字符宽度: controlName-显示控件 font-显示字体  str-显示的字符串
    private int GetWidth(String controlName,Font font, String str)
    {
        int width = -1;
        switch (controlName)
        {
            case "Button":
                {
                    Button btn = new Button();
                    btn.Font = font;
                    btn.Text = str;
                    btn.AutoSize = true;
                    btn.Visible = false;
                    this.Controls.Add(btn);
                    width = btn.Width;
                    btn.Dispose();
                    break;
                }
            case "Label":
                {
                    Label lbl = new Label();
                    lbl.Font = font;
                    lbl.Text = str;
                    lbl.AutoSize = true;
                    lbl.Visible = false;
                    this.Controls.Add(lbl);
                    width = lbl.Width;
                    lbl.Dispose();
                    break;
                }
            
            // case "...":   这里还可以扩展其它控件,或者找一个接近的代替。
        }
        return width;
    }// 测试用代码
    private void button8_Click(object sender, EventArgs e)
    {
        Font font = new Font("Verdana", 9, FontStyle.Regular);    MessageBox.Show("Label(中123)  宽:" + GetWidth("Label", "中123", font).ToString());
        MessageBox.Show("Button(中123) 宽:" + GetWidth("Button", "中123", font).ToString());
    }