C#-将形状移动到距窗体顶部一半的位置

发布于 2024-08-29 22:13:47 字数 1739 浏览 5 评论 0原文

在这里,我必须使用画线方法创建一个菱形,并使其沿着距离表单顶部一半的路径水平移动。

我创建了一个菱形,它正在水平移动,但我希望它从距离表单顶部一半的位置开始移动。

这是创建钻石的代码,

    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;
    }

我可以使用计时器使其移动,以下是代码,

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

请告诉我如何将钻石移动到距离表格顶部一半的位置?


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;
}

它在 middleForm = Form1.Height / 2diamondMiddleOfTheForm = middleForm - middle 处显示错误

,如果我在实现您所说的过程中做了任何事情,我为我的错误道歉......

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

I created a diamond and it is moving horizontally, but i want it to start moving from a position which is half way from the top of the form.

This is the code to create a 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 using the timer and following is the 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 which is half way from the top of the form?


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 at middleForm = Form1.Height / 2 and diamondMiddleOfTheForm = middleForm - middle

I apologize for my mistake, if I did any in implementing what you said...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-09-05 22:13:47

首先,您需要找到钻石的高度。取菱形中的最高点:0,并添加菱形中的最低点:10

height = 10

然后找到菱形的中间,垂直:

middle = height / 2 code>

然后找到表单的中间:

middleForm = form.Height / 2

然后通过将菱形从表单中间“向上”移动菱形高度的一半来计算菱形的位置:

diamondMiddleOfTheForm = middleForm - middleddle

“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);

you need to find the height of the diamond, first. take the highest point in the diamond: 0, and add the lowest point in the diamond: 10

height = 10

then find the middle of the diamond, vertically:

middle = height / 2

then find the middle of the form:

middleForm = form.Height / 2

then calculate the position of the diamond by moving it "up" from the middle of the form by half the height of the diamond:

diamondMiddleOfTheForm = middleForm - midddle

the "diamondMiddleOfTheForm" variable tells you where to offset your "y" values

        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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文