for(i=1;i<5;I++)
{
g.FillRectangle(new SolidBrush(Colors.Red),rect[i]);
}
请问我如何编写可以实现不同的颜色的矩形!
怎么写呀!

解决方案 »

  1.   


      Int32[] Colors = new Int32[10];
                    Colors[0] = Color.Red.ToArgb();             
                    Colors[1] = Color.Yellow.ToArgb();
                    Colors[2] = Color.YellowGreen.ToArgb();
                    Colors[3] = Color.Teal.ToArgb();
                    Colors[4] = Color.Blue.ToArgb();
                    Colors[5] = Color.Black.ToArgb();
                    Colors[6] = Color.Brown.ToArgb();
                    Colors[7] = Color.Cyan.ToArgb();
                    Colors[8] = Color.DarkMagenta.ToArgb();
                    Colors[9] = Color.DarkOrchid.ToArgb();
                    
                    
                   this.button1.BackColor = Color.FromArgb(Colors[i]);//10种颜色,自己给i赋值就行了
      

  2.   


    实现不同的颜色是随机呢?还是预先设置好呢?随机的话,Colors.Red本身就是一个枚举,你随机生产一个数字转换为Colors枚举就可以了
      

  3.   

    Color.Red确实是一个枚举,可是没下标啊,如果是FOR循环的话,怎么和其它颜色区别呢?
    如果用随机产生COLOR.FromArgb(随机数1,2,3)这种的话,产生的颜色很可能对比度很低啊。请赐教啊。
      

  4.   


    Graphics g = e.Graphics;
                Color[] colorArr=new Color[5];
                colorArr[0] = Color.Red;
                colorArr[1] = Color.Black;
                colorArr[2] = Color.Blue;
                colorArr[3] = Color.Brown;
                colorArr[4] = Color.White;            for (int i = 0, j = 0; i < 5 || j < colorArr.Length; i++, j++)
                {
                    g.FillRectangle(new SolidBrush(colorArr[i]), rect[i]);
                }
      

  5.   


    int minColorArgb = Color.FromArgb(0, 0, 0).ToArgb();
                int maxColorArgb = Color.FromArgb(255, 255, 255).ToArgb();
                Color c = Color.FromArgb(new Random().Next(minColorArgb, maxColorArgb + 1));            Graphics g = e.Graphics;
                for (int i = 0, j = 0; i < 5 || j < colorArr.Length; i++, j++)
                {
                    g.FillRectangle(new SolidBrush(colorArr[i]), rect[i]);
                }
    我以前这样写过随机颜色。你看看吧
      

  6.   

    msdn上的,关键是Color.FromName和KnownColor枚举,你可以随机产生一个knowncolor枚举,作为参数给FromName静态方法。private void DisplayKnownColors(PaintEventArgs e)
    {
        this.Size = new Size(650, 550);    // Get all the values from the KnownColor enumeration.
        System.Array colorsArray = Enum.GetValues(typeof(KnownColor));
        KnownColor[] allColors = new KnownColor[colorsArray.Length];    Array.Copy(colorsArray, allColors, colorsArray.Length);    // Loop through printing out the values' names in the colors 
        // they represent.
        float y = 0;
        float x = 10.0F;    for(int i = 0; i < allColors.Length; i++)
        {        // If x is a multiple of 30, start a new column.
            if (i > 0 && i % 30 == 0)
            {
                x += 105.0F;
                y = 15.0F;
            }
            else
            {
                // Otherwise, increment y by 15.
                y += 15.0F;
            }        // Create a custom brush from the color and use it to draw
            // the brush's name.
            SolidBrush aBrush = 
                new SolidBrush(Color.FromName(allColors[i].ToString()));
            e.Graphics.DrawString(allColors[i].ToString(), 
                this.Font, aBrush, x, y);        // Dispose of the custom brush.
            aBrush.Dispose();
        }}