在ColorDialog中设置CustomColors

Afs*_*hin 13 c# colordialog

颜色对话框中的自定义颜色集应使用以下代码设置为{Blue,Blue}:

colorDialog1.CustomColors = new int[] { System.Drawing.Color.Blue.ToArgb(), 0xFF0000 };
colorDialog1.ShowDialog();
Run Code Online (Sandbox Code Playgroud)

但是,我得到了一个不同的集合{Black,Blue}:

在此输入图像描述

不知道我在这里做错了什么?谢谢.

Geo*_*ton 22

您需要使用OLE颜色.实现此目的的简单方法是使用内置ColorTranslator对象,例如

colorDialog1.CustomColors = new int[] { 
                                        ColorTranslator.ToOle(Color.Blue), 
                                        ColorTranslator.ToOle(Color.Red)
                                      };
colorDialog1.ShowDialog(); 
Run Code Online (Sandbox Code Playgroud)

如果您需要从HTML颜色转换,您也可以使用该ColorTranslator.FromHtml方法,例如

colorDialog1.CustomColors = new int[]
                                {
                                    ColorTranslator.ToOle(Color.Blue), 
                                    ColorTranslator.ToOle(ColorTranslator.FromHtml("#FF0000"))
                                };
Run Code Online (Sandbox Code Playgroud)