自学DevExpress为Form表格换肤

本文是参照网上所找资料自己整理制作出来的项目经验,以方便日后使用之便为本意,也希望可以给需要的人一些帮助或者启发。本人使用的是VS2013+DevExpress15.2设计开发的。下面开始正式操作了。


1.打开VS,点击新建项目,选择“C#”里面的“DevExpress V15.1 Template Gallery”并且命名为MainForm,点击“确定”。如图。这里写图片描述


2.然后选择“Blank Application”,点击“Creat Project”,创建新项目完成。这里写图片描述


3.建好后会有如图的“Form1”窗体,将其“Text”属性改为如“制造执行系统”等的窗体显示名,然后添加“DX.15.1”中的“SimpleButton”控件创建按钮,添加“TextEdit”控件创建输入框,然后将它们的“Text”属性改为对应的显示值,建立好后如图。
(注意:本项目中添加的控件均为图中标识出的“DX.15.1”中的控件,因为他们是继承于DevExpress的控件类型,而不是直接继承于Windows的控件。)这里写图片描述


4.单击“Form1”的空白区域选中窗体,设置“属性”中的“MaximizeBox”为“false”(因为放大界面后显示内容会错位,或者采用其他方式解决之也可),设置“StartPossition”为“CenterScreen”(设置窗体显示默认位置为屏幕居中)。这里写图片描述


5.右键工程项目选择“添加”->“新建项”选择“DevExpress V15.1 Template Gallery”并命名为“DevSkins”,店家“确定”,进入下一界面选择“Ribbon Form”,如图。这里写图片描述


6.添加完成后工程中就会出现一个名为“DevSkins.CS”的”Ribbon Form”窗体,并会自动添加如图中的引用,接下来就开始设计该”Ribbon Form”窗体。这里写图片描述


7.选中”设计”界面的”ribbonPage1”的”Text”为”皮肤”,选中”ribbonPageGroup1”设置”Text”为”皮肤”,”name”改为”ribbonPageGroup_Skin”.然后右键设计界面的”ribbonPageGroup1”选择添加”RibbonGalleryBarItem”,设置其”Text”为”请选择你喜欢的皮肤”name”改为”ribbonGalleryBarItem_Skin”.设计好后的界面如图所示。这里写图片描述


8.选择“DevExpress.cs”项目中的“DevSkins”双击进入添加代码。
添加如下的using:

using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
 using DevExpress.Skins;
 using DevExpress.Utils.Drawing;
using System.Collections;

这里写图片描述


9,这里是RibbonFrmMain的全部代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;

using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using DevExpress.Utils.Drawing;
using System.Collections;

namespace DevSkin
{
    public partial class RibbonFrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public RibbonFrmMain()
        {
            InitializeComponent();
        }

        //



