计算Windows窗体宽度的显示的文本窗体、宽度、文本、Windows

2023-09-06 22:02:55 作者:易杯让人凯心的源味奶茶

我需要计算的Windows窗体的宽度显示的文本。 该表格​​的宽度显然是像素,所以你只是想获取像素文本的宽度,但不起作用:

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;

计算表格宽度的方式过小(切断文本) - 什么是怎么回事? MeasureText使用像素,表格宽度以像素为单位。

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);

帮助将AP preciated。

Help would be appreciated.

AnimationPic - 图片框, 标题设置在一个标签

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:

推荐答案

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

Rather than Form.Width, you need to use Form.ClientSize.Width.

后者将返回表格的客户区域的实际宽度;即,在这里你可以借鉴的东西的地方。从文档中的备注部分:

The latter will return the actual width of the form's client area; i.e., the place where you can draw stuff. From the Remarks section in the documentation:

窗体的客户区的大小是表格不包括边框和标题栏的大小。一种形式的客户区是一个形式,其中控制可以放在内的区域。您可以使用此属性执行图形操作或窗体上的大小和位置控制时,当得到适当的尺寸。为了让整个窗体的大小,使用 尺寸属性或使用各个属性的 身高 和的 宽度

The size of the client area of the form is the size of the form excluding the borders and the title bar. The client area of a form is the area within a form where controls can be placed. You can use this property to get the proper dimensions when performing graphics operations or when sizing and positioning controls on the form. To get the size of the entire form, use the Size property or use the individual properties Height and Width.

对比与 Form.Width (或 Form.Size.Width ),它返回的整个的宽度,因为它出现在屏幕上,其中包括多余的东西,在非客户区,如窗口边框形式。绝对不要浪费任何时间去计算并删除尺寸手动这些项目;它已经为你做好了。

Contrast that with Form.Width (or Form.Size.Width), which returns the entire width of the form as it appears on the screen, including superfluous stuff in the non-client area, like window borders. Definitely don't waste any time trying to calculate and remove the dimensions for these items manually; it's already been done for you.

使用下面的code:

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);
}

我得到这样的:

I get this:

看起来pretty的对我好......

Looks pretty good to me...