0

我有一个矩形的图形对象,这是在单击图片框上的按钮时创建的,但是如果将表单移出屏幕,则图形消失以及整个表单变白我试图使图片框无效但这会阻止按钮工作任何人都可以帮助我使用无效方法将对象保留在表单上我知道有一种方法但我似乎无法掌握它

  private void squareButton_Click(object sender, EventArgs e)
        {

            // Declaring a new graphics object has been assigned null
            Graphics objGraphics = null;
            // This will create the picture graphics to be drawn in the picturebox
            objGraphics = PictureBox1.CreateGraphics();
            // This will redraw the picture box with a fill chosen after the systemcolors
            objGraphics.Clear(SystemColors.ControlDark);
            // This will draw the rectangle with a red pen 10,10 represent position and 50,50 reprsent the width and height 
            objGraphics.DrawRectangle(Pens.Red, 10, 10, 50, 50);
            // This will draw the rectangle
            objGraphics.Dispose();
invalidate(PictureBox1);

// This is not redrawing the graphic it just shows a blank form

        }
4

1 回答 1

0

尝试这个:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics objGraphics = null;
    // This will create the picture graphics to be drawn in the picturebox
    objGraphics = e.Graphics;
    // This will redraw the picture box with a fill chosen after the systemcolors
    objGraphics.Clear(SystemColors.ControlDark);
    // This will draw the rectangle with a red pen 10,10 represent position and 50,50 reprsent the width and height 
    objGraphics.DrawRectangle(Pens.Red, 10, 10, 50, 50);
    // This will draw the rectangle
    //objGrphics.Dispose();

}
于 2013-10-16T18:50:51.393 回答