问题描述
有没有到窗体的大小适应其标题/标题文本大小的方式?
Is there a way to adapt the size of a form to the size of its title / caption text?
例如,官方C#消息框的形式将调整其标题文本的大小(注意的 Lorem存有的):
For example, official C# Message Box forms will adjust to the size of its title text (Note the lorem ipsum):
其他形式不会调整其大小,以他们的标题文字:
Other forms will not adjust their size to that of their title text:
相反,省略号加在最后,以适应在设计师的大小属性中提到的大小。
Instead, an ellipsis is added at the end to fit the size mentioned in the "Size" property of the designer.
有没有一种方法,使形式调整为标题,而不是我们提到的大小尺寸是多少?如果没有,有没有办法让文字的全长,使我们可以把它分配给表格?
Is there a way to make the form adjust to the size of the title instead of the size we mention? If not, is there a way to get the full length of the text so that we can assign it to the form?
我试着设置使用表格宽度
I tried setting the width of the form using
int topTextWidth = TextRenderer.MeasureText(this.Text, this.Font).Width;
this.Width = topTextWidth;
但 this.Font
显然指的是另一字体大小。
But this.Font
apparently refers to another font size.
推荐答案
对于那些谁想要一个完整的答案,在这里。
For those who wants a full answer, here it is.
这是根据字幕文本大小调整表格的实际线路如下:
The actual line that resizes the form according to the caption text is the following:
this.Width = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont).Width + AllButtonsAndPadding;
AllButtonsAndPadding
包含所有按钮的宽度(最小化,最大化和关闭),窗口边框和图标。获得这些信息需要多一点的编码。 。下面是自身的大小,不管你放什么按钮或图标形式的一个完整的例子
AllButtonsAndPadding
contains the width of all the buttons (minimize, maximize and close), the window borders and the icon. Getting these information takes a bit of coding. Here's a full example of a form that resizes itself, no matter what buttons or icon you put.
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;
}
}
}
这篇关于适应窗体的大小在C#中的标题文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!