繁体   English   中英

DrawRectangle超出范围

[英]DrawRectangle goes out of form bounds

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        g = e.Graphics;
        blockList.DrawBlocksInList(g);  //Drawing the blocks that were added to the list in the Form1_Load below;
        g.FillRectangle(MainBlock._Pen.Brush, MainBlock._Rectangle);  //Drawing the Main Block;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        float rectSize = this.Width / 10.0f;    //What the size of each rectangle should be, depending on the form's size;
        Block BlockForList; //No need to initialize it;
        for (float y = 0; y < this.Height; y += rectSize)   //500 is the size of the Form, 50 is the size of the X & Y of each rectangle;
            for (float x = 0; x < this.Width; x += rectSize)
            {
                BlockForList = new Block(pen, new RectangleF(x, y, rectSize, rectSize)); //Initializing the block each time to set different locations;
                blockList.list.Add(BlockForList);   //Adding the blocks to the list to draw them all;
            }
    }

之后,我使用一种方法来绘制blockList.list中的所有块,但是会发生这种情况: http ://imgur.com/5h7ZMTH

当然,它将超出范围-您已告诉它允许每个块的左边缘上移至Width ,而右边缘上移至Height

你想要这个吗?

    for (float y = 0; y < this.Height-rectSize; y += rectSize)   //500 is the size of the Form (this.Width & this.Height), 50 (rectSize) is the size of the X & Y of each rectangle (basically squares);
        for (float x = 0; x < this.Width-rectSize; x += rectSize)

-

编辑

ah-同样在绘画中,您首先需要调用base OnPaint()方法。 这将填补背景。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM