添加C1Themes到WinForms应用程序

发布时间:2014/05/29 00:05 发布者:Alice

返回博客中心

本文介绍如何添加C1Themes到已经存在的WinForms应用程序。C1Themes内置超过25种专业设计的主题,也可以自定义主题,用于应用程序。本文主要介绍了给C1FlexGrid和C1TrueDBGrid设置主题,根据下述步骤,也可以给ComponentOne Studio for WinForms的C1Report,C1Ribbon以及其他控件,添加C1Themes。

1 添加C1ThemeController

首先添加需要更改主题的控件到应用程序中,在本文Demo中,先从工具箱拖拽C1FlexGrid和C1TrueDBGrid到窗体,并且对两个控件进行数据绑定。C1FlexGridDataSource绑定Employees数据表,C1TrueDBGrid绑定EmployeesOrders,数据绑定代码如下:

this.c1TrueDBGrid1.DataSource = this.employeesOrdersBindingSource;
this.c1FlexGrid1.DataSource = this.employeesBindingSource;

 

然后从工具箱中拖拽C1ThemeController到窗体,会弹出如下编辑框:

C1Theme-ThemeController

 

可以在给C1FlexGrid、C1TrueDBGrid何窗体设置不同的主题,默认为None。

2 将所有Themes添加到ComboBox

添加标准的ComboBox到窗体,然后在FormLoad的时候,将C1ThemController所有可用的主题都加入到ComboBox的下拉菜单中,方便运行时选择,并设置初始化主题,代码如下:

// populate combobox with all available themes
            this.comboBox1.Items.AddRange(C1.Win.C1Themes.C1ThemeController.GetThemes());
            // set initial theme
            comboBox1.SelectedIndex = 0;

 

3 设置主题

设置ComboBox的SelectedIndexChanged事件,当打开下拉框选择相应的主题时,调用该事件改变窗体上所有控件的主题,具体代码如下:

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Set theme on the theme controller:
            this.c1ThemeController1.Theme = this.comboBox1.SelectedItem.ToString();
            // ...and apply it to all themable controls (this will override any control-specific theme settings):
            Action setTheme = null;
            setTheme = (c) =>
            {
                if (C1.Win.C1Themes.C1ThemeController.IsObjectThemeable(c))
                    this.c1ThemeController1.SetTheme(c, this.c1ThemeController1.Theme);
                foreach (Control cc in c.Controls)
                    setTheme(cc);
            };
            setTheme(this);
        }
    }

 

根据代码,首先将ComboBox下拉菜单中选择的Theme设置给C1ThemeController,然后给所有的窗体上的控件使用该Theme。运行起来的效果如下图所示,可以在ComboBox的下拉菜单里选择不同的Theme设置给窗体上的C1FlexGrid和C1TrueDBGrid。

C1Themepng

 

本文Demo的源代码如下:

 

C1Theme1.7z (2.83 mb)


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网