c#绘图,圆圈超出形式

c# drawing, circle goes outside form

我想在表格内画一个圆圈。 但对我来说很奇怪我将表格宽度和高度设置为固定数字,我对圆圈也这样做,但是圆圈图形超出了表格。

private void Form3_Paint(object sender, PaintEventArgs e)
{        
    this.SuspendLayout();

    gr = this.CreateGraphics();
    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    Brush fill_circ3 = Brushes.Blue;

    Pen ellipse_pen = new Pen(Color.Blue);            
    ellipse_pen.Width = (float)2.0;

    this.Width = this.Height = 400;
    Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
    gr.DrawEllipse(ellipse_pen, rect);

    this.ResumeLayout();
}

Rectangle 构造函数的第三个和第四个参数定义了圆的宽度和高度。

看我得到的圈子

为什么圆圈超出了形式???!我把表格和圆的大小设置成一样了!!!

这是因为您使用的是 Window 尺寸,而不是 "client" size。只需将您的代码替换为:

gr.DrawEllipse(ellipse_pen, this.ClientRectangle);

The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus.