Oliver
Oliver

Reputation: 1

C# Forms - How to draw shape over existing panels

enter image description here

Trying to draw a shape over existing panels for a good while, but by now out of ideas. Could somebody help me out, please? It ends up always behind the panels (and pictureBox /the grey one/). I tried 3 different ways, whithout success. this is my code:

namespace DrawingOnFront
{
   public partial class Form1 : Form
    {
        public Form1()
        {
        InitializeComponent();
        }

    private void panel11_MouseClick(object sender, MouseEventArgs e)
    {
        DrawIt(90, 70);
    }

    private void DrawIt(int x, int y)
    {
        Rectangle Circle = new Rectangle(x,y,40,40);
        SolidBrush Red = new SolidBrush(Color.Red);
        Graphics g = this.CreateGraphics();
        g.FillEllipse(Red, Circle);

        /*
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics g = Graphics.FromImage(bmp);
        int width = pictureBox1.Width /4;
        int height = pictureBox1.Height /2;
        int diameter = Math.Min(width, height);
        g.FillEllipse(Red, x, y, width, height);
        pictureBox1.Image = bmp;

        */

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        using (Graphics g = e.Graphics)
        {
            Rectangle Circle = ClientRectangle;
            Circle.Location = new Point(100, 60);                  
            Circle.Size = new Size(40, 40);                       

            using (SolidBrush Green = new SolidBrush(Color.Green))    
            {
                g.FillEllipse(Green, Circle);
            }
        }
    }
 }
}

Sorry for this basic lama question, probably for most of you it is very easy, I am still learning it. Thanks a lot in advance.

Upvotes: 0

Views: 679

Answers (1)

TaW
TaW

Reputation: 54453

My comments above apply. Here is an example of how to draw onto each control and the form separately:

We best have a common drawing routine that we can call from the Paint event of each participating element, in our case a Panel, a PictureBox and the Form.

The trick is for all nested elements to draw the circle shifted by their own location. To do so we pass these things into the drawing routine:

  • a valid Graphics object. We get it from the Paint events.
  • and a reference to the control; we use it to offset the drawing on each control (except the form) with Graphics.TranslateTransform..:

Result:

As you can see it looks as if we painted one circle over all elements but actually we drew three circles, each onto one element..:

enter image description here

private void canvasForm_Paint(object sender, PaintEventArgs e)
{
    draw(sender as Control, e.Graphics);
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
    draw(sender as Control, e.Graphics);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    draw(sender as Control, e.Graphics);
}

private void draw(Control ctl, Graphics g)
{
    Rectangle r = new Rectangle(200, 100, 75, 75);
    if (ctl != canvasForm)  g.TranslateTransform(-ctl.Left, -ctl.Top);
    g.FillEllipse(Brushes.Green, r);
    g.ResetTransform();
}

Note that the same result could be created with three calls, one FillRectangle, one DrawImage and one FillEllipse :-)

Upvotes: 1

Related Questions