1

I need to calculate Windows Form Width for displayed text. The form width is obviously in pixels so you would just want to obtain the width of the text in pixels but this doesn't work:

animationForm.Width = TextRenderer.MeasureText(_caption, animationForm.Font).Width;

The calculated form width is way too small (cuts off text) - what is going on here? MeasureText uses pixels, form width is in pixels.

This works better but I don't think is right:

animationForm.Width = (int)(_caption.Length * animationForm.Font.Size);

Help would be appreciated.

AnimationPic - PictureBox, Caption is set on a Label

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PrlSystems.MaimInvoicing.Forms
{
    public partial class ProgressIndicatorForm : Form
    {
        private volatile bool _animating = false;
        private Form _parent;
        private string _caption = "";

        private object _mutex = new object();

        public ProgressIndicatorForm(Form parent)
        {
            InitializeComponent();
            _parent = parent;
        }

        public string Caption
        {
            set { _caption = value; }
            get { return _caption; }
        }

        public void StartAnimation()
        {
            lock (_mutex)
            {
                if (!_animating)
                {
                    if (_parent != null)
                    {
                        _parent.Cursor = Cursors.WaitCursor;
                        _parent.Enabled = false;
                    }

                    _animating = true;
                    _SpawnAnimationThread();
                }
            }
        }

        public void StopAnimation()
        {
            lock (_mutex)
            {
                if (_animating)
                {
                    _animating = false; // stop animation thread

                    if (_parent != null)
                    {
                        // restore parent form UI
                        _parent.Cursor = Cursors.Default;
                        _parent.Enabled = true;
                        _parent.Activate();
                    }
                }
            }
        }

        private void _SpawnAnimationThread()
        {
            Thread animationThread = new Thread(new ThreadStart(_Animate));
            animationThread.Priority = ThreadPriority.Normal;
            animationThread.IsBackground = true;    // should terminate when application ends
            animationThread.Start();
        }

        private void _Animate()
        {
            ProgressIndicatorForm animationForm = new ProgressIndicatorForm(_parent);
            animationForm.CaptionLabel.Text = _caption;
            animationForm.StartPosition = FormStartPosition.CenterScreen;
            animationForm.TopMost = true;
            animationForm.Width = _CalculateFormWidth(animationForm);

            animationForm.Show();

            while (_animating)
            {
                animationForm.CaptionLabel.Text = _caption;
                animationForm.Width = _CalculateFormWidth(animationForm);

                animationForm.BringToFront();
                animationForm.Refresh();
            }
            animationForm.Close();
            animationForm.Dispose();
        }

        private int _CalculateFormWidth(ProgressIndicatorForm animationForm)
        {
            int width = AnimationPic.Location.X + AnimationPic.Width +
                TextRenderer.MeasureText(_caption, animationForm.Font).Width;


            return width;
        }
    }
}

Image in the designer:

This is how it looks in the designer, the progress dialog

4

3 回答 3

4

而不是Form.Width,你需要使用Form.ClientSize.Width.

后者将返回表单客户区的实际宽度;即,你可以画东西的地方。从文档中的备注部分:

表单客户区的大小是不包括边框和标题栏的表单大小。窗体的客户区是窗体中可以放置控件的区域。在执行图形操作或在窗体上调整和定位控件时,您可以使用此属性来获取正确的尺寸。要获取整个表单的大小,请使用Size属性或使用单个属性HeightWidth.

Form.Width(or Form.Size.Width) 相比,它返回​​屏幕上显示的表单的整个宽度,包括非客户区中的多余内容,如窗口边框。绝对不要浪费任何时间尝试手动计算和删除这些项目的尺寸;它已经为你完成了。


使用以下代码:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    string longStringOfText = "This is a really long string of text we're using for testing purposes.";

    // Resize the form's client area, keeping the same height, but
    // changing the width to match that needed to draw the text.
    this.ClientSize = new Size(TextRenderer.MeasureText(longStringOfText,
                                                        this.Font).Width,
                               ClientSize.Height);

    // Then draw in the text.
    TextRenderer.DrawText(e.Graphics,
                          longStringOfText,
                          this.Font,
                          Point.Empty,
                          Color.Purple);
}

我明白了:

表单,文本字符串正确地适合它

在我看来还不错...

于 2011-05-18T13:55:57.070 回答
0

您还需要考虑额外的尺寸,例如表格的宽度。标签周围的空间(如果您使用的是标签?)

于 2011-05-18T13:53:33.407 回答
0

您是否尝试过指定设备上下文?试试这个方法重载MeasureText(IDeviceContext, String, Font, Size)

使用Form.Width时,宽度还包含窗口边框的宽度。尝试这个:

animationForm.ClientSize.Width = TextRenderer.MeasureText(_caption, animationForm.Font).Width;
于 2011-05-18T13:56:40.567 回答