简体   繁体   English

如何在C#中将透明添加到ColorDialog

[英]How to add Transparent to ColorDialog in C#

I want to add Transparent Color to my ColorDialog. 我想将透明色添加到我的ColorDialog中。

I tried this code : 我尝试了这段代码:

MyDialog.CustomColors = new int[] { Color.Transparent.ToArgb() };    
MyDialog.ShowDialog();    
string hex = ColorTranslator.ToHtml(MyDialog.Color);    
MessageBox.Show(hex);

But, when I pick my Custom Color (I mean Transparent), the messageBox show me "White" instead of "Transparent". 但是,当我选择“自定义颜色”(我的意思是“透明”)时,messageBox将我显示为“白色”,而不是“透明”。 convert to White itself. 转换成怀特本身。 But I want to pick Transparent. 但我想选择透明。 How can I do that? 我怎样才能做到这一点?

My guess is that it is showing the colour behind. 我的猜测是它在显示颜色。 Please post your control XAML to be sure. 请确保发布您的控件XAML。

A common workaround to that is to put a chequerboard pattern behind your swatch control, so that as the alpha is decreased, the chequer shows through. 常见的解决方法是将棋盘格图案放置在色板控件的后面,以便当Alpha减小时,检查器会显示出来。 Otherwise, how else do you show something that has no visibility? 否则,您如何显示看不见的东西?

The CustomColors property accepts colors in an Int32 composed of the BGR (blue, green, red) and you are passing it an ARGB representation of Transparent color. CustomColors属性接受由BGR组成的Int32中的颜色(蓝色,绿色,红色),并且您要向其传递透明色的ARGB表示形式。 The A in ARGB controls the alpha channel and transparency. ARGB中的A控制Alpha通道和透明度。 I would put a check box on the form for user to specify color transparency . 我会在表单上放置一个复选框,以供用户指定颜色透明度。 Else you may map your white in the color dialog box to transparent selection. 另外,您可以将颜色对话框中的白色映射到透明选择。

If (dlgCol.Color.ToArgb() == Color.White.ToArgb() )
 {
        btnColor.BackColor = Color.Transparent;

 }

http://en.wikipedia.org/wiki/RGBA_color_space http://en.wikipedia.org/wiki/RGBA_color_space

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM