如何:拉伸 ToolStripTextBox 以填充 ToolStrip 的其余宽度(Windows 窗体)
2012-07-16 18:51
459 查看
http://msdn.microsoft.com/zh-cn/library/ms404304.aspx
using System;
using System.Drawing;
using System.Windows.Forms;
publicclass ToolStripSpringTextBox : ToolStripTextBox
{
publicoverride Size GetPreferredSize(Size constrainingSize)
{
// Use the default size if the text box is on the overflow menu
// or is on a vertical ToolStrip.
if (IsOnOverflow || Owner.Orientation == Orientation.Vertical)
{
return DefaultSize;
}
// Declare a variable to store the total available width as
// it is calculated, starting with the display width of the
// owning ToolStrip.
Int32 width = Owner.DisplayRectangle.Width;
// Subtract the width of the overflow button if it is displayed.
if (Owner.OverflowButton.Visible)
{
width = width - Owner.OverflowButton.Width -
Owner.OverflowButton.Margin.Horizontal;
}
// Declare a variable to maintain a count of ToolStripSpringTextBox
// items currently displayed in the owning ToolStrip.
Int32 springBoxCount = 0;
foreach (ToolStripItem item in Owner.Items)
{
// Ignore items on the overflow menu.
if (item.IsOnOverflow) continue;
if (item is ToolStripSpringTextBox)
{
// For ToolStripSpringTextBox items, increment the count and
// subtract the margin width from the total available width.
springBoxCount++;
width -= item.Margin.Horizontal;
}
else
{
// For all other items, subtract the full width from the total
// available width.
width = width - item.Width - item.Margin.Horizontal;
}
}
// If there are multiple ToolStripSpringTextBox items in the owning
// ToolStrip, divide the total available width between them.
if (springBoxCount > 1) width /= springBoxCount;
// If the available width is less than the default width, use the
// default width, forcing one or more items onto the overflow menu.
if (width < DefaultSize.Width) width = DefaultSize.Width;
// Retrieve the preferred size from the base class, but change the
// width to the calculated width.
Size size = base.GetPreferredSize(constrainingSize);
size.Width = width;
return size;
}
}
publicclass Form1 : Form
{
public Form1()
{
ToolStrip toolStrip1 = new ToolStrip();
toolStrip1.Dock = DockStyle.Top;
toolStrip1.Items.Add(new ToolStripLabel("Address"));
toolStrip1.Items.Add(new ToolStripSpringTextBox());
toolStrip1.Items.Add(new ToolStripButton("Go"));
Controls.Add(toolStrip1);
Text = "ToolStripSpringTextBox demo";
}
}
staticclass Program
{
[STAThread]
staticvoid Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
publicclass ToolStripSpringTextBox : ToolStripTextBox
{
publicoverride Size GetPreferredSize(Size constrainingSize)
{
// Use the default size if the text box is on the overflow menu
// or is on a vertical ToolStrip.
if (IsOnOverflow || Owner.Orientation == Orientation.Vertical)
{
return DefaultSize;
}
// Declare a variable to store the total available width as
// it is calculated, starting with the display width of the
// owning ToolStrip.
Int32 width = Owner.DisplayRectangle.Width;
// Subtract the width of the overflow button if it is displayed.
if (Owner.OverflowButton.Visible)
{
width = width - Owner.OverflowButton.Width -
Owner.OverflowButton.Margin.Horizontal;
}
// Declare a variable to maintain a count of ToolStripSpringTextBox
// items currently displayed in the owning ToolStrip.
Int32 springBoxCount = 0;
foreach (ToolStripItem item in Owner.Items)
{
// Ignore items on the overflow menu.
if (item.IsOnOverflow) continue;
if (item is ToolStripSpringTextBox)
{
// For ToolStripSpringTextBox items, increment the count and
// subtract the margin width from the total available width.
springBoxCount++;
width -= item.Margin.Horizontal;
}
else
{
// For all other items, subtract the full width from the total
// available width.
width = width - item.Width - item.Margin.Horizontal;
}
}
// If there are multiple ToolStripSpringTextBox items in the owning
// ToolStrip, divide the total available width between them.
if (springBoxCount > 1) width /= springBoxCount;
// If the available width is less than the default width, use the
// default width, forcing one or more items onto the overflow menu.
if (width < DefaultSize.Width) width = DefaultSize.Width;
// Retrieve the preferred size from the base class, but change the
// width to the calculated width.
Size size = base.GetPreferredSize(constrainingSize);
size.Width = width;
return size;
}
}
publicclass Form1 : Form
{
public Form1()
{
ToolStrip toolStrip1 = new ToolStrip();
toolStrip1.Dock = DockStyle.Top;
toolStrip1.Items.Add(new ToolStripLabel("Address"));
toolStrip1.Items.Add(new ToolStripSpringTextBox());
toolStrip1.Items.Add(new ToolStripButton("Go"));
Controls.Add(toolStrip1);
Text = "ToolStripSpringTextBox demo";
}
}
staticclass Program
{
[STAThread]
staticvoid Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
相关文章推荐
- 如何:拉伸 ToolStripTextBox 以填充 ToolStrip 的其余宽度(Windows 窗体)
- c# 如何改变ToolStrip的大小,并向ToolStripButton加载合适大小的Image
- 如何:使用 Windows 窗体 TextBox 控件创建密码文本框 .NET Framework 2.0 其他版本 密码框是一种 Windows 窗体文本框,它在用户键入字符串时显示占位符。 创
- 如何跨线程调用Windows窗体控件
- 如何:对 Windows 窗体控件进行线程安全调用
- Windows 窗体控件中的多线程处理之:如何对 Windows 窗体控件进行线程安全调用
- 如何创建[圆角、边框色彩渐变、边框宽度自定义]窗体
- 如何跨线程调用.NET Windows窗体控件
- MSDN-如何向 Windows 窗体组件附加智能标记
- C#窗体-个人简历生成(自己设计并编写一个 Windows 应用程序,要求用到TextBox、GroupBox、RadioButton )
- 如何把Windows 窗体 DataGridView 的某一列的数据显示为“*”。
- MDI窗体的工具栏合并(ToolStrip Merging)
- MDI窗体的工具栏合并(ToolStrip Merging)的功能实现
- MDI窗体的工具栏合并(ToolStrip Merging)的功能实现
- 如何使用C#Windows窗体完成文件浏览功能:
- c#中如何跨线程调用windows窗体控件?
- 如何:从命令行创建 Windows 窗体应用程序
- 习题9:设计一个Windows应用程序,窗体上有一个TextBox控件、一个Button控件。
- 如何:为 Windows 窗体 DataGridView 控件中的新行指定默认值
- C++中如何用cout实现输出的填充,宽度,对齐及其精度控制