- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想将按钮添加到 FlowLayoutPanel。按钮可能包含较长的文本,单词之间有空格。按钮是 Autosize=true 和 AutoSizeMode = AutoSizeMode.GrowAndShrink。此外,我将 MaximumSize 属性设置为 (maxwidth,0)。 maxwidth 是面板的宽度。所以按钮不会变得太宽。
我看到的是,Button 的宽度受 MaximumSize 属性的限制,但是当发生文本换行时,Button 的高度不会自动调整为换行文本的高度。这个问题有解决方案吗?
我也试过像这样手动调整按钮的大小:
using (Graphics cg = this.CreateGraphics()) {
SizeF size = cg.MeasureString(button.Text, button.Font, 200);
button.Width = (int)size.Width+20;
button.Height = (int)size.Height+20;
button.Text = someLongTextWithSpaces;
}
但请注意,我在计算的大小上加了 20。它正在工作,但是是否有正确的方法来确定这个额外的大小?也许 2x 填充 + ??????
几个小时后...
我来到这个版本似乎工作得很好。
using (Graphics cg = this.CreateGraphics()) {
var fmt = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
var prop = new Size(tableLayoutPanel1.Width - 20, 0);
var size = TextRenderer.MeasureText(button.Text, button.Font, prop, fmt);
int border = button.Height - button.Font.Height;
button.Width = (int)size.Width + border;
button.Height = (int)size.Height + border;
button.Text = someLongTextWithSpaces;
}
按钮的初始高度似乎是 borders + 字体的高度。所以我计算了边框减去button.Height-button.font.Height。
根据 Hans 的说法,我现在使用 TextRenderer.MeasureText。我在未启用 VisualStyles 的情况下对其进行了测试,并且运行良好。对此有何评论?
最佳答案
有一个正确的方法,但它并不是非常微妙。从ButtonRenderer类源码逆向出来,绘制按钮文字的Winforms类,必须使用TextRenderer类来测量文字。并且您必须使用 VisualStyleRenderer.GetBackgroundContentRectangle() 方法来获取有效绘图边界。请注意,由于边框和边距取决于所选视觉样式,它小于按钮的尺寸。
重要的问题是将计算出的内容矩形映射回外部按钮大小,以及处理未启用视觉样式的旧机器。看起来大小正确的示例代码:
private static void SetButtonSize(Graphics gr, Button button) {
VisualStyleElement ButtonElement = VisualStyleElement.Button.PushButton.Normal;
var visualStyleRenderer = new VisualStyleRenderer(ButtonElement.ClassName, ButtonElement.Part, 0);
var bounds = visualStyleRenderer.GetBackgroundContentRectangle(gr, button.Bounds);
var margin = button.Height - bounds.Height;
var fmt = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
var prop = new Size(bounds.Width, 0);
var size = TextRenderer.MeasureText(button.Text, button.Font, prop, fmt);
button.ClientSize = new Size(button.ClientSize.Width, size.Height - margin);
}
protected override void OnLoad(EventArgs e) {
using (var gr = this.CreateGraphics()) {
SetButtonSize(gr, this.button1);
}
base.OnLoad(e);
}
没有针对极端情况进行广泛测试,不能说我推荐这个。
关于c# - WinForms 按钮 : Autosize Maximumsize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634866/
我正在使用 Autosize自动调整文本框的大小(高度)。该插件是 JS,但可以用作 jQuery 插件(在网站中有说明)。作者解释了如何在使用 JavaScript 更改文本时触发手动“更新”事件
创建ScatterChart时,我的左侧和底部总是有这个多余的空白,我想将其删除。 这是我的代码。 xAxis.autosize()和yAxis.autosize()不变。 vbox { ad
我在 Delphi XE2 中编写了代码,运行完美。但在Delphi XE6中它不起作用。我创建了一个 Tform,其属性 AutoSize 为 true。我使用 TPanel 将 alTop 与按钮
我正在研究一个 UserControl,它由一组水平排列在 flowlayoutpanel 中控件顶部的 ComboBoxes 和一个位于 flowlayoutpanel 正下方的 datagridv
我有一个表单,通过将 AutoSize 设置为 true 和 AutoSizeMode 到 GrowAndShrink。 初始化后,我希望窗体取得控制权并关闭 AutoSizing 并使 UserCo
据我了解,遵循 documentation ,HOC 是一个接受一个组件并返回一个新组件的函数。一个例子是来自react-redux的connect函数: connect(mapStateToProp
我使用jQuery Autosize我不知道如何使用回调。这是我的尝试: $('#selector').autosize({callback: function myfunc()}); functio
我在应用程序退出时存储列宽并在启动时恢复它们。除非用户双击标题,否则一切正常。这会导致列宽变为 double.NaN,据我所知这是一个用于自动调整大小的标志。那我就有问题了。 在调查该问题时,我注意到
我想在运行时布局控件(动态创建)。出于这个问题的目的,让我们限制为 Button 控件。我想设置控件的属性(例如 Text),然后确定控件的最小尺寸以使其正常显示;设置 AutoSize = true
在自定义 Windows 窗体控件上实现自动调整大小的最佳做法是什么?例如,自定义 Label 控件继承自 System.Windows.Forms.Label 类。 MSDN 上的 Control.
我正在为 iOS 8+ 编写代码。 我有一个 UICollectionReusableView 用作 UICollectionView 的标题 class UserHeader: UICollecti
我想在自定义控件(不是 用户控件)中实现 AutoSize 属性,其行为方式与在设计中实现 AutoSize(ala CheckBox)的其他标准 .NET WinForms 控件一样模式。 我已经设
我在 Windows.Form 上有一个标签。我将标签上的 AutoSize 属性设置为 True,我注意到当我这样做时,它会用 ~5px 的白色背景填充右侧。我将 Padding 属性设置为 [0,
我正在 FlowLayoutPanel 中动态加载图像。我需要此面板自动调整大小,但只能垂直调整。 这是否可能?如果可能,我该如何实现? 最佳答案 简单,添加一个类型控件的事件已添加: private
我使用 jQuery Autosize 插件: http://www.jacklmoore.com/autosize/ 您可以在此处看到脚本本身: http://www.jacklmoore.com/
我只是误解了 Editor-control 上的名称“AutoSize”-property 还是它根本不起作用? 我在 xaml 中有一个这样的控件: 在高度为 100 时效果很好,但是当我写了几行
如何在设计器模式下防止 Label 控件自动换行,同时它的自动大小设置为 false? 基本上我想在设计器模式下自由移动和调整标签大小,但它的文本应该始终在一行中(如果需要剪切)。 感谢您的帮助! 最
我有一个位于屏幕右下角的 UIImageView,每次用户按下按钮时,它都会向左移动几个点。当图像到达屏幕的另一侧时,我使用 if 语句将其放回右侧。我关闭了自动布局并使用以下选项自动调整大小: 因此
有没有一种方法可以找到使用 AutoSize=true 调整自身大小的表单的实际大小? 例如,我有一个在运行时创建 Controller 的表单,我希望将一些 Controller 放置在相对于表单大
据我所知,如果将 collectionviewlayout 的 estimatedItemSize 设置为非零值,将开始自动调整大小,但它对我不起作用. 您可以获得工作副本here如果你想玩它。 最佳
我是一名优秀的程序员,十分优秀!