【发布时间】:2013-05-29 03:51:21
【问题描述】:
我正在创建一个颜色选择器,并且我有一个“输入”和“输出”。在输入中我有 4 个滚动条,在输出中我有一个预览,以及 3 个带有输出的文本框。
当您单击预览时,将打开一个带有颜色对话框的窗口。它适用于十六进制并返回一个十六进制值。
但是,我想将它返回的十六进制值放在滚动条中。
我认为我必须将其转换为 RGB 或 Int,然后设置滚动条值。
我该怎么做?
来自图片框的代码:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.BackColor = colorDialog1.Color;
string color, colorRGB;
color = string.Format("{0:X8}", pictureBox1.BackColor.ToArgb());
colorRGB = string.Format("{0:X6}", pictureBox1.BackColor.ToArgb());
hexResult = color;
pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " 0x" + hexResult;
hexTextBox.Text = "0x" + hexResult;
}
}
滚动条中的代码:
private void alphaScroll_Scroll(object sender, ScrollEventArgs e)
{
Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
pictureBox1.BackColor = previewColor;
int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));
hexTextBox.Text = hexResult;
pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
alphaResultLabel.Text = alphaScroll.Value.ToString();
}
private void redScroll_Scroll(object sender, ScrollEventArgs e)
{
Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
pictureBox1.BackColor = previewColor;
int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));
int red = redScroll.Value;
int green = greenScroll.Value;
int blue = blueScroll.Value;
getColorTxtBox.Text = red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
embeddTextBox.Text = "{" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2") + "}";
hexTextBox.Text = hexResult;
pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
redResultLabel.Text = redScroll.Value.ToString();
}
private void greenScroll_Scroll(object sender, ScrollEventArgs e)
{
Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
pictureBox1.BackColor = previewColor;
int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));
int red = redScroll.Value;
int green = greenScroll.Value;
int blue = blueScroll.Value;
getColorTxtBox.Text = red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
embeddTextBox.Text = "{" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2") + "}";
hexTextBox.Text = hexResult;
pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
greenResultLabel.Text = greenScroll.Value.ToString();
}
private void blueScroll_Scroll(object sender, ScrollEventArgs e)
{
Color previewColor = Color.FromArgb(alphaScroll.Value, redScroll.Value, greenScroll.Value, blueScroll.Value);
pictureBox1.BackColor = previewColor;
int colorHex = alphaScroll.Value | (blueScroll.Value << 8) | (greenScroll.Value << 16) | (redScroll.Value << 24);
hexResult = "0x" + string.Format("{0:X}", colorHex.ToString("X8"));
int red = redScroll.Value;
int green = greenScroll.Value;
int blue = blueScroll.Value;
getColorTxtBox.Text = red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
embeddTextBox.Text = "{" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2") + "}";
hexTextBox.Text = hexResult;
pawnTextBox.Text = "#define " + colorNameTxtBox.Text + " " + hexResult;
blueResultLabel.Text = blueScroll.Value.ToString();
}
【问题讨论】:
-
请提供您的代码并明确说明您的问题。
-
不知道提供什么代码,代码很大。我有 4 个滚动条(Alpha、红色、绿色和蓝色)并想在上面放置一个十六进制值。但我必须使用整数,我不知道该怎么做。
-
你能让你的滚动条有一个
minimum值0和一个maximum的255吗?然后从那里做一些转换? -
已经做了,我会在问题中添加一些代码。
-
我已将问题回滚到删除“问题”代码之前。如果您已经解决了自己的问题,发布并回答,不要替换您的问题。我已从您的问题中提取了“答案”,并将其作为下面的答案。
标签: c# int hex rgb scrollbars