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);
}
}
}