2013-08-06 47 views
5

Có cách nào để điều chỉnh kích thước của biểu mẫu với kích thước văn bản tiêu đề/chú thích không?Điều chỉnh kích thước của biểu mẫu thành văn bản tiêu đề trong C#

Ví dụ, chính thức C# tin nhắn dạng Box sẽ điều chỉnh với kích thước của văn bản tiêu đề của nó (Lưu ý lorem ipsum):

Normal Message Box

hình thức khác sẽ không điều chỉnh kích thước của chúng như của họ tiêu đề văn bản:

Other form

Thay vào đó, một dấu chấm lửng được thêm vào ở cuối để phù hợp với kích thước được đề cập trong "Kích" bất động sản của t anh ấy thiết kế.

Có cách nào để làm cho biểu mẫu điều chỉnh theo kích thước của tiêu đề thay vì kích thước mà chúng tôi đề cập không? Nếu không, có cách nào để có được chiều dài đầy đủ của văn bản để chúng tôi có thể gán nó vào biểu mẫu không?

tôi đã cố gắng thiết lập chiều rộng của hình thức sử dụng

int topTextWidth = TextRenderer.MeasureText(this.Text, this.Font).Width; 
this.Width = topTextWidth; 

Nhưng this.Font rõ ràng đề cập đến một kích thước phông chữ.

+5

Có thể bạn muốn sử dụng 'SystemFonts.CaptionFont'. Ngoài ra, hãy nhớ rằng 'Chiều rộng' phải tính đến các đường viền, biểu tượng, các nút thu nhỏ/tối đa/đóng và các phần đệm/lề giữa chúng cũng vậy. – JosephHirn

+1

Cảm ơn gợi ý.Tôi biết có nhiều cách trong C# để biết chiều rộng của nút X và phần đệm xung quanh nó. Tôi sẽ đăng câu trả lời ngay sau khi tôi biết câu trả lời. – CydrickT

Trả lời

7

Dành cho những ai muốn có câu trả lời đầy đủ, tại đây.

Dòng thực tế mà thay đổi kích thước các hình thức theo quy định của văn bản chú thích như sau:

this.Width = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont).Width + AllButtonsAndPadding; 

AllButtonsAndPadding chứa chiều rộng của tất cả các nút (minimize, maximize và gần), các đường viền cửa sổ và biểu tượng. Bắt những thông tin này cần một chút mã hóa. Dưới đây là ví dụ đầy đủ về biểu mẫu tự thay đổi kích thước, bất kể bạn đặt nút hoặc biểu tượng nào.

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

namespace WindowAutoAdapt 
{ 
public partial class Form1 : Form 
{ 
    //A default value in case Application.RenderWithVisualStyles == false 
    private int AllButtonsAndPadding = 0; 
    private VisualStyleRenderer renderer = null; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; //A big text in the title 
     GetElementsSize(); 
     ResizeForm(); 
    } 

    //This gets the size of the X and the border of the form 
    private void GetElementsSize() 
    { 
     var g = this.CreateGraphics(); 

     // Get the size of the close button. 
     if (SetRenderer(VisualStyleElement.Window.CloseButton.Normal)) 
     { 
      AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width; 
     } 

     // Get the size of the minimize button. 
     if (this.MinimizeBox && SetRenderer(VisualStyleElement.Window.MinButton.Normal)) 
     { 
      AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width; 
     } 

     // Get the size of the maximize button. 
     if (this.MaximizeBox && SetRenderer(VisualStyleElement.Window.MaxButton.Normal)) 
     { 
      AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width; 
     } 

     // Get the size of the icon. 
     if (this.ShowIcon) 
     { 
      AllButtonsAndPadding += this.Icon.Width; 
     } 

     // Get the thickness of the left, bottom, 
     // and right window frame. 
     if (SetRenderer(VisualStyleElement.Window.FrameLeft.Active)) 
     { 
      AllButtonsAndPadding += (renderer.GetPartSize(g, ThemeSizeType.True).Width) * 2; //Borders on both side 
     } 
    } 

    //This resizes the form 
    private void ResizeForm() 
    { 
     this.Width = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont).Width + AllButtonsAndPadding; 
    } 

    //This sets the renderer to the element we want 
    private bool SetRenderer(VisualStyleElement element) 
    { 
     bool bReturn = VisualStyleRenderer.IsElementDefined(element); 

     if (bReturn && renderer == null) 
      renderer = new VisualStyleRenderer(element); 
     else 
      renderer.SetParameters(element); 

     return bReturn; 
    } 
} 
} 
0

