Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表格上的零件图纸清单_C# - Fatal编程技术网

C# 表格上的零件图纸清单

C# 表格上的零件图纸清单,c#,C#,首先,我是C#世界的新手。 我今年刚开始学习C#,没有编程背景经验。 我带着一个问题来到这里,我想尽快得到解决。 我设法写了一些代码来创建一个包含绘制位置的位图列表。 我想做的是在表单上画出列表中的每一块(一个点的图片)。 我花了好几个小时在这上面,只是想弄清楚如何在表格上画出清单。。。 我在代码后面添加了注释,使读者更容易理解它的用途,这样就不会对读者造成任何大脑损伤或出汗P 请参阅下面的代码: Form1.cs public partial class Form1 : Form {

首先,我是C#世界的新手。 我今年刚开始学习C#,没有编程背景经验。 我带着一个问题来到这里,我想尽快得到解决。 我设法写了一些代码来创建一个包含绘制位置的位图列表。 我想做的是在表单上画出列表中的每一块(一个点的图片)。 我花了好几个小时在这上面,只是想弄清楚如何在表格上画出清单。。。 我在代码后面添加了注释,使读者更容易理解它的用途,这样就不会对读者造成任何大脑损伤或出汗P 请参阅下面的代码:

Form1.cs

public partial class Form1 : Form
{
    private GridDrawing drawing;
    private Bitmap bmpPic;

    public Form1()
    {
        InitializeComponent();

        bmpPic = new Bitmap("Dot.png"); // Picture(Dot 28*28 pixels)
        this.Paint += Form_Paint;

    }

    private void Form_Paint(object sender, PaintEventArgs e)
    {
        drawing = new GridDrawing(this, bmpPic, 6, 8); // Form, Bitmap, Rows, Columns
        foreach (var piece in drawing.Pieces)
        {
            e.Graphics.DrawImage(bmpPic, piece.Position);
        }
    }

    private void btnStart_Click(object sender, PaintEventArgs e)
    {
    }
}
    public class GridDrawing
{
    private Bitmap bmpPic;
    private int columns;
    private int rows;
    private List<GridPiece> pieces;

    private Point position;

    /// <summary>
    /// Constructs a grid with dots.
    /// </summary>
    /// <param name="ctrl"></param>
    /// <param name="gridPic"></param>
    /// <param name="rows"></param>
    /// <param name="columns"></param>
    public GridDrawing(Control ctrl, Bitmap bmpPic, int rows, int columns)
    {
        this.bmpPic = bmpPic;                                         //The picture(Dot).
        this.rows = rows;                                             //The amount of rows in the matrix.
        this.columns = columns;                                       //The amount of columns in the matrix.

        this.pieces = new List<GridPiece>();                                            //Initializes the List GridPieces
        Point position = new Point(0, 0);                                               //Draw position of the picture(Dot)
        Size size = new Size(bmpPic.Width, bmpPic.Height);                              //Size of picture(Dot).

        for (int i = 0; i <= rows; i++)             //A  with 6 rows
        {
            position.X = 0;                         //Puts the value X on 0 when it starts a new row.
            for (int j = 0; j <= columns; j++)      //A matrix with 8 columns
            {
                GridPiece s = new GridPiece(bmpPic, position);  // Creates a piece
                pieces.Add(s);                                  // Puts the piece that has to be drawn in the List<GridPiece>pieces
                position.X += size.Width;                       // Changes the width of the draw position
            }
            position.Y += size.Height;                          // Changes the height of the draw position
        }
    }

    public List<GridPiece> Pieces
    {
        get { return this.pieces; }
    }
}
public class GridPiece
{
    private Bitmap bmpPic;
    private Point position;

    /// <summary>
    /// Constructor of GriedPiece
    /// </summary>
    /// <param name="bmpPic"></param>
    /// <param name="position"></param>
    public GridPiece(Bitmap bmpPic, Point position)
    {
        this.bmpPic = bmpPic;
        this.position = position;
    }

    public Point Position
    {
        get { return position; }
    }
}
GridDrawing.cs

public partial class Form1 : Form
{
    private GridDrawing drawing;
    private Bitmap bmpPic;

    public Form1()
    {
        InitializeComponent();

        bmpPic = new Bitmap("Dot.png"); // Picture(Dot 28*28 pixels)
        this.Paint += Form_Paint;

    }

