C # - move a shape to a point that is halfway from the top of the form

Here I have to create a diamond using the drawlines method and make it move horizontally along a path that is halfway from the top of the form.

I created a diamond and moves horizontally, but I want it to start moving from a position that is halfway from the top of the form.

This is the code for creating the diamond,

    private void Form1_Paint(object sender, PaintEventArgs e)
    {

        Graphics g = e.Graphics;
        Point p1 = new Point(5+x, 0);
        Point p2 = new Point(10+x, 5);
        Point p3 = new Point(5+x, 10);
        Point p4 = new Point(0+x, 5);
        Point[] ps = { p1, p2, p3, p4, p1 };
        Pen p_yellow = new Pen(Color.Yellow, 5);
        g.DrawLines(p_yellow, ps);
        this.BackColor = System.Drawing.Color.DarkBlue;
    }

I can make it move with a timer, and then code,

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (x < 500)
            x += 2;
        else
            timer1.Enabled = false;
        this.Invalidate(); 
    }

Please tell me how to bring the diamond to a point that is halfway from the top of the mold?


private void Form1_Paint(object sender, PaintEventArgs e)
{
    int height = 10;
    int middle = height / 2;
    int middleform = Form1.height / 2;
    int diamondMiddleOfTheForm;
    diamondMiddleOfTheForm = middleForm - middle;

    Graphics g = e.Graphics;
    Point p1 = new Point(5 + x, 0 + diamondMiddleOfTheForm);
    Point p2 = new Point(10 + x, 5 + diamondMiddleOfTheForm);
    Point p3 = new Point(5 + x, 10 + diamondMiddleOfTheForm);
    Point p4 = new Point(0 + x, 5 + diamondMiddleOfTheForm);
    Point[] ps = { p1, p2, p3, p4, p1 };
    Pen p_yellow = new Pen(Color.Yellow, 5);
    g.DrawLines(p_yellow, ps);
    this.BackColor = System.Drawing.Color.DarkBlue;
}

It shows an error in middleForm = Form1.Height / 2anddiamondMiddleOfTheForm = middleForm - middle

I apologize for my mistake if I did something to implement what you said ...

+3
1

. : 0 : 10

height = 10

:

middle = height / 2

:

middleForm = form.Height / 2

, "" :

diamondMiddleOfTheForm = middleForm - midddle

"diamondMiddleOfTheForm" , "y"

        Point p1 = new Point(5+x, 0+diamondMiddleOfTheForm);
        Point p2 = new Point(10+x, 5+diamondMiddleOfTheForm);
        Point p3 = new Point(5+x, 10+diamondMiddleOfTheForm);
        Point p4 = new Point(0+x, 5+diamondMiddleOfTheForm);
+2

Source: https://habr.com/ru/post/1742550/


All Articles