        private void RibbonFrmMain_Load(object sender, EventArgs e)
        {
            //RibbonGalleryBarItem ribbonGalleryBarItem_Skin = new RibbonGalleryBarItem();

            ribbonGalleryBarItem_Skin.Gallery.AllowHoverImages = true;

            ribbonGalleryBarItem_Skin.Gallery.Appearance.ItemCaption.Options.UseFont = true;

            ribbonGalleryBarItem_Skin.Gallery.Appearance.ItemCaption.Options.UseTextOptions = true;

            ribbonGalleryBarItem_Skin.Gallery.Appearance.ItemCaption.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            //设置每一行显示的皮肤图标数目
            ribbonGalleryBarItem_Skin.Gallery.ColumnCount = 9;


            ribbonGalleryBarItem_Skin.Gallery.FixedHoverImageSize = false;


            //为已经添加的第三方自定义皮肤设置窗口显示区域等属性。
            //DevExpress.XtraBars.Ribbon.GalleryItemGroup galleryItemGroup_System = new DevExpress.XtraBars.Ribbon.GalleryItemGroup();

            //DevExpress.XtraBars.Ribbon.GalleryItemGroup galleryItemGroup_Office = new DevExpress.XtraBars.Ribbon.GalleryItemGroup();

            //DevExpress.XtraBars.Ribbon.GalleryItemGroup galleryItemGroup_Bonus = new DevExpress.XtraBars.Ribbon.GalleryItemGroup();



            //galleryItemGroup_System.Caption = "系统皮肤";

            //galleryItemGroup_Office.Caption = "Office皮肤";

            //galleryItemGroup_Bonus.Caption = "附加风格";


            //ribbonGalleryBarItem_Skin.Gallery.Groups.AddRange(new DevExpress.XtraBars.Ribbon.GalleryItemGroup[] { galleryItemGroup_System, galleryItemGroup_Office, galleryItemGroup_Bonus });

            //ribbonGalleryBarItem_Skin.Gallery.ImageSize = new System.Drawing.Size(32, 17);

           // ribbonGalleryBarItem_Skin.Gallery.ItemImageLocation = DevExpress.Utils.Locations.Top;


            //选择皮肤下拉框显示的皮肤图标列数
            ribbonGalleryBarItem_Skin.Gallery.RowCount = 2;

            ribbonGalleryBarItem_Skin.Gallery.InitDropDownGallery += new DevExpress.XtraBars.Ribbon.InplaceGalleryEventHandler(rgbiSkins_Gallery_InitDropDownGallery);

            ribbonGalleryBarItem_Skin.Gallery.ItemClick += new DevExpress.XtraBars.Ribbon.GalleryItemClickEventHandler(rgbiSkins_Gallery_ItemClick);

            ribbonGalleryBarItem_Skin.Name = "ribbonGalleryBarItem_Skin";

            //ribbonPageGroup_Skin.ItemLinks.Add(ribbonGalleryBarItem_Skin);

            InitSkinGallery(ribbonGalleryBarItem_Skin);

        }



        #region 皮肤



        private void rgbiSkins_Gallery_ItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
        {

            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(e.Item.Tag.ToString());

        }

        private void rgbiSkins_Gallery_InitDropDownGallery(object sender, DevExpress.XtraBars.Ribbon.InplaceGalleryEventArgs e)
        {

            e.PopupGallery.CreateFrom((sender as DevExpress.XtraBars.Ribbon.Gallery.InRibbonGallery).OwnerItem.Gallery);

            e.PopupGallery.AllowFilter = false;

            e.PopupGallery.ShowItemText = true;

            e.PopupGallery.ShowGroupCaption = true;

            e.PopupGallery.AllowHoverImages = false;

            foreach (GalleryItemGroup galleryGroup in e.PopupGallery.Groups)

                foreach (GalleryItem item in galleryGroup.Items)

                    item.Image = item.HoverImage;

            e.PopupGallery.ColumnCount = 3;

            e.PopupGallery.ImageSize = new Size(70, 36);

        }

