如何在C#中以编程方式在颜色对话框中添加自定义颜色 [英] How to add custom colors in color dialog programatically in C#

查看:70
本文介绍了如何在C#中以编程方式在颜色对话框中添加自定义颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下代码设置颜色对话框的自定义颜色



 MyDialog.CustomColors =  new   int  [] { 6916092  15195440  16107657  1836924 
3758726 12566463 7526079 7405793 6945974 241502 2296476 5130294
3102017 7324121 14993507 11730944 ,};





但是我想在打开彩色对话框时添加一个自定义颜色。



如果我打开另一种颜色,那么只需要在第二个插槽中添加一个新的自定义coor



如何执行此操作。

解决方案



首先,您的示例来自MSDN文档,没有归属(包括int []声明中不必要的尾随逗号)。复制代码示例时,您必须始终提供归属(例如来自MSDN ColorDialog.CustomColors属性)。这样我知道你知道什么。





请注意ColorDialog中自定义颜色插槽的数量限制为16.请注意,颜色从左到右填充,顶行先填充。





将AllowFullOpen属性设置为false。这样可以控制应用程序的自定义颜色槽。现在,每次用户选择颜色时,您都可以将其添加到您保存在应用程序中的数组中并提供给ColorDialog。





我们都熟悉Color结构。但是在将Color转换为Int32时,您必须进行调整。颜色结构红色,绿色和蓝色组件以ARGB的顺序存储在相邻的字节中,其中A是alpha值(透明度)。但是正如MSDN文档中所指出的那样。



用户可以创建他们的自己的一套自定义颜色。这些颜色是

包含在Int32中,由BGR(蓝色,绿色,红色)
创建颜色所需的
值组成。



因此,提供给ColorDialog的颜色的构造与Color结构的构造不同。





以下代码执行我认为您想要的内容。它需要一个名为Form1的表单,其中包含一个名为button1的按钮。

 使用系统; 
使用 System.Drawing;
使用 System.Windows.Forms;

命名空间 CustomColorDialog
{

// ************************************* **************类Form1

public 部分 Form1:表单
{

const int MAXIMUM_CUSTOM_COLORS = 16 ;

int [] custom_colors = new int [MAXIMUM_CUSTOM_COLORS];
int custom_colors_count = 0 ;

// **************** ************************************* Form1

public Form1()
{

InitializeComponent();

for int i = 0 ;(i < custom_colors.Length); i ++)
{
custom_colors [i] =
(Color.White.ToArgb()& 0x00FFFFFF);
}

button1.Click + = new EventHandler(button1_Click);
}

// *********** ********************************** button1_Click

void button1_Click( object sender,EventArgs e)
{
Color color = Color.Empty;
ColorDialog color_dialog = new ColorDialog();

color_dialog.AllowFullOpen = false ;
color_dialog.CustomColors = custom_colors;
if (color_dialog.ShowDialog()== DialogResult.OK)
{
color = color_dialog.Color;
if (custom_colors_count > = custom_colors.Length)
{
custom_colors_count = 0 ;
}
button1.BackColor = color;
int color_value =(color.B<< 16 )+
(color.G<< 8 )+
color.R;
custom_colors [custom_colors_count ++] = color_value;
}
}

} // class Form1

} // 命名空间CustomColorDialog


I can set the custom colors for color dialog by the code below

MyDialog.CustomColors = new int[]{6916092, 15195440, 16107657, 1836924,
   3758726, 12566463, 7526079, 7405793, 6945974, 241502, 2296476, 5130294,
   3102017, 7324121, 14993507, 11730944,};



But I want to add one custom color one when color dialogbox is opened.

And if I open Another color then only a new custom coor should be added to the second slot

How to do this.

解决方案


First, your example is taken from MSDN documentation without attribution (including the unnecessary trailing comma in the int [ ] declaration). You MUST always provide attribution (like "from MSDN ColorDialog.CustomColors Property") when you copy code examples. That way I know what you know.



Note that the number of custom color slots in the ColorDialog is limited to 16. Note too that the colors are filled from left to right, top row first.



Set the AllowFullOpen property to false. This keeps control of the custom color slots to your application. Now, each time that a user chooses a color, you can add it to the array that you keep in your application and supply to the ColorDialog.



We are all familiar with the Color structure. BUT when converting a Color to an Int32 you must make an adjustment. The Color structure red, green, and blue components are stored in adjacent bytes in the order ARGB, where A is the alpha value (transparency). But as pointed
out in MSDN documentation


Users can create their own set of custom colors. These colors are
contained in an Int32 composed of the BGR (blue, green, red)
values necessary to create the color.


So the colors supplied to the ColorDialog are constructed differently than the construction of the Color structure.



The following code performs what I think you want. It requires a form, named Form1, with a button named button1.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomColorDialog
    {

    // *************************************************** class Form1

    public partial class Form1 : Form
        {

        const int   MAXIMUM_CUSTOM_COLORS = 16;

        int [ ]     custom_colors = new int [ MAXIMUM_CUSTOM_COLORS ];
        int         custom_colors_count = 0;

        // ***************************************************** Form1

        public Form1 ( )
            {

            InitializeComponent ( );

            for ( int i = 0; ( i < custom_colors.Length ); i++ )
                {
                custom_colors [ i ] = 
                    ( Color.White.ToArgb ( ) & 0x00FFFFFF );
                }

            button1.Click += new EventHandler ( button1_Click );
            }

        // ********************************************* button1_Click

        void button1_Click ( object sender, EventArgs e )
            {
            Color       color = Color.Empty;
            ColorDialog color_dialog = new ColorDialog ( );

            color_dialog.AllowFullOpen = false;
            color_dialog.CustomColors = custom_colors;
            if ( color_dialog.ShowDialog ( ) == DialogResult.OK )
                {
                color = color_dialog.Color;
                if ( custom_colors_count >= custom_colors.Length )
                    {
                    custom_colors_count = 0;
                    }
                button1.BackColor = color;
                int color_value = ( color.B << 16 ) +
                                  ( color.G << 8 ) +
                                  color.R;
                custom_colors [ custom_colors_count++ ] = color_value;
                }
            }

        } // class Form1

    } // namespace CustomColorDialog


这篇关于如何在C#中以编程方式在颜色对话框中添加自定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