2011-05-18 41 views
1

我需要計算顯示文本的Windows窗體寬度。 形式寬度顯然是在像素所以你只是想獲得文本的寬度,以像素爲單位,但是,這並不工作:計算顯示文本的Windows窗體寬度

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

計算的表格寬度來說太小了(切斷文本) - 這裏發生了什麼? MeasureText使用像素,表單寬度以像素爲單位。

這工作好,但我不認爲這是正確的:

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

幫助,將不勝感激。

AnimationPic - 圖片框, 標題上一個標籤組

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

形象設計師:

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

+0

我要澄清一些我遺漏的東西,但這很重要。我也有動畫片。以下是整個公式:int width = AnimationPic.Location.X + AnimationPic.Width + TextRenderer.MeasureText(_caption,animationForm.Font).Width; – ActiveX 2011-05-18 14:08:35

回答

0

您還需要考慮額外的尺寸,如寬度形成。標籤周圍的空間(如果您使用標籤?)

+0

但絕對不要嘗試手動執行此操作。使用爲此目的明確提供的屬性。 – 2011-05-18 13:58:57

+0

是的,我正在使用一個標籤......文本的嚴重損失(是截斷)告訴我還有其他錯誤。我上面發佈了完整的代碼。 – ActiveX 2011-05-18 15:05:37

4

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

後者將返回形式的客戶區的實際寬度;即你可以畫東西的地方。從文檔中的備註部分:

窗體客戶區的大小是窗體的大小,不包括邊框和標題欄。表單的客戶區域是可以放置控件的表單中的區域。您可以在執行圖形操作時或在窗體上調整大小和定位控件時使用此屬性來獲取適當的尺寸。要獲取整個表格的大小,請使用Size property或使用單個屬性HeightWidth

對比度與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); 
} 

我得到這個:

Form, with text string correctly fit across it

看起來不錯,我...

+0

Form.Width沒有被調用,它被設置... – Frosty840 2011-05-18 13:59:57

+0

@Frosty:你的意思是什麼?我是否使用「所謂」而不是「set」這個詞?我的不好,我會解決它。當然,這一點仍然是一樣的。您需要設置**客戶區**的大小,而不是整個表單的大小(包括非客戶區)。 – 2011-05-18 14:00:54

+0

夥計們,這是沒有任何邊界的動畫對話框,所以它並不重要。不過,無論我是否使用客戶區域,我仍然輸(截止因爲寬度太短)40%的文字。 – ActiveX 2011-05-18 14:01:46

0

您是否嘗試過指定的設備上下文? 嘗試使用此方法重載MeasureText(IDeviceContext, String, Font, Size)

當您使用Form.Width時,寬度也包含窗口邊框的寬度。試試這個:

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

我很難想象這裏的設備環境可能如何相關。 – 2011-05-18 13:58:35

+0

我試過了,沒關係。 – ActiveX 2011-05-18 14:07:45

+0

是否將AnimationPic直接放置在表單上,​​還是放在某個容器中? – 2011-05-18 14:21:37