0

我在 PictureBox 上创建了一个网格,如下所示:

private void PictureBoxPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int numOfCellsWidth = 50;
            int numOfCellsHeight = 600;
            int cellSize = 20;
            Pen p = new Pen(Color.Black);

            for (int y = 0; y < numOfCellsHeight; ++y)
            {
                g.DrawLine(p, 0, y * cellSize, numOfCellsHeight * cellSize, y * cellSize);                    
            }

            for (int x = 0; x < numOfCellsWidth; ++x)
            {
                g.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCellsHeight * cellSize);
            }
        }

这就是它的外观:
在此处输入图像描述

我之前使用过 tableLayoutPanel,它有一个 CellPaint 事件,我可以将它绑定到一个数组列表,这样当列表改变时单元格的颜色就会改变。这就是我所拥有的:

private void tableLayoutPanelMainGrid_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    if (mainVisualization.mainGrid != null)
        if (mainVisualization.mainGrid.cellList != null)
            using (var b = new SolidBrush(mainVisualization.mainGrid.cellList[e.Column, e.Row].color))
                e.Graphics.FillRectangle(b, e.CellBounds);
}

我怎样才能将这两者结合起来?

4

1 回答 1

1

除非您的图片框很大,否则我认为它不会滞后。PBox 是双缓冲的,应该可以正常工作。

考虑一下:虽然您的CellPaint事件看起来很小,但实际上每次都会为每个单元格调用它,因此每次使 TLP无效都会重新绘制 TLP 的整个表面。所以:与 PBox 的情况没有太大不同。

这是您自己的示例PaintCellPaint. 它使用一个简单的 2d-Color 数组和两个ints来存储当前单元格大小。在调整电路板大小时重新计算PictureBox

Color[,] cellList;
int cellWidth = 23;
int cellHeight = 23;

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (cellList == null) InitCells(22, 22);


    for (int c = 0; c < cellList.GetLength(0); c++)
        for (int r = 0; r < cellList.GetLength(1); r++)
        {
            TableLayoutCellPaintEventArgs tep = new 
                TableLayoutCellPaintEventArgs(e.Graphics,
                    Rectangle.Round(e.Graphics.ClipBounds),
                    new Rectangle(c*cellWidth, r*cellHeight, cellWidth, cellHeight), 
                    c, r);
            pictureBox1_CellPaint(e, tep);
        }
    // insert the gridline drawing here:
    for (int c = 0; c <= cellList.GetLength(1); c++)
            e.Graphics.DrawLine(Pens.DarkSlateBlue, 0, c * cellHeight, 
                                cellWidth * cellList.GetLength(0), c * cellHeight);
    for (int c = 0; c <= cellList.GetLength(0); c++)
            e.Graphics.DrawLine(Pens.DarkSlateBlue, c * cellWidth, 0, 
                                c * cellWidth, cellHeight * cellList.GetLength(1));
}

private void pictureBox1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    //if (mainVisualization.mainGrid != null)
    //    if (mainVisualization.mainGrid.cellList != null)
            using (var b = new SolidBrush(cellList[e.Column, e.Row]))
                e.Graphics.FillRectangle(b, e.CellBounds);
}

您可以看到它是您为TLP. 不确定您是否真的应该这样做,但这是一个如何模拟CellPaint事件的示例。

当然你会想要使用你自己的cellList数据结构..

由您决定如何整合网格线的绘制。最简单的方法是在cellPaint循环之后绘制它们。

通过重新计算单元格大小(和),它将很好地显示其内容InvalidatingPictureBoxresize

在此处输入图像描述

于 2016-11-11T15:03:56.943 回答