2013-06-03 82 views
1

我创建了一个拾色器,并且我有一个“输入”和“输出”。在输入中,我有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(); 
    } 
+0

请提供您的代码,并清楚地说明你的问题。 – PSS

+0

我不知道要提供什么代码,代码很大。 我有4个滚动条(阿尔法,红色,绿色和蓝色),并希望把一个十六进制值。但我将不得不使用整数来它,我不知道我该怎么做。 – Los799

+0

你可以让你的滚动条有'最小'值'0'和'最大'255吗?然后从那里做一些转换? – gunr2171

回答

0

没有为在.NET函数:

Color c = System.Drawing.ColorTranslator.FromHtml(hmtlcolor); 
0

Disclamer:这是由作者张贴在问题中,替换有问题的代码。作为一个社区维基答案,我已经回滚了这个问题并将解决的代码放在这里。


如果有人让我是同样的问题,这里的解决方案:

string c = color.ToString(); 
string redHex = c.Substring(0, 2); 
string greenHex = c.Substring(2, 2); 
string blueHex = c.Substring(4, 2); 
string alphaHex = c.Substring(6, 2); 

int red = Convert.ToInt32(redHex, 16); 
int green = Convert.ToInt32(greenHex, 16); 
int blue = Convert.ToInt32(blueHex, 16); 
int alpha = Convert.ToInt32(alphaHex, 16); 

alphaScroll.Value = alpha; 
redScroll.Value = red; 
greenScroll.Value = green; 
blueScroll.Value = blue; 

alphaResultLabel.Text = alphaScroll.Value.ToString(); 
redResultLabel.Text = redScroll.Value.ToString(); 
greenResultLabel.Text = greenScroll.Value.ToString(); 
blueResultLabel.Text = blueScroll.Value.ToString();