DotNetBar的使用(七)Office系列风格的切换
之前也实现过界面风格的切换,不过之前的代码实现非常简单也不够标准化,是通过对按钮绑定点击事件实现的。本章采用标准的方法,实现Office风格界面的切换,控件的添加、属性的规定和事情的绑定都采用控制语句实现,这样做的好处是代码移植性好,不必在属性列表中手动设置控件属性和事件。
①首先创建一个Ribbon风格的Windows窗体,向其中添加一个RibbonControl控件
②添加如下代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
namespace _01
{
public partial class Form4 : DevComponents.DotNetBar.RibbonForm
{
private bool m_ColorSelected = false;
private eStyle m_BaseStyle = eStyle.Office2010Silver;
private ButtonItem buttonChangeStyle;
private ButtonItem buttonItem17;
private ButtonItem buttonStyleMetro;
private ButtonItem buttonStyleOffice2010Blue;
private ButtonItem buttonStyleOffice2010Silver;
private ButtonItem buttonStyleOffice2010Black;
private ButtonItem buttonStyleVS2010;
private ButtonItem buttonItem62;
private ButtonItem buttonStyleOffice2007Blue;
private ButtonItem buttonStyleOffice2007Black;
private ButtonItem buttonStyleOffice2007Silver;
private ButtonItem buttonItem60;
private ButtonItem buttonItem16;
private Command AppCommandTheme;
private SuperTooltip superTooltip1;
private ColorPickerDropDown buttonStyleCustom;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
this.buttonChangeStyle = new ButtonItem();
this.buttonItem17 = new ButtonItem();
this.AppCommandTheme = new Command(this.components);
this.buttonStyleMetro = new ButtonItem();
this.buttonStyleOffice2010Blue = new ButtonItem();
this.buttonStyleOffice2010Silver = new ButtonItem();
this.buttonStyleOffice2010Black = new ButtonItem();
this.buttonStyleVS2010 = new ButtonItem();
this.buttonItem62 = new ButtonItem();
this.buttonStyleOffice2007Blue = new ButtonItem();
this.buttonStyleOffice2007Black = new ButtonItem();
this.buttonStyleOffice2007Silver = new ButtonItem();
this.buttonItem60 = new ButtonItem();
this.buttonItem16 = new ButtonItem();
this.superTooltip1 = new SuperTooltip();
this.buttonStyleCustom = new ColorPickerDropDown();
this.ribbonControl1.Items.AddRange(new BaseItem[]
{
this.buttonChangeStyle //为ribbonControl1添加此控件
}
);
// buttonChangeStyle
//
this.buttonChangeStyle.Visible = true; //可见
this.buttonChangeStyle.AutoExpandOnClick = true; //点击按钮任意位置可以展开子项目;若设置为false只能点击展开按钮才展开
this.buttonChangeStyle.ItemAlignment = eItemAlignment.Far; //设置按钮对齐方式
this.buttonChangeStyle.Name = "buttonChangeStyle"; //设置标识名
this.buttonChangeStyle.SubItems.AddRange(new BaseItem[] {
this.buttonItem17,
this.buttonStyleMetro,
this.buttonStyleOffice2010Blue,
this.buttonStyleOffice2010Silver,
this.buttonStyleOffice2010Black,
this.buttonStyleVS2010,
this.buttonItem62,
this.buttonStyleOffice2007Blue,
this.buttonStyleOffice2007Black,
this.buttonStyleOffice2007Silver,
this.buttonItem60,
this.buttonItem16,
this.buttonStyleCustom});
this.superTooltip1.SetSuperTooltip(this.buttonChangeStyle, new SuperTooltipInfo("Change the style", "", "Change the style of all DotNetBar User Interface elements.", null, null, eTooltipColor.Gray));
//为此按钮添加提示文本,当鼠标在此按钮上方时显示功能提示
this.buttonChangeStyle.Text = "Style"; //设置界面显示文本
//
// buttonItem17
//
this.buttonItem17.Checked = true; //默认选择此按钮
this.buttonItem17.Command = this.AppCommandTheme; //添加控制
this.buttonItem17.CommandParameter = "Office2016"; //控制参数设置
this.buttonItem17.Name = "buttonItem17";
this.buttonItem17.OptionGroup = "style"; //设置按钮所属的组,这样可以保证组内的元素处于互斥状态,即一个状态只能有一个按钮被选中
this.buttonItem17.Text = "Office 2016";
//
// AppCommandTheme
//
this.AppCommandTheme.Name = "AppCommandTheme"; //设置控制的标识名
this.AppCommandTheme.Executed += new System.EventHandler(this.AppCommandTheme_Executed);
//
// buttonStyleMetro
//
this.buttonStyleMetro.Command = this.AppCommandTheme;
this.buttonStyleMetro.CommandParameter = "Metro";
this.buttonStyleMetro.Name = "buttonStyleMetro";
this.buttonStyleMetro.OptionGroup = "style";
this.buttonStyleMetro.Text = "Metro/Office 2013";
//
// buttonStyleOffice2010Blue
//
this.buttonStyleOffice2010Blue.Command = this.AppCommandTheme;
this.buttonStyleOffice2010Blue.CommandParameter = "Office2010Blue";
this.buttonStyleOffice2010Blue.Name = "buttonStyleOffice2010Blue";
this.buttonStyleOffice2010Blue.OptionGroup = "style";
this.buttonStyleOffice2010Blue.Text = "Office 2010 Blue";
//
// buttonStyleOffice2010Silver
//
this.buttonStyleOffice2010Silver.Command = this.AppCommandTheme;
this.buttonStyleOffice2010Silver.CommandParameter = "Office2010Silver";
this.buttonStyleOffice2010Silver.Name = "buttonStyleOffice2010Silver";
this.buttonStyleOffice2010Silver.OptionGroup = "style";
this.buttonStyleOffice2010Silver.Text = "Office 2010 <font color=\"Silver\"><b>Silver</b></font>";
//
// buttonStyleOffice2010Black
//
this.buttonStyleOffice2010Black.Command = this.AppCommandTheme;
this.buttonStyleOffice2010Black.CommandParameter = "Office2010Black";
this.buttonStyleOffice2010Black.Name = "buttonStyleOffice2010Black";
this.buttonStyleOffice2010Black.OptionGroup = "style";
this.buttonStyleOffice2010Black.Text = "Office 2010 Black";
//
// buttonStyleVS2010
//
this.buttonStyleVS2010.Command = this.AppCommandTheme;
this.buttonStyleVS2010.CommandParameter = "VisualStudio2010Blue";
this.buttonStyleVS2010.Name = "buttonStyleVS2010";
this.buttonStyleVS2010.OptionGroup = "style";
this.buttonStyleVS2010.Text = "Visual Studio 2010";
//
// buttonItem62
//
this.buttonItem62.Command = this.AppCommandTheme;
this.buttonItem62.CommandParameter = "Windows7Blue";
this.buttonItem62.Name = "buttonItem62";
this.buttonItem62.OptionGroup = "style";
this.buttonItem62.Text = "Windows 7";
//
// buttonStyleOffice2007Blue
//
this.buttonStyleOffice2007Blue.Command = this.AppCommandTheme;
this.buttonStyleOffice2007Blue.CommandParameter = "Office2007Blue";
this.buttonStyleOffice2007Blue.Name = "buttonStyleOffice2007Blue";
this.buttonStyleOffice2007Blue.OptionGroup = "style";
this.buttonStyleOffice2007Blue.Text = "Office 2007 <font color=\"Blue\"><b>Blue</b></font>";
//
// buttonStyleOffice2007Black
//
this.buttonStyleOffice2007Black.Command = this.AppCommandTheme;
this.buttonStyleOffice2007Black.CommandParameter = "Office2007Black";
this.buttonStyleOffice2007Black.Name = "buttonStyleOffice2007Black";
this.buttonStyleOffice2007Black.OptionGroup = "style";
this.buttonStyleOffice2007Black.Text = "Office 2007 <font color=\"black\"><b>Black</b></font>";
//
// buttonStyleOffice2007Silver
//
this.buttonStyleOffice2007Silver.Command = this.AppCommandTheme;
this.buttonStyleOffice2007Silver.CommandParameter = "Office2007Silver";
this.buttonStyleOffice2007Silver.Name = "buttonStyleOffice2007Silver";
this.buttonStyleOffice2007Silver.OptionGroup = "style";
this.buttonStyleOffice2007Silver.Text = "Office 2007 <font color=\"Silver\"><b>Silver</b></font>";
//
// buttonItem60
//
this.buttonItem60.Command = this.AppCommandTheme;
this.buttonItem60.CommandParameter = "Office2007VistaGlass";
this.buttonItem60.Name = "buttonItem60";
this.buttonItem60.OptionGroup = "style";
this.buttonItem60.Text = "Vista Glass";
//
// buttonItem16
//
this.buttonItem16.Command = this.AppCommandTheme;
this.buttonItem16.CommandParameter = "VisualStudio2012Light";
this.buttonItem16.Name = "buttonItem16";
this.buttonItem16.OptionGroup = "style";
this.buttonItem16.Text = "Visual Studio 2012 Light";
//
// buttonStyleCustom
//
this.buttonStyleCustom.BeginGroup = true; //设置这个选项是否开启一个新组,即在此按钮上方是否有分隔符
this.buttonStyleCustom.Command = this.AppCommandTheme;
this.buttonStyleCustom.Name = "buttonStyleCustom";
this.buttonStyleCustom.Text = "Custom scheme";
this.buttonStyleCustom.Tooltip = "Custom color scheme is created based on currently selected color table. Try selec" +
"ting Silver or Blue color table and then creating custom color scheme.";
this.buttonStyleCustom.SelectedColorChanged += new System.EventHandler(this.buttonStyleCustom_SelectedColorChanged);
this.buttonStyleCustom.ColorPreview += new DevComponents.DotNetBar.ColorPreviewEventHandler(this.buttonStyleCustom_ColorPreview);
this.buttonStyleCustom.ExpandChange += new System.EventHandler(this.buttonStyleCustom_ExpandChange);
////
}
private void AppCommandTheme_Executed(object sender, EventArgs e)
{
ICommandSource source = sender as ICommandSource; //将命令强制转换为ICommandSource
if (source.CommandParameter is string) //若控制参数为字符串
{
eStyle style = (eStyle)Enum.Parse(typeof(eStyle), source.CommandParameter.ToString());
// Using StyleManager change the style and color tinting
if (StyleManager.IsMetro(style))
{
// More customization is needed for Metro
// Capitalize App Button and tab
buttonFile.Text = buttonFile.Text.ToUpper();
foreach (BaseItem item in RibbonControl.Items)
{
// Ribbon Control may contain items other than tabs so that needs to be taken in account
RibbonTabItem tab = item as RibbonTabItem;
if (tab != null)
tab.Text = tab.Text.ToUpper();
}
buttonFile.BackstageTabEnabled = true; // Use Backstage for Metro
ribbonControl1.RibbonStripFont = new System.Drawing.Font("Segoe UI", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
if (style == eStyle.Metro)
StyleManager.MetroColorGeneratorParameters = DevComponents.DotNetBar.Metro.ColorTables.MetroColorGeneratorParameters.DarkBlue;
//// Adjust size of switch button to match Metro styling
//switchButtonItem1.SwitchWidth = 16;
//switchButtonItem1.ButtonWidth = 48;
//switchButtonItem1.ButtonHeight = 19;
//// Adjust tab strip style
tabStrip1.Style = eTabStripStyle.Metro;
StyleManager.Style = style; // BOOM
}
else
{
// If previous style was Metro we need to update other properties as well
if (StyleManager.IsMetro(StyleManager.Style))
{
ribbonControl1.RibbonStripFont = null;
// Fix capitalization App Button and tab
//buttonFile.Text = ToTitleCase(buttonFile.Text);
foreach (BaseItem item in RibbonControl.Items)
{
// Ribbon Control may contain items other than tabs so that needs to be taken in account
RibbonTabItem tab = item as RibbonTabItem;
if (tab != null)
tab.Text = tab.Text.ToUpper();
}
// Adjust size of switch button to match Office styling
//switchButtonItem1.SwitchWidth = 28;
//switchButtonItem1.ButtonWidth = 62;
//switchButtonItem1.ButtonHeight = 20;
}
// Adjust tab strip style
tabStrip1.Style = eTabStripStyle.Office2007Document;
StyleManager.ChangeStyle(style, Color.Empty);
if (style == eStyle.Office2007Black || style == eStyle.Office2007Blue || style == eStyle.Office2007Silver || style == eStyle.Office2007VistaGlass)
buttonFile.BackstageTabEnabled = false;
else
buttonFile.BackstageTabEnabled = true;
}
}
else if (source.CommandParameter is Color)
{
if (StyleManager.IsMetro(StyleManager.Style))
StyleManager.MetroColorGeneratorParameters = new DevComponents.DotNetBar.Metro.ColorTables.MetroColorGeneratorParameters(Color.White, (Color)source.CommandParameter);
else
StyleManager.ColorTint = (Color)source.CommandParameter;
}
}
private void buttonStyleCustom_SelectedColorChanged(object sender, System.EventArgs e)
{
m_ColorSelected = true; // Indicate that color was selected for buttonStyleCustom_ExpandChange method
buttonStyleCustom.CommandParameter = buttonStyleCustom.SelectedColor;
}
private void buttonStyleCustom_ColorPreview(object sender, DevComponents.DotNetBar.ColorPreviewEventArgs e)
{
if (StyleManager.IsMetro(StyleManager.Style))
{
Color baseColor = e.Color;
StyleManager.MetroColorGeneratorParameters = new DevComponents.DotNetBar.Metro.ColorTables.MetroColorGeneratorParameters(Color.White, baseColor);
}
else
StyleManager.ColorTint = e.Color;
}
private void buttonStyleCustom_ExpandChange(object sender, System.EventArgs e)
{
if (buttonStyleCustom.Expanded)
{
// Remember the starting color scheme to apply if no color is selected during live-preview
m_ColorSelected = false;
m_BaseStyle = StyleManager.Style;
}
else
{
if (!m_ColorSelected)
{
if (StyleManager.IsMetro(StyleManager.Style))
StyleManager.MetroColorGeneratorParameters = DevComponents.DotNetBar.Metro.ColorTables.MetroColorGeneratorParameters.Default;
else
StyleManager.ChangeStyle(m_BaseStyle, Color.Empty);
}
}
}
}
}
③运行显示
参考资料:DOTNETBAR在线帮助文档
地理信息科学
Writed By NX
QQ:1051926720