        private void InitSkinGallery(RibbonGalleryBarItem rgbiSkins)
        {

            Hashtable skinName = new Hashtable();

            //系统风格 系统默认

            skinName.Add("SystemDev Caramel", "焦糖");

            skinName.Add("SystemDev Money Twins", "潮汐");

            skinName.Add("SystemDev Lilian", "莉莲");

            skinName.Add("SystemDev The Asphalt World", "柏油");

            skinName.Add("SystemDev iMaginary", "虚幻");

            skinName.Add("SystemDev Black", "夜色");

            skinName.Add("SystemDev Blue", "蓝色");

            //Office风格 添加OfficeSkins这个dll才可用 要先进行注册才能使用

            skinName.Add("Office 2007 Blue", "Office 2007 蓝色");

            skinName.Add("Office 2007 Black", "Office 2007 黑色");

            skinName.Add("Office 2007 Silver", "Office 2007 银色");

            skinName.Add("Office 2007 Green", "Office 2007 绿色");

            skinName.Add("Office 2007 Pink", "Office 2007 粉色");

            //额外风格  添加BonusSkin这个dll才可用 要先进行注册才能使用

            skinName.Add("Bonus Coffee", "咖啡色调");

            skinName.Add("Bonus Liquid Sky", "碧海蓝天");

            skinName.Add("Bonus London Liquid Sky", "伦敦夜空");

            skinName.Add("Bonus Glass Oceans", "玻璃海洋");

            skinName.Add("Bonus Stardust", "星尘");

            skinName.Add("Bonus Xmas 2008 Blue", "蓝色圣诞2008");

            skinName.Add("Bonus Valentine", "粉色情人节");

            skinName.Add("Bonus McSkin", "玻璃黑");

            skinName.Add("Bonus Springtime", "春暖花开");

            skinName.Add("Bonus Seven", "淡蓝色调");

            skinName.Add("Bonus Darkroom", "黑色空间");

            skinName.Add("Bonus Foggy", "朦胧记忆");

            skinName.Add("Bonus Sharp Plus", "深黑色调");

            skinName.Add("Bonus Sharp", "浓色灰黑");

            skinName.Add("Bonus Seven Classic", "淡灰经典");

            skinName.Add("Bonus High Contrast", "纯黑夜色");

            skinName.Add("Bonus Dark Side", "浓黑一方");

            skinName.Add("Bonus Pumpkin", "万圣节南瓜");

            skinName.Add("Bonus Summer 2008", "阳光海滩");



            //自定义皮肤,通过SkinEdit制作

            // skinName.Add("BonusMySkin_Lilian_Test", "MySkin");

            //皮肤与官方实例相比缺少5种,

            //分别为Flat,Office 2003,Style3D,UltraFlat,Window Theme

            SimpleButton imageButton = new SimpleButton();

            foreach (SkinContainer cnt in SkinManager.Default.Skins)
            {

                string nameTemp = null;

                foreach (DictionaryEntry item in skinName)
                {

                    string halfName = string.Empty;

                    if (item.Key.ToString().Contains("SystemDev"))
                    {

                        halfName = item.Key.ToString().Replace("SystemDev", "");

                    }

                    else if (item.Key.ToString().Contains("Bonus"))
                    {

                        halfName = item.Key.ToString().Replace("Bonus", "");

                    }

                    else if (item.Key.ToString().Contains("Office"))
                    {

                        halfName = item.Key.ToString();

                    }

                    else
                    {

                        halfName = null;

                    }

                    if (halfName.Trim() == cnt.SkinName.Trim())
                    {

                        nameTemp = item.Key.ToString();

                        break;

                    }

                }

                if (string.IsNullOrEmpty(nameTemp))
                {

                    continue;

                }

                imageButton.LookAndFeel.SetSkinStyle(cnt.SkinName);

                GalleryItem gItem = new GalleryItem();

                int groupIndex = -1;

                if (nameTemp.IndexOf("SystemDev") > -1)

                    groupIndex = 0;

                if (nameTemp.IndexOf("Office") > -1)

                    groupIndex = 1;

                if (nameTemp.IndexOf("Bonus") > -1)

                    groupIndex = 2;

                rgbiSkins.Gallery.Groups[groupIndex].Items.Add(gItem);

                gItem.Image = GetSkinImage(imageButton, 32, 17, 2);

                gItem.HoverImage = GetSkinImage(imageButton, 70, 36, 5);

                gItem.Tag = cnt.SkinName;

                gItem.Caption = skinName[nameTemp].ToString();

                gItem.Hint = skinName[nameTemp].ToString();

                rgbiSkins.Gallery.Groups[1].Visible = false;

            }

        }

        private Bitmap GetSkinImage(SimpleButton button, int width, int height, int indent)
        {

            Bitmap image = new Bitmap(width, height);

            using (Graphics g = Graphics.FromImage(image))
            {

                StyleObjectInfoArgs info = new StyleObjectInfoArgs(new GraphicsCache(g));

                info.Bounds = new Rectangle(0, 0, width, height);

                button.LookAndFeel.Painter.GroupPanel.DrawObject(info);

                button.LookAndFeel.Painter.Border.DrawObject(info);

                info.Bounds = new Rectangle(indent, indent, width - indent * 2, height - indent * 2);

                button.LookAndFeel.Painter.Button.DrawObject(info);

            }

            return image;

        }


        #endregion






    }
}

文中有不周到之处欢迎大家批评指正,本文后续内容还会陆续写出来。

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试