2013-08-06 4 views
5

में किसी फॉर्म के आकार को अपने शीर्षक टेक्स्ट में एडाप्टर करें क्या किसी शीर्षक के आकार/कैप्शन टेक्स्ट के आकार में आकार बदलने का कोई तरीका है?सी #

उदाहरण के लिए, सरकारी सी बॉक्स रूपों इसके शीर्षक पाठ के आकार को समायोजित करेगा # संदेश (नोट lorem ipsum):

Normal Message Box

अन्य रूपों की है कि उनके आकार को समायोजित नहीं होंगे उनके शीर्षक पाठ:

Other form

इसके बजाय, एक अंडाकार अंत में जोड़ा जाता है आकार टी के "आकार" संपत्ति में उल्लेख किया है फिट करने के लिए वह डिजाइनर।

क्या हमारे द्वारा उल्लेख किए गए आकार के बजाय फ़ॉर्म को आकार के आकार में समायोजित करने का कोई तरीका है? यदि नहीं, तो पाठ की पूर्ण लंबाई प्राप्त करने का कोई तरीका है ताकि हम इसे फ़ॉर्म में असाइन कर सकें?

मैं

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

लेकिन this.Font जाहिरा तौर पर एक और फ़ॉन्ट आकार को संदर्भित करता है का उपयोग कर फार्म की चौड़ाई की स्थापना की कोशिश की।

+5

शायद आप 'SystemFonts.CaptionFont' का उपयोग करना चाहते हैं। यह भी ध्यान रखें कि 'चौड़ाई' को सीमाओं, आइकन, न्यूनतम/अधिकतम/बंद बटन, और उनके बीच पैडिंग/मार्जिन के लिए भी खाते हैं। – JosephHirn

+1

संकेत के लिए धन्यवाद।मुझे पता है कि एक्स बटन की चौड़ाई और इसके चारों ओर पैडिंग जानने के लिए सी # में तरीके हैं। जैसे ही मुझे जवाब पता है, मैं एक उत्तर पोस्ट करूंगा। – CydrickT

उत्तर

7

उन लोगों के लिए जो पूर्ण उत्तर चाहते हैं, यहां यह है।

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

AllButtonsAndPadding सभी बटन की चौड़ाई (छोटा करना, बड़ा और करीब), खिड़की सीमाओं और आइकन होता है:

वास्तविक लाइन है कि कैप्शन पाठ के अनुसार प्रपत्र का आकार बदलता है निम्नलिखित है। इन जानकारी को प्राप्त करना थोड़ा कोडिंग लेता है। यहां एक ऐसे फॉर्म का एक पूर्ण उदाहरण दिया गया है जो स्वयं का आकार बदलता है, इससे कोई फर्क नहीं पड़ता कि आपके द्वारा डाले गए बटन या आइकन।

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

धन्यवाद @CydrickT, यह सहायक था। मेरे पास कुछ संशोधन हैं जिन्हें FixedDialog के साथ विंडो में लागू करने के लिए आवश्यक था और यदि फॉर्म पर ControlBox == false (त्रुटि फेंकने के लिए प्रयास करने की आवश्यकता है)।

इसके अलावा और आइकन निर्दिष्ट भी है, कुछ FormBorderStyle उन्हें प्रदर्शित नहीं करते हैं (भले ही ShowIcon == सत्य है कि आपका कोड कुछ तर्क आधारित है) तो कोड का यह संस्करण उस के लिए समायोजित करता है।

मैंने एक नई निजी संपत्ति भी जोड़ा जो कि कम से कम चौड़ाई तक कन्स्ट्रक्टर में सेट की गई विंडो की न्यूनतम चौड़ाई रखती है या यदि डिजाइन की गई चौड़ाई नहीं है। यह विंडो की चौड़ाई के संकुचन की अनुमति देता है अगर उसके कोड (शीर्षक) को कोड में बदल दिया गया था और फिर फॉर्म फिर से चलाया गया था।

मैंने फ़ॉर्म के पाठ के लिए एक पाठ परिवर्तित विधि जोड़ा: ताकि किसी भी समय फॉर्म का शीर्षक टेक्स्ट बदल दिया जाए, फॉर्म चौड़ाई समायोजित हो (फिर, फॉर्म प्रपत्र का पुन: उपयोग करने पर)। कोर्स के डिजाइनर को इसे टेक्स्ट चेंज इवेंट के इस्तेमाल के लिए सेट करने की आवश्यकता होगी।

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