简体   繁体   中英

How to draw a string on a picturebox with C# immediately after I show it?

I have a main windows form called Menu.cs , in this class I have two buttons called 'lines' and 'polygons', each button open its respective window. Polygons got a picturebox and I have to draw the x and y axis

private void botonPoligonos_Click(object sender, EventArgs e) 
{
    WindowPolygons objPolygons = new WindowPolygons();
    objPolygons.Show();
}

I actually do it with a button called 'drawAxes':

public void drawAxes_Click() 
{
    drawAxes();
}

public void drawAxes()
{
    Graphics papel = pictureBox1.CreateGraphics();
    Pen lapiz = new Pen(Color.Black);
    //Dibujo de ejes X y Y
    papel.DrawLine(lapiz, 20, 425, 742, 425);
    papel.DrawLine(lapiz, 20, 425, 20, 20);
    papel.DrawString("X", myFont, Brushes.Black, new Point(745, 418));
    papel.DrawString("Y", myFont, Brushes.Black, new Point(14, 5));
}

But I don't want to do it with a button, I'd like to draw the axes since the picturebox is shown, I already tried to put the method drawAxes() in the constructor, in the loader of WindowPolygons.cs and under objPolygons.show() but nothing happens, do anyone know how to do it?

This is my first time making a question, thanks:)

Use the following code and the following setup:

private void drawAxes(Graphics g, Rectangle rect)
{
    Pen lapiz = new Pen(Color.Black);
    //Dibujo de ejes X y Y
    g.DrawLine(lapiz, 20, rect.Height - 20, rect.Width - 20, rect.Height - 20);
    g.DrawLine(lapiz, 20, rect.Height - 20, 20, 20);
    g.DrawString("X", Font, Brushes.Black, new Point(rect.Width - 17 , rect.Height - 27));
    g.DrawString("Y", Font, Brushes.Black, new Point(14, 5));
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    drawAxes(e.Graphics, pictureBox1.Bounds);
}

private void pictureBox1_Resize(object sender, EventArgs e)
{
    pictureBox1.Invalidate();
}

Have the pictureBox1 docked to the form (property Dock set to Fill ), so that it automatically grows with the form.

Attach the method pictureBox1_Paint to the Paint-Event of pictureBox1.

Attach the method pictureBox1_Resize to the Resize-Event of pictureBox1.

The call to Invalidate() in pictureBox1_Resize() asks the pictureBox1 to repaint it when its size has changed.

The method pictureBox1_Paint() is automatically called when the picture box needs to be drawn, either because for initial display or because the size has changed or anything else. It calls drawAxes to do the actual drawing, passing along the Graphics object to draw on and the current size of the picture box.

In the constructor or form load, the form still not have been painted. So put the method on form paint event of element:

protected void pictureBox1_Paint(object sender, PaintEventArgs e)
{
   Graphics papel = e.Graphics();
   Pen lapiz = new Pen(Color.Black);
   //Dibujo de ejes X y Y
   papel.DrawLine(lapiz, 20, 425, 742, 425);
   papel.DrawLine(lapiz, 20, 425, 20, 20);
   papel.DrawString("X", myFont, Brushes.Black, new Point(745, 418));
   papel.DrawString("Y", myFont, Brushes.Black, new Point(14, 5));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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