Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CustomColors Property in ColorDialog control

168 views
Skip to first unread message

Tony Lin

unread,
Aug 7, 2003, 8:30:09 PM8/7/03
to

I am having trouble using the CustomColors property in the ColorDialog control.

 

I have created an int array from ARGB values and assigned it to the CustomColors property of my ColorDialog control.  AllowFullOpen has been set to true.

 

Yet when I execute ShowDialog, the boxes specified for custom colors display only black.

 

Here's my code:

 

int[] customColors = new int[4];

 

customColors[0] = Color.Beige.ToArgb();

customColors[1] = Color.CadetBlue.ToArgb();

customColors[2] = Color.Chocolate.ToArgb();

customColors[3] = Color.DeepPink.ToArgb();

 

this.colorDialog.AllowFullOpen    = true;

this.colorDialog.AnyColor         = true;

this.colorDialog.CustomColors     = customColors;

 

DialogResult dr = this.colorDialog.ShowDialog();

 

 

 

The ColorDialog displays 4 black squares and 12 white squares in the Custom Colors section.

 

What am I doing wrong here?

 

Tony Lin

Fremont, CA

 

 

Ying-Shen Yu

unread,
Aug 8, 2003, 4:19:43 AM8/8/03
to
Hello Tony,
Because the .NET GDI+ uses the AARRGGBB color model, while the Windows
still uses the 00BBGGRR color model. Unfortunately, the property
CustomColors of ColorDialog didn't do this convertion internally, so you
must convert every argb value manuly. It's a known problem, I hope it will
be solved soon.

You may work around this by using the following static method

public static int ColorToColorref(Color clr)
{
return (clr.R + (clr.G << 8) + (clr.B << 16));
}

e.g.
customColors[0] = ColorToColorref(Color.Biege);

Be free to let me know if you have any questions on this issue, Thanks!


Kind regards,

Ying-Shen Yu [MSFT]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.

Tony Lin

unread,
Aug 8, 2003, 12:27:22 PM8/8/03
to
Ying-Shen,

Thanks for your excellent tip.

Now that I can select from my list of custom colors, I have another
question.

The ColorDialog's "Add to Custom Colors" doesn't seem to work.

First, I can only change the first custom color in the array. The other
custom color in the array can't be changed.

Second, "Add to Custom Colors" doesn't do any good since no changes are
saved.

When I rerun this, my custom colors int array is unchanged.

Am I missing something, or is this a defect in the ColorDialog control?

Tony Lin

"Ying-Shen Yu" <v-...@online.microsoft.com> wrote in message
news:J#DgVXYXD...@cpmsftngxa06.phx.gbl...

Jonny Yu

unread,
Aug 9, 2003, 10:30:13 AM8/9/03
to
Hi Tony,
Because the ColorDialog uses the native windows common control- color
dialog, I guess it must be a P/Invoke, so the CustomColors array in
colordialog is a copy of your customColor array, not a reference, that's why
your color array remain unchanged after you changed custom color in the
dialog. Just copy it back to your own array will be OK, like this:

int[] customColors = new int[]{....};
dlg.CustomColors = customColors;
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
panel1.BackColor = dlg.Color;
customColors = dlg.CustomColors;//this line!
}

I've built a simple example for you, you may try it, thanks!

Kind regards,

Ying-Shen Yu [MSFT]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.

"Tony Lin" <to...@linfamily.org> 写入邮件
news:eJVXrncX...@TK2MSFTNGP12.phx.gbl...

0 new messages