    private void Form_Paint(object sender, PaintEventArgs e)
    {
        drawing = new GridDrawing(this, bmpPic, 6, 8); // Form, Bitmap, Rows, Columns
        foreach (var piece in drawing.Pieces)
        {
            e.Graphics.DrawImage(bmpPic, piece.Position);
        }
    }

    private void btnStart_Click(object sender, PaintEventArgs e)
    {
    }
}
    public class GridDrawing
{
    private Bitmap bmpPic;
    private int columns;
    private int rows;
    private List<GridPiece> pieces;

    private Point position;

    /// <summary>
    /// Constructs a grid with dots.
    /// </summary>
    /// <param name="ctrl"></param>
    /// <param name="gridPic"></param>
    /// <param name="rows"></param>
    /// <param name="columns"></param>
    public GridDrawing(Control ctrl, Bitmap bmpPic, int rows, int columns)
    {
        this.bmpPic = bmpPic;                                         //The picture(Dot).
        this.rows = rows;                                             //The amount of rows in the matrix.
        this.columns = columns;                                       //The amount of columns in the matrix.

        this.pieces = new List<GridPiece>();                                            //Initializes the List GridPieces
        Point position = new Point(0, 0);                                               //Draw position of the picture(Dot)
        Size size = new Size(bmpPic.Width, bmpPic.Height);                              //Size of picture(Dot).

        for (int i = 0; i <= rows; i++)             //A  with 6 rows
        {
            position.X = 0;                         //Puts the value X on 0 when it starts a new row.
            for (int j = 0; j <= columns; j++)      //A matrix with 8 columns
            {
                GridPiece s = new GridPiece(bmpPic, position);  // Creates a piece
                pieces.Add(s);                                  // Puts the piece that has to be drawn in the List<GridPiece>pieces
                position.X += size.Width;                       // Changes the width of the draw position
            }
            position.Y += size.Height;                          // Changes the height of the draw position
        }
    }

    public List<GridPiece> Pieces
    {
        get { return this.pieces; }
    }
}
public class GridPiece
{
    private Bitmap bmpPic;
    private Point position;

    /// <summary>
    /// Constructor of GriedPiece
    /// </summary>
    /// <param name="bmpPic"></param>
    /// <param name="position"></param>
    public GridPiece(Bitmap bmpPic, Point position)
    {
        this.bmpPic = bmpPic;
        this.position = position;
    }

    public Point Position
    {
        get { return position; }
    }
}
公共类GridDrawing
{
私有位图bmpPic;
私有int列;
私有int行;
私人物品清单;
私人点位;
/// 
///用点构建网格。
/// 
/// 
/// 
/// 
/// 
公共网格绘图(控件ctrl、位图bmpPic、int行、int列)
{
this.bmpPic=bmpPic;//图片(点)。
this.rows=rows;//矩阵中的行数。
this.columns=columns;//矩阵中的列数。
this.pieces=new List();//初始化列表GridPieces
点位置=新点(0,0);//绘制图片的位置(点)
大小大小=新大小(bmpPic.Width,bmpPic.Height);//图片大小(点)。

对于(int i=0;i您需要处理表单的
Paint
事件,并使用
e.Graphics
中的方法在循环中绘制片段

(可能是
e.Graphics.DrawImage
,也可能是
FillCircle

看起来像

void Form_Paint(object sender, PaintEventArgs e) { 
    foreach(var piece in drawing.Pieces) {
        e.Graphics.DrawImage(bmpPic, piece.Position);
    }
}
每次需要绘制形状时都会引发绘制事件


当碎片移动时,您需要通过调用
Invalidate()

手动强制表单重新绘制,您能给我一个示例代码,以便我更容易理解吗?:)我的态度如何,首先我要感谢你的时间。我尝试了你的例子,我会编辑原始帖子,这样你就可以看到我做了什么。piece.position声明由于其保护级别,它无法访问。我已经尝试将其公开(暂时只是为了测试)。有什么建议可以解决这个问题吗?你需要将它们公开。你应该公开那些公开它们的只读属性。我想是的。这可能就是你使用大写字母的原因。位置是。你应该通过单击空心复选框来接受这个答案。