可自定义的拖放式 ToolStrip






4.90/5 (7投票s)
.NET ToolStrip 允许拖放自定义

介绍
你是否想在你的 .NET 项目中添加一个类似 Firefox 中的可拖放自定义的 toolStrip?
背景
我编写这个控件是基于 .NET 的 toolStrip
,并考虑了以下目的/约束:
- 代码必须易于使用,并且易于集成到已经实现了
ToolStrip
的现有项目中。 - 代码应该尽可能轻量级,并且不依赖于任何第三方库。
- 它应该具有灵活性,允许不同的“自定义对话框”和持久化用户设置的方法。
- 如果在进一步的项目开发中添加或删除项,则不应有维护开销。
Using the Code
新的 ToolStrip
将 ToolStripCustom
控件添加到你的窗体中,除了额外的属性外,使用方式与 .NET ToolStrip
类似。
现有的 ToolStrip
小心。 如果你尚未执行此操作,请备份你的项目/解决方案。
- 检查你是否有一个有效的程序集,“
ToolStripCustom
”控件应该在你的工具箱中可见。 - 打开包含工具栏的窗体的设计器文件,找到你的现有
ToolStrip
的定义,并将类型更改为ToolStripCustom
。 - 在创建
ToolStrip
的地方,将构造函数从ToolStrip
更改为ToolStripCustom
。 - 如果修改了
ImageScalingSize
属性,请将属性更改为SmallImageScalingSize
。
自定义 ToolStrip
调用 ToolStripCustom
的 customize
方法来显示自定义对话框,允许用户将工具拖放到 toolstrip
和对话框之间,以及重新排序 toolstrip
中的工具。
private void btnCustomize_Click(object sender, EventArgs e)
{
this.toolStripCustom.Customize();
}
将用户设置保存在注册表中
该控件公开了两个方法,用于将用户的设置保存在注册表中,并检索它们以修改工具栏。
'LoadUserLayoutFromRegistry
' 从注册表中检索用户的设置,并修改 toolstrip
上项目的位置和可见性,以及 toolstrip
的样式(如果给定的键存在)。 它接受 2 个 string
作为参数,用于命名注册表中的应用程序和字段键。
'SaveUserLayoutToRegistry
' 将 toolstrip
的当前布局和样式保存在注册表中,保存在之前调用 'LoadUserLayoutFromRegistry
' 设置的键名下。
private void Demo_Load(object sender, EventArgs e)
{
this.toolStripCustom.LoadUserLayoutFromRegistry("DemoApp", "ToolStripSettings");
}
private void Demo_FormClosed(object sender, FormClosedEventArgs e)
{
this.toolStripCustom.SaveUserLayoutToRegistry();
}
序列化用户设置
存储用户设置的 'toolstripdata
' 类作为 toolstripcustom
的属性公开,并且是可序列化的,这允许用户设置存储在注册表之外。 下面的示例使用独立存储将用户设置存储为二进制数据。
public class UserSettings
{
public ToolStripData MainToolStripUserSettings;
public void LoadUserSettings()
{
IsolatedStorageFileStream fs = new IsolatedStorageFileStream
("Solar01.cfg", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
if (fs.Length > 0)
{
try
{
MainToolStripUserSettings = (ToolStripData)bf.Deserialize(fs);
//deserialize other user preferences
}
catch(Exception e)
{
//serialization exception caught
}
}
else
{
MainToolStripUserSettings = new ToolStripData();
}
fs.Close();
}
public void SaveUserSettings()
{
IsolatedStorageFileStream fs = new IsolatedStorageFileStream
("Solar01.cfg", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
if ( MainToolStripUserSettings != null )
{
try
{
bf.Serialize(fs, MainToolStripUserSettings);
//serialize other user preferences
}
catch(Exception e)
{
//serialization exception caught
}
}
fs.Close();
}
private void MDIParent_Load(object sender, EventArgs e)
{
this.mainToolStrip.UserData = _userSettings.MainToolStripUserSettings;
}
分隔符和可用工具
如果 toolstrip
中有任何分隔符,则分隔符将出现在自定义对话框中的可用工具列表中。 因此,如果你不想默认情况下在 toolstrip
中显示分隔符,但希望分隔符出现在自定义对话框中,则将分隔符添加到 toolstrip
并将其 Visible
属性设置为 false
。
同样,任何其他 visible
属性设置为 false
的工具都将作为自定义对话框中的可用工具出现,但不会出现在默认工具栏上。
图像大小和 LargeImageList
toolstrip
支持两种不同的图像图标大小,并允许用户选择大图标或小图标。
'LargeIcons
' 属性决定 toolstrip
项目是显示大图像还是小图像。 小图像来自每个 toolstrip
项目的图像属性,大图像来自 toolStripCustom
的 'LargeImageList
' 属性。 如果没有找到项目的任何大图像,并且该项目的 'ImageScalingProperty
' 设置为 'SizeToFit
',则项目的图像将拉伸到 'LargeImageSize
'。
图标由 http://pixel-mixer.com 提供。
历史
- 2011 年 12 月 5 日:首次发布