无法在C#中的窗体加载上绘制椭圆 [英] cannot draw ellipse on form load in c#
问题描述
我正在尝试在包含C#中的formload上的PNG的图片框中绘制一些椭圆.当我在下面执行代码时,我会看到椭圆半秒钟,然后再也看不到它们了.
I am trying to draw some ellipse in a picturebox that contains a PNG on a formload in c#. When I execute the code down below, I see my ellipses for half of a second, then I don't see them no more.
当我单击图片框时,我可以绘制一个椭圆,但是,当我最小化窗体时,它们不再显示.
When I click on my picturebox, I am able to draw an ellipse however, when I minimize the form, they don't appear no more.
我读到您不应该将绘图代码放在formload中,而应该放在OnPaint方法中,这就是我所做的.我不知道该怎么办了.谢谢你. (请注意,我已经留下了一些代码注释以显示我尝试过的内容).
I've read that you shouldn't put your drawing code in the formload but rather in the OnPaint method, which is what I did. I don't know what to try anymore. thank you. (Be aware that I've left some code commented to show what I've tried).
public partial class FormParterre : Form
{
Graphics g;
float circleSize = 15;
//Brushes rouge = new Brushes (Brushes.Red);
ReservationBilletSiegeDAO reservationBilletSiegeDAO = new ReservationBilletSiegeDAO();
SiegeDAO siegeDAO = new SiegeDAO();
List <Siege> sieges;
List<ReservationBilletSiege> rbs;
ReservationBillet reservationBillet = new ReservationBillet();
ReservationBilletSiege reservationBilletSiege;
SolidBrush semiTransBrush;
public FormParterre()
{
InitializeComponent();
pictureBox1.Image = new Bitmap("./parterre.png");
g = pictureBox1.CreateGraphics();
}
public FormParterre(ReservationBillet rb)
{
reservationBillet = rb;
pictureBox1.Image = new Bitmap("./parterre.png");
g = pictureBox1.CreateGraphics();
InitializeComponent();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// do nothing! prevents flicker
}
protected override void OnPaint(PaintEventArgs e)
{
sieges = siegeDAO.readAll();
rbs = reservationBilletSiegeDAO.readAll();
foreach (ReservationBilletSiege reservationBilletSiegeTMP in rbs)
{
Console.WriteLine(reservationBilletSiegeTMP.toString());
int x = siegeDAO.read(reservationBilletSiegeTMP.idSiege).xValeur;
int y = siegeDAO.read(reservationBilletSiegeTMP.idSiege).yValeur;
float xx = (float)x;
float yy = (float)y;
Console.WriteLine("le x: " + xx);
Console.WriteLine("le y: " + yy);
/*e.Graphics.FillRectangle(new SolidBrush(BackColor), e.ClipRectangle);*/
g.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 0, 255)), xx - circleSize / 2, yy - circleSize / 2, circleSize, circleSize);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
txtX.Text = me.X.ToString();
txtY.Text = me.Y.ToString();
Console.WriteLine("click");
g.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 0, 255)), float.Parse(txtX.Text) - circleSize / 2, float.Parse(txtY.Text) - circleSize / 2, circleSize, circleSize);
}
private void FormParterre_Shown(object sender, EventArgs e)
{
}
private void FormParterre_Load(object sender, EventArgs e)
{
/*sieges = siegeDAO.readAll();
//semiTransBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 255));
rbs = reservationBilletSiegeDAO.readAll();
foreach (ReservationBilletSiege reservationBilletSiegeTMP in rbs)
{
Console.WriteLine(reservationBilletSiegeTMP.toString());
int x = siegeDAO.read(reservationBilletSiegeTMP.idSiege).xValeur;
int y = siegeDAO.read(reservationBilletSiegeTMP.idSiege).yValeur;
float xx = (float)x;
float yy = (float)y;
Console.WriteLine("le x: " + xx);
Console.WriteLine("le y: " + yy);
g.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 0, 255)), xx - circleSize / 2, yy - circleSize / 2, circleSize, circleSize);
}*/
}
}
推荐答案
您需要使用传递给您的PaintEventArgs.Graphics
属性在OnPaint
方法中进行绘制.如果要在PictureBox
上绘画,可以尝试订阅其Paint
事件并在其中绘画.
You need to paint in the OnPaint
method using the PaintEventArgs.Graphics
property which is passed to you. If you want to paint on the PictureBox
you can try subscribing to its Paint
event and painting there.
这对我有用:
public void Form1()
{
InitializeComponent();
pictureBox1.Paint += pictureBox1_Paint;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Red, pictureBox1.ClientRectangle);
}
这篇关于无法在C#中的窗体加载上绘制椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!