Advertisement
simos-sigma

CustomColorEditor

Dec 12th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.49 KB | Source Code | 0 0
  1. public class CustomColorEditor : ColorEditor
  2. {
  3.     private static TabPage MyColors_TabPage;
  4.     private static ListBox MyColors_ListBox;
  5.  
  6.     private static object[] MyColor_Values
  7.     {
  8.         get
  9.         {
  10.             if (MyColor_Constants == null)
  11.             {
  12.                 MyColor_Constants = GetConstants(typeof(Color));
  13.             }
  14.  
  15.             return MyColor_Constants;
  16.         }
  17.     }
  18.  
  19.     static CustomColorEditor()
  20.     {
  21.         MyColors_TabPage = new System.Windows.Forms.TabPage();
  22.         MyColors_TabPage.Location = new System.Drawing.Point(4, 22);
  23.         MyColors_TabPage.Name = "MyColors_TabPage";
  24.         MyColors_TabPage.Size = new System.Drawing.Size(192, 174);
  25.         MyColors_TabPage.TabIndex = 3;
  26.         MyColors_TabPage.Text = "My Colors";
  27.         MyColors_TabPage.UseVisualStyleBackColor = true;
  28.  
  29.         MyColors_ListBox = new System.Windows.Forms.ColorEditorListBox();
  30.         MyColors_ListBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  31.         // MyColors_ListBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
  32.         MyColors_ListBox.Dock = System.Windows.Forms.DockStyle.Fill;
  33.         MyColors_ListBox.FormattingEnabled = true;
  34.         MyColors_ListBox.IntegralHeight = false;
  35.         MyColors_ListBox.Location = new System.Drawing.Point(0, 0);
  36.         MyColors_ListBox.Name = "MyColors_ListBox";
  37.         MyColors_ListBox.Size = new System.Drawing.Size(192, 248);
  38.         MyColors_ListBox.Sorted = false;
  39.         MyColors_ListBox.TabIndex = 0;
  40.         Array.Sort(MyColor_Values, new CustomColorEditor.StandardColorComparer());
  41.         MyColors_ListBox.Items.Clear();
  42.         MyColors_ListBox.Click += new System.EventHandler(OnMyColorsClick);
  43.         MyColors_ListBox.KeyDown += new System.Windows.Forms.KeyEventHandler(OnMyColorsKeyDown);
  44.  
  45.         foreach (object item in MyColor_Values) { MyColors_ListBox.Items.Add(item); }
  46.     }
  47.  
  48.     private static void OnMyColorsClick(object sender, EventArgs e)
  49.     {
  50.         ListBox listBox = (ListBox)sender;
  51.         if (listBox.SelectedItem != null)
  52.         {
  53.             value = (Color)listBox.SelectedItem;
  54.         }
  55.         // edSvc.CloseDropDown();
  56.     }
  57.  
  58.     private static void OnMyColorsKeyDown(object sender, KeyEventArgs ke)
  59.     {
  60.         if (ke.KeyCode == Keys.Return)
  61.         {
  62.             OnMyColorsClick(sender, EventArgs.Empty);
  63.         }
  64.     }
  65.  
  66.     private class StandardColorComparer : IComparer
  67.     {
  68.         public int Compare(object x, object y)
  69.         {
  70.             Color color = (Color)x;
  71.             Color color2 = (Color)y;
  72.             if (color.A < color2.A)
  73.             {
  74.                 return -1;
  75.             }
  76.  
  77.             if (color.A > color2.A)
  78.             {
  79.                 return 1;
  80.             }
  81.  
  82.             if (color.GetHue() < color2.GetHue())
  83.             {
  84.                 return -1;
  85.             }
  86.  
  87.             if (color.GetHue() > color2.GetHue())
  88.             {
  89.                 return 1;
  90.             }
  91.  
  92.             if (color.GetSaturation() < color2.GetSaturation())
  93.             {
  94.                 return -1;
  95.             }
  96.  
  97.             if (color.GetSaturation() > color2.GetSaturation())
  98.             {
  99.                 return 1;
  100.             }
  101.  
  102.             if (color.GetBrightness() < color2.GetBrightness())
  103.             {
  104.                 return -1;
  105.             }
  106.  
  107.             if (color.GetBrightness() > color2.GetBrightness())
  108.             {
  109.                 return 1;
  110.             }
  111.  
  112.             return 0;
  113.         }
  114.     }
  115.  
  116.     private static object[] GetConstants(Type enumType)
  117.     {
  118.         MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.Static;
  119.         PropertyInfo[] properties = enumType.GetProperties();
  120.         ArrayList arrayList = new ArrayList();
  121.         foreach (PropertyInfo propertyInfo in properties)
  122.         {
  123.             if (propertyInfo.PropertyType == typeof(Color))
  124.             {
  125.                 MethodInfo getMethod = propertyInfo.GetGetMethod();
  126.                 if (getMethod != null && (getMethod.Attributes & methodAttributes) == methodAttributes)
  127.                 {
  128.                     object[] index = null;
  129.                     arrayList.Add(propertyInfo.GetValue(null, index));
  130.                 }
  131.             }
  132.         }
  133.  
  134.         return arrayList.ToArray();
  135.     }
  136.  
  137.  
  138.  
  139.     public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
  140.     {
  141.         CustomColorEditor custom_color_editor = this;
  142.  
  143.         Type color_ui_type = typeof(System.Drawing.Design.ColorEditor).GetNestedType("ColorUI", BindingFlags.NonPublic);
  144.         ConstructorInfo color_ui_constructor = color_ui_type.GetConstructors()[0];
  145.         FieldInfo color_ui_field = typeof(System.Drawing.Design.ColorEditor).GetField("colorUI", BindingFlags.Instance | BindingFlags.NonPublic);
  146.         object color_ui_object = color_ui_constructor.Invoke(new[] { custom_color_editor });
  147.         color_ui_field.SetValue(custom_color_editor, color_ui_object);
  148.  
  149.         FieldInfo tabControl_field = color_ui_object.GetType().GetField("tabControl", BindingFlags.Instance | BindingFlags.NonPublic);
  150.         object tabControl_object = tabControl_field.GetValue(color_ui_object);
  151.  
  152.         TabControl tab_control = tabControl_object as TabControl;
  153.         tab_control.Controls.Add(MyColors_TabPage);
  154.         MyColors_TabPage.Controls.Add(MyColors_ListBox);
  155.  
  156.         object selected_value = base.EditValue(context, provider, value);
  157.         return selected_value;
  158.     }
  159. }
Tags: C# Incomplete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement