2013-08-06 13 views
5

Bir formun boyutunu başlık/başlık metninin boyutuna uyarlamanın bir yolu var mı? ÖrneğinBir formun boyutunu C'deki başlık metnine uyar C#

, Kutu formları başlığı metnin boyutuna göre ayarlanır resmi C# Mesajı (lorem ipsum Not):

Normal Message Box

Diğer formlar bu onların boyutunu ayarlamak olmaz onların başlık metni:

Other form

Bunun yerine, üç nokta sonuna eklenir t "boyutu" özelliğinde belirtildiği boyutuna uyacak şekilde o tasarımcı.

Formu, bahsettiğimiz boyut yerine başlığın boyutuna göre ayarlamanın bir yolu var mı? Değilse, metnin tüm uzunluğunu elde etmenin bir yolu var, böylece forma atayabilir miyiz?

I

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

Ancak this.Font görünüşte bir yazı tipi boyutu belirtmektedir kullanarak form genişliğini ayarlama çalıştı.

+5

Muhtemelen 'SystemFonts.CaptionFont'unu kullanmak istiyorsunuz. Ayrıca, Width'un sınırları, simge, simge durumuna küçült/maksimize/kapama düğmelerini ve bunların arasındaki dolgu/kenar boşluklarını da hesaba katması gerektiğini unutmayın. – JosephHirn

+1

İpucu için teşekkürler.C düğmesinin X düğmesinin genişliğini ve etrafındaki dolguyu bilmenin yolları olduğunu biliyorum. Cevabı bildiğim anda bir cevap göndereceğim. – CydrickT

cevap

7

Tam cevap isteyenler için, işte burada.

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

AllButtonsAndPadding tüm düğmelerin genişliğine (en aza indirmek, en üst düzeye çıkarmak ve kapalı), pencere sınırları ve simge içerir:

başlık metnine göre formu yeniden boyutlandırılır gerçek satır aşağıdaki gibidir. Bu bilgileri almak biraz kodlama gerektirir. İşte, hangi düğmeleri veya simgeyi seçerseniz seçin, kendini yeniden boyutlandıran bir formun tam bir örneği.

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

Teşekkürler @CydrickT, bu yardımcı oldu. Bunu, FormBorderStyleFixedDialog ile bir pencereye uygularken ve formda ControlBox == false varsa (atılan hatayı işlemek için yakalamayı deneyin) gereken bazı değişikliklere sahibim.

Ayrıca, bazı FormBorderStyle'lar belirtilse ve simgesi belirtilmiş olsa bile, bu kod sürümü bunu ayarlayabilmek için bunları görüntülemez (kodunuzun bazı mantığı temel aldığı ShowIcon == true olsa bile).

Ayrıca, yapılandırıcıda ayarlanan pencerenin minimum genişliğini, belirtildiyse veya belirtilen tasarım süresi genişliğinde minimum genişliğe ayarlayan yeni bir özel özellik ekledim. Bu, metnin (başlık) kodda değiştirilmiş olması ve sonra formun yeniden gösterilmesi durumunda pencerenin genişliğinin daralmasına izin verir.

Formun metni için bir metin değiştirilmiş yöntem ekledim: , böylece formun başlık metni değiştirildiği zaman form genişliği ayarlandığında (form örneğini yeniden kullanırsanız) yeniden ayarlayın. Formun tasarımcısı elbette, kullanılacak Metin Değiştirme olayı için bunu ayarlamanız gerekir.

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; 
      } 
     } 
    } 
} 
İlgili konular