Cảm ơn @CydrickT, đó là hữu ích. Tôi có một số sửa đổi cần thiết khi áp dụng điều này cho cửa sổ với FormBorderStyle của FixedDialog và nếu ControlBox == false trên biểu mẫu (cần thử bắt để xử lý lỗi được ném).

Ngoài ra mặc dù biểu tượng được chỉ định, một số FormBorderStyle không hiển thị chúng (mặc dù ShowIcon == đúng là mã của bạn dựa trên một số logic) nên phiên bản mã này điều chỉnh cho điều đó.

Tôi cũng đã thêm thuộc tính riêng mới có chiều rộng tối thiểu của cửa sổ được đặt trong hàm tạo thành chiều rộng tối thiểu nếu được chỉ định hoặc chiều rộng thiết kế nếu không. Điều này cho phép co chiều rộng của cửa sổ nếu nó có văn bản (tiêu đề) thay đổi trong mã và sau đó biểu mẫu được hiển thị lại.

Tôi đã thêm phương thức văn bản đã thay đổi cho văn bản của biểu mẫu: sao cho bất kỳ lúc nào văn bản tiêu đề của biểu mẫu được thay đổi chiều rộng biểu mẫu điều chỉnh (lại, nếu sử dụng lại mẫu biểu mẫu). Tất nhiên, nhà thiết kế của biểu mẫu sẽ cần thiết lập điều này cho sự kiện Text Changed được sử dụng.

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Windows.Forms.VisualStyles; 

namespace WindowAutoAdapt 
{ 
    public partial class Form1: Form 
    { 
     private int AllButtonsAndPadding = 10;//seems things are coming up about this amount short so. . . 
     private VisualStyleRenderer renderer = null; 
     private int minWidth = 0;//will hold either the minimum size width if specified or the design time width of the form. 

     public Form1() 
     { 
      InitializeComponent(); 

      //Capture an explicit minimum width if present else store the design time width. 
      if (this.MinimumSize.Width > 0) 
      { 
       minWidth = this.MinimumSize.Width;//use an explicit minimum width if present. 
      } 
      else 
      { 
       minWidth = this.Size.Width;//use design time width 
      } 

      GetElementsSize(); 
      ResizeForm(); 
     } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void Form1_TextChanged(object sender, EventArgs e) 
    { 
     GetElementsSize(); 
     ResizeForm(); 
    } 

     //This gets the size of the X and the border of the form 
     private void GetElementsSize() 
     { 
      var g = this.CreateGraphics(); 

      // Get the size of the close button. 
      if (SetRenderer(VisualStyleElement.Window.CloseButton.Normal)) 
      { 
       AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width; 
      } 

      // Get the size of the minimize button. 
      if (this.MinimizeBox && SetRenderer(VisualStyleElement.Window.MinButton.Normal)) 
      { 
       AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width; 
      } 

      // Get the size of the maximize button. 
      if (this.MaximizeBox && SetRenderer(VisualStyleElement.Window.MaxButton.Normal)) 
      { 
       AllButtonsAndPadding += renderer.GetPartSize(g, ThemeSizeType.True).Width; 
      } 

      // Get the size of the icon only if it is actually going to be displayed. 
      if (this.ShowIcon && this.ControlBox && (this.FormBorderStyle == FormBorderStyle.Fixed3D || this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.FixedSingle)) 
      { 
       AllButtonsAndPadding += this.Icon.Width; 
      } 

      // Get the thickness of the left and right window frame. 
      if (SetRenderer(VisualStyleElement.Window.FrameLeft.Active)) 
      { 
       AllButtonsAndPadding += (renderer.GetPartSize(g, ThemeSizeType.True).Width) * 2; //Borders on both side 
      } 
     } 

     //This resizes the form 
     private void ResizeForm() 
     { 
      //widen window if title length requires it else contract it to the minWidth if required. 
      int newWidth = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont).Width + AllButtonsAndPadding; 
      if (newWidth > minWidth) 
      { 
       this.Width = newWidth; 
      } 
      else 
      { 
       this.Width = minWidth; 
      } 
     } 

     //This sets the renderer to the element we want 
     private bool SetRenderer(VisualStyleElement element) 
     { 
      try 
      { 
       bool bReturn = VisualStyleRenderer.IsElementDefined(element); 
       if (bReturn && renderer == null) 
       { 
        renderer = new VisualStyleRenderer(element); 
       } 
       else 
       { 
        renderer.SetParameters(element); 
       } 
       return bReturn; 
      } 
      catch (Exception) 
      { 
       return false; 
      } 
     } 
    } 
} 
Các vấn đề liên quan