Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomColorEditor : ColorEditor
- {
- private static TabPage MyColors_TabPage;
- private static ListBox MyColors_ListBox;
- private static object[] MyColor_Values
- {
- get
- {
- if (MyColor_Constants == null)
- {
- MyColor_Constants = GetConstants(typeof(Color));
- }
- return MyColor_Constants;
- }
- }
- static CustomColorEditor()
- {
- MyColors_TabPage = new System.Windows.Forms.TabPage();
- MyColors_TabPage.Location = new System.Drawing.Point(4, 22);
- MyColors_TabPage.Name = "MyColors_TabPage";
- MyColors_TabPage.Size = new System.Drawing.Size(192, 174);
- MyColors_TabPage.TabIndex = 3;
- MyColors_TabPage.Text = "My Colors";
- MyColors_TabPage.UseVisualStyleBackColor = true;
- MyColors_ListBox = new System.Windows.Forms.ColorEditorListBox();
- MyColors_ListBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- // MyColors_ListBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
- MyColors_ListBox.Dock = System.Windows.Forms.DockStyle.Fill;
- MyColors_ListBox.FormattingEnabled = true;
- MyColors_ListBox.IntegralHeight = false;
- MyColors_ListBox.Location = new System.Drawing.Point(0, 0);
- MyColors_ListBox.Name = "MyColors_ListBox";
- MyColors_ListBox.Size = new System.Drawing.Size(192, 248);
- MyColors_ListBox.Sorted = false;
- MyColors_ListBox.TabIndex = 0;
- Array.Sort(MyColor_Values, new CustomColorEditor.StandardColorComparer());
- MyColors_ListBox.Items.Clear();
- MyColors_ListBox.Click += new System.EventHandler(OnMyColorsClick);
- MyColors_ListBox.KeyDown += new System.Windows.Forms.KeyEventHandler(OnMyColorsKeyDown);
- foreach (object item in MyColor_Values) { MyColors_ListBox.Items.Add(item); }
- }
- private static void OnMyColorsClick(object sender, EventArgs e)
- {
- ListBox listBox = (ListBox)sender;
- if (listBox.SelectedItem != null)
- {
- value = (Color)listBox.SelectedItem;
- }
- // edSvc.CloseDropDown();
- }
- private static void OnMyColorsKeyDown(object sender, KeyEventArgs ke)
- {
- if (ke.KeyCode == Keys.Return)
- {
- OnMyColorsClick(sender, EventArgs.Empty);
- }
- }
- private class StandardColorComparer : IComparer
- {
- public int Compare(object x, object y)
- {
- Color color = (Color)x;
- Color color2 = (Color)y;
- if (color.A < color2.A)
- {
- return -1;
- }
- if (color.A > color2.A)
- {
- return 1;
- }
- if (color.GetHue() < color2.GetHue())
- {
- return -1;
- }
- if (color.GetHue() > color2.GetHue())
- {
- return 1;
- }
- if (color.GetSaturation() < color2.GetSaturation())
- {
- return -1;
- }
- if (color.GetSaturation() > color2.GetSaturation())
- {
- return 1;
- }
- if (color.GetBrightness() < color2.GetBrightness())
- {
- return -1;
- }
- if (color.GetBrightness() > color2.GetBrightness())
- {
- return 1;
- }
- return 0;
- }
- }
- private static object[] GetConstants(Type enumType)
- {
- MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.Static;
- PropertyInfo[] properties = enumType.GetProperties();
- ArrayList arrayList = new ArrayList();
- foreach (PropertyInfo propertyInfo in properties)
- {
- if (propertyInfo.PropertyType == typeof(Color))
- {
- MethodInfo getMethod = propertyInfo.GetGetMethod();
- if (getMethod != null && (getMethod.Attributes & methodAttributes) == methodAttributes)
- {
- object[] index = null;
- arrayList.Add(propertyInfo.GetValue(null, index));
- }
- }
- }
- return arrayList.ToArray();
- }
- public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
- {
- CustomColorEditor custom_color_editor = this;
- Type color_ui_type = typeof(System.Drawing.Design.ColorEditor).GetNestedType("ColorUI", BindingFlags.NonPublic);
- ConstructorInfo color_ui_constructor = color_ui_type.GetConstructors()[0];
- FieldInfo color_ui_field = typeof(System.Drawing.Design.ColorEditor).GetField("colorUI", BindingFlags.Instance | BindingFlags.NonPublic);
- object color_ui_object = color_ui_constructor.Invoke(new[] { custom_color_editor });
- color_ui_field.SetValue(custom_color_editor, color_ui_object);
- FieldInfo tabControl_field = color_ui_object.GetType().GetField("tabControl", BindingFlags.Instance | BindingFlags.NonPublic);
- object tabControl_object = tabControl_field.GetValue(color_ui_object);
- TabControl tab_control = tabControl_object as TabControl;
- tab_control.Controls.Add(MyColors_TabPage);
- MyColors_TabPage.Controls.Add(MyColors_ListBox);
- object selected_value = base.EditValue(context, provider, value);
- return selected_value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement