1

表示されるテキストの Windows フォームの幅を計算する必要があります。フォームの幅は明らかにピクセル単位であるため、テキストの幅をピクセル単位で取得したいだけですが、これは機能しません。

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

計算されたフォームの幅が小さすぎます (テキストが途切れます) - 何が起こっているのでしょうか? MeasureText はピクセルを使用し、フォームの幅はピクセル単位です。

これはうまく機能しますが、正しくないと思います:

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

助けていただければ幸いです。

AnimationPic - PictureBox、キャプションがラベルに設定されています

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

デザイナーの画像:

これは、デザイナーの進行状況ダイアログでどのように見えるかです

4

3 に答える 3

4

ではなくForm.Width、 を使用する必要がありますForm.ClientSize.Width

後者は、フォームのクライアント領域の実際の幅を返します。つまり、何かを描くことができる場所です。ドキュメントの備考セクションから:

フォームのクライアント領域のサイズは、枠線とタイトル バーを除いたフォームのサイズです。フォームのクライアント領域は、コントロールを配置できるフォーム内の領域です。このプロパティを使用して、グラフィック操作を実行するとき、またはフォーム上のコントロールのサイズ変更と配置を行うときに、適切な寸法を取得できます。フォーム全体のサイズを取得するには、 プロパティをSize使用するか、個々のプロパティHeightと を使用しますWidth

これをForm.Width(または) と比較すると、画面に表示されるフォームの幅全体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 に答える