工具栏控件绑定下拉式颜色选择框、文本框及响应事件

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraTab;
using DevExpress.XtraBars;
using DevExpress.LookAndFeel;

namespace DevExpress.XtraBars.Demos.SimplePad {
 public class ColorPopup {

             //页面容器
  XtraTabControl tabControl;

              //选中的颜色
  Color fResultColor;

              //点击的工具栏控件
  BarItem itemFontColor;

                //dev容器
  PopupControlContainer container;

               //要改变文字颜色的文本框
  RichTextBox rtPad;

               //container,item,rtPad三控件绑定
  public ColorPopup(PopupControlContainer container, BarItem item, RichTextBox rtPad) {
   this.rtPad = rtPad;
   this.container = container;
   this.itemFontColor = item; 
   this.fResultColor = Color.Empty;
                       
                        //创建页面容器
   this.tabControl = CreateTabControl();

                       //设置此控件不能用tab获取焦点
   this.tabControl.TabStop = false;

                       //在页面容器中添加三个页面
   this.tabControl.TabPages.AddRange(new XtraTabPage[] { new  XtraTabPage(), new XtraTabPage(), new                         XtraTabPage()});

                         //对三个页面分别进行初始化
   for(int i = 0; i < tabControl.TabPages.Count; i++)                            
                             SetTabPageProperties(i);

   tabControl.Dock = DockStyle.Fill;
                        
                         //将页面容器添加到container容器中
   this.container.Controls.AddRange(new System.Windows.Forms.Control[]{tabControl});
                       
                        //页面主题变化事件
                        UserLookAndFeel.Default.StyleChanged += new EventHandler(delegate { SetContainerSize(); });
                        SetContainerSize();
  }

                //设置容器大小
                void SetContainerSize() {
                     this.container.Size = CalcFormSize();
                }
             
               //获取新页面
  private XtraTabControl CreateTabControl() { return new XtraTabControl(); }

              
  private ColorListBox CreateColorListBox() { return new ColorListBox(); }

              //页面初始化
  private void SetTabPageProperties(int pageIndex) {
   XtraTabPage tabPage = this.tabControl.TabPages[pageIndex];
                     
                        //
   ColorListBox colorBox = null;

                      //
   BaseControl control = null;
   switch(pageIndex) {
    case 0:
     tabPage.Text = Localizer.Active.GetLocalizedString (StringId.ColorTabCustom);
     control = new ColorCellsControl(null);
     DevExpress.XtraEditors.Repository.RepositoryItemColorEdit rItem = new                                                         DevExpress.XtraEditors.Repository.RepositoryItemColorEdit();
     rItem.ShowColorDialog = false;
     (control as ColorCellsControl).Properties = rItem;
     (control as ColorCellsControl).EnterColor += new EnterColorEventHandle(OnEnterColor);
     control.Size = ColorCellsControlViewInfo.BestSize;
     break;
    case 1:
     tabPage.Text = Localizer.Active.GetLocalizedString(StringId.ColorTabWeb);
     colorBox = CreateColorListBox();
     colorBox.Items.AddRange(ColorListBoxViewInfo.WebColors);
     colorBox.EnterColor += new EnterColorEventHandler(OnEnterColor);
     control = colorBox;
     break;
    case 2:
     tabPage.Text = Localizer.Active.GetLocalizedString(StringId.ColorTabSystem);
     colorBox = CreateColorListBox();
     colorBox.Items.AddRange(ColorListBoxViewInfo.SystemColors);
     colorBox.EnterColor += new EnterColorEventHandler(OnEnterColor);
     control = colorBox;
     break;
   }
   control.Dock = DockStyle.Fill;
   control.BorderStyle = BorderStyles.NoBorder;
   control.LookAndFeel.ParentLookAndFeel = itemFontColor.Manager.GetController().LookAndFeel;
   tabPage.Controls.Add(control);
  }
  private void OnEnterColor(object sender, EnterColorEventArgs e) {
   fResultColor = e.Color;
   OnEnterColor(e.Color);
  }
  private void OnEnterColor(Color clr) {
   container.HidePopup();
   rtPad.SelectionColor = clr;

                    //控件rtpad上可变化颜色条
   int imIndex = itemFontColor.ImageIndex;
   ImageList iml = itemFontColor.Images as ImageList;
   Bitmap im = (Bitmap)iml.Images[imIndex];
   Graphics g = Graphics.FromImage(im);
   Rectangle r = new Rectangle(7, 7, 8, 8);
   g.FillRectangle(new SolidBrush(clr), r);
   if(imIndex == iml.Images.Count - 1) {
    iml.Images.RemoveAt(imIndex);
   }
                        g.Dispose();
   iml.Images.Add(im);
   itemFontColor.ImageIndex = iml.Images.Count - 1;
   
  }

  private ColorCellsControl CellsControl { get { return ((ColorCellsControl)tabControl.TabPages[0].Controls                                                              [0]); } }
  private string CustomColorsName { get { return "CustomColors"; } }
  public Color ResultColor { get { return fResultColor; } }
       
  public Size CalcFormSize() {
   Size size = ColorCellsControlViewInfo.BestSize;
                        size.Height = 6 * ColorCellsControlViewInfo.CellSize.Height + 7 *                                                               ColorCellsControlViewInfo.CellsInternval;
   return tabControl.CalcSizeByPageClient(size);
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值