使用C#绘制多个矩形

3

如何在C#中绘制25个矩形(5*5)?

我之后需要能够访问特定的矩形并更改它的颜色,例如如果用户输入不正确的单词,则更改颜色为红色。

在这种情况下,创建一个矩形数组是否更合适?

这是我目前的代码:

Graphics g = pictureBox1.CreateGraphics();

int x =0;
int y= 0;
int width = 20;
int height = 20;
for (int i = 0; i < 25; i++)
{
    if (i <= 4)
    {
        g.FillRectangle(Brushes.Blue, x, y, width, height);
        x += 50;
    }
    else if (i > 4)
    {
        y = 50;
        g.FillRectangle(Brushes.Blue, x, y, width, height);
        x += 50;
    }
}

2
创建非自动调整大小的空标签、面板或其他组件。 - SimpleVar
一个循环不够好。最好有两个循环,一个嵌套在另一个里面。for (int yPos = 0; yPos < 5; yPos++) { for (int xPos = 0; xPos < 5; xPos++) { /* 在这里编写代码 */ } - Kieren Johnstone
你考虑过使用WPF并在XAML中使用Canvas来完成这个吗? - Will Faithfull
@ Kieren Johnstone,我不完全理解你的代码意图,请你能更具体地说明一下吗?先谢谢了。 - user1808211
@Ran 作业标签已被官方弃用,请不要再使用它。 - rikitikitik
永远不要使用control.CreateGraphics!永远不要尝试缓存绑定到控件的Graphics对象!可以使用Graphics g = Graphics.FromImage(bmp)Bitmap bmp中进行绘制,或者在控件的Paint事件中使用e.Graphics参数进行绘制。您可以通过执行最小化/最大化序列来测试图形的持久性。 - TaW
2个回答

1

这应该可以让你开始,但不是完整的代码。你需要添加一个PictureBox控件并使用默认名称(picurebox1)。编辑:还需要添加一个按钮 :)

public partial class Form1 : Form
{
    public List<Rectangle> listRec = new List<Rectangle>();
    Graphics g;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Rectangle rect = new Rectangle();
        rect.Size = new Size(100,20);
        for (int x = 0; x < 5; x++)
        {
            rect.X = x * rect.Width;
            for (int y = 0; y < 5; y++)
            {
                rect.Y = y * rect.Height;
                listRec.Add(rect);
            }
        }

        foreach (Rectangle rec in listRec)
        {
            g = pictureBox1.CreateGraphics();
            Pen p = new Pen(Color.Blue);
            g.DrawRectangle(p, rec);
        }
    }

    public void ChangeColor(Rectangle target, Color targetColor)
    {
        Pen p = new Pen(targetColor);
        g.DrawRectangle(p, target.X, target.Y, target.Width, target.Height);
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.D0: ChangeColor(listRec[0], Color.Red);
                break;
            case Keys.D1: ChangeColor(listRec[1], Color.Red);
                break;
            //..more code to handle all keys..
        }
    }    
}

谢谢,我搞定了。如果我想改变特定矩形的颜色,最简单的方法是什么? - user1808211
最简单的方式是主观的。你可以根据鼠标位置在鼠标点击时更改颜色,或者像我上面所做的那样使用一个键。确保将窗体属性“KeyPreview”设置为True,否则它不会接受键盘输入。你真正需要的只是一个改变颜色的函数,然后确定哪些矩形需要改变的方法。 - dcreight
很酷。但是只有一个问题。请问如何在每个矩形内添加一些文本?谢谢! - user3790692
永远不要使用control.CreateGraphics!结果是非持久的!!- 要么使用Graphics g = Graphics.FromImage(bmp)Bitmap bmp中绘制,要么在控件的Paint事件中使用e.Graphics参数进行绘制。您可以通过执行最小化/最大化序列来测试图形的持久性。 - TaW

0
如果您不关心性能或外观,那么最简单的方法是在Form_Load事件中创建一个Panel列表的列表,正如其中一条评论所提到的那样。
List<List<Panel>> panelGrid = new List<List<Panel>>();
for (var i = 0; i < 5; i++)
{  
    var panelRow = new List<Panel>();
    for (var j = 0; j < 5; j++)
    {
        panelRow.Add(new Panel());

        // add positioning logic here
    }

    panelGrid.Add(panelRow);
}

然后您将能够在以后的阶段中引用每个单独的对象...

如果您必须使用Graphics类(这是更好的方法),那么您应该设置类似的东西,不过要用自己的类替换Panel。然后在Form_Paint事件中,您将遍历对象列表并呈现它们。

class MyPanel
{
    public Size size;
    public Color color;
}

...

foreach (var myPanelRow in myPanelGrid)
    foreach (var myPanel in myPanelRow)
        g.FillRectangle(myPanel.color, myPanel.size); // this obviously won't work as is, but you get the idea

然后当您需要更改颜色时,可以执行以下操作:

myPanelsGrid[0][0].color = Color.Blue;
myForm.Invalidate();

第二行代码会导致事件中再次调用 Paint。

性能不是问题,所以您使用面板的方法很好。您能给我一个添加逻辑的示例吗?谢谢。 - user1808211

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接