使用Visual Studio和C#在Windows Studio中使用Windows窗体,是否有一种方法可以获取Graphics.DrawEllipse函数来完全按照指定的方式绘制椭圆?

我正在使用Windows Forms for Visual Studio学习C#和图形编程,并且遇到了一个问题,其中调用DrawEllipse并传递窗口大小的矩形会导致椭圆超出右侧窗口的范围和底面。

以下是绘制椭圆的代码:

class Screen extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => ScreenState();
}

class ScreenState extends State<Screen>{

  BuildContext context;

  @override
  Widget build(BuildContext context) => Scaffold(
    floatingactionButton: FloatingactionButton(
      onpressed: action,),body: Builder(
      builder : (BuildContext context){
        this.context = context;
        return Container();
      }
    )
  );

  action() => Scaffold.of(context).showsnackBar(snackBar(
    duration: Duration(milliseconds : 1000),content: Container(height: 10)
    behavior: snackBarBehavior.floating,// Add this line
  ));
}

此代码运行时,将产生以下结果: Ellipse drawn in window

我已经尝试过在Form1_Load函数中设置窗口大小,但没有任何改变。 我在某些时候应该改变一些财产吗?我发现很难相信 private void Form1_Paint(object sender,PaintEventArgs e) { e.Graphics.Clear(Color.FromArgb(0,40)); e.Graphics.DrawEllipse(new Pen(Color.FromArgb(0,150,0)),new Rectangle(0,this.Width,this.Height)); } this.Width与窗口的尺寸实际上不匹配。有人遇到过这个问题吗?

wdh709571286 回答:使用Visual Studio和C#在Windows Studio中使用Windows窗体,是否有一种方法可以获取Graphics.DrawEllipse函数来完全按照指定的方式绘制椭圆?

使用Form.WidthForm.Height时得到的是表单的尺寸,包括非客户区域。尝试使用Form.ClientSize.WidthForm.ClientSize.Height

private void Form1_Paint(object sender,PaintEventArgs e)
{
    e.Graphics.Clear(Color.FromArgb(0,40));
    e.Graphics.DrawEllipse(new Pen(Color.FromArgb(0,150,0)),new Rectangle(0,this.ClientSize.Width-1,this.ClientSize.Height-1));
}
本文链接:https://www.f2er.com/3112815.html

大家都在问