0

我想绘制 16*64 矩阵,每个矩阵包含一个:

Microsoft.VisualBasic.PowerPacks.OvalShape.

我用这个:

List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = 
    new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();

for (int i = 0; i < 16; i++)
{
    for (int j = 0; j < 64; j++)
    {
        OvalShape ovl = new OvalShape();
        ovl.Width = 20;
        ovl.Height = 20;
        ovl.FillStyle = FillStyle.Solid;
        ovl.FillColor = Color.Green;

        ovalShape.Add(ovl);
    }
}

如何在窗口中显示它?

4

3 回答 3

2

不需要为每个形状创建单独的容器。您也可以跳过形状的附加容器列表。所以你可以使用这个非常紧凑的代码。

        var ovalShapes = new Microsoft.VisualBasic.PowerPacks.ShapeContainer()
        {
            Dock = DockStyle.Fill,
            Margin = new Padding(0),
            Padding = new Padding(0),
        };
        for (int i = 0; i < 16; i++)
            for (int j = 0; j < 64; j++)
                ovalShapes.Shapes.Add(
                    new Microsoft.VisualBasic.PowerPacks.OvalShape()
                    {
                        Width = 20,
                        Height = 20,
                        FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid,
                        FillColor = Color.Green,
                        Location = new Point(20 * i, 20 * j),
                    });
        this.Controls.Add(ovalShapes);
于 2012-09-24T09:09:35.027 回答
1

来自MSDN

OvalShape 控件不能直接显示在窗体或容器控件上;它必须包含在 ShapeContainer 对象中。初始化 OvalShape 后,必须将其 Parent 属性设置为现有的 ShapeContainer 或 ShapeContainer 的新实例。

尝试设置位置并将您的控件添加到表单:

        List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();

        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < 64; j++)
            {
                OvalShape ovl = new OvalShape();
                ovl.Width = 20;
                ovl.Height = 20;
                ovl.FillStyle = FillStyle.Solid;
                ovl.FillColor = Color.Green;

                ovl.Location = new Point(ovl.Width*i, ovl.Height*j);

                ovalShape.Add(ovl);
            }
        }

        foreach(OvalShape os in ovalShape)
        {
              Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
              Control c = new Control();
              shapeContainer.Parent = c;
              os.Parent = shapeContainer;
              myForm.Controls.Add(c);              
        }
于 2012-03-28T20:48:48.623 回答
0

首先简化

int totalCount = 1024; //16*64
const int shapeWidth  =20;
const int shapeHeight = 20;

for (int j = 0; j < totalCount; j++)
{
   OvalShape ovl = new OvalShape();
   ovl.Width = shapeWidth;
   ovl.Height = shapeHeight;
   ovl.FillStyle = FillStyle.Solid;
   ovl.FillColor = Color.Green;

   ovalShape.Add(ovl);
}

在拾取您的绘图区域并决定每行您想要多少形状后。所以一些假设的代码可能如下所示:

int shapesPerRowCount = 5;
int yPos = 0;

for(int i=0;i<ovalShape.Count;i++)
{
  if(i % shapesPerRowCount  == 0) //reach end of the row, so offset Y position by Height
     yPos += shapeHeight; 

  int xPos = i*shapeWidth;
  DrawShapeAtPos(ovalShape[i], xPos, yPos); //some special function that draws the shape
}

一个非常通用的代码,但只是为您提供一个想法。

如果这不是您要搜索的内容,请澄清。

于 2012-03-28T20:55:58.707 回答