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

标签 c# c#-3.0 drawing shapes

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

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

这是创建钻石的代码,

    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 处显示错误

我为我的错误道歉,如果我在执行你所说的时做了任何...

最佳答案

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

高度 = 10

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

中间 = 高度/2

然后找到表格的中间部分:

middleForm = form.Height/2

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

diamondMiddleOfTheForm = middleForm - 中间

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

关于C#- 将形状移动到距窗体顶部一半的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2702743/

相关文章:

c# - 在WinRT MVVM中无法执行按钮单击和组合框选择更改

c# - 如何在 Winform 中通过 Numpad 键控制按钮?

.net-3.5 - 本地化 IEnumerable 的最佳方法是什么?

c# - 如何用 func 委托(delegate)替换 Action 委托(delegate)将值返回给函数

android - 如何在 View 上添加图层?

silverlight - 如何在 Silverlight 中从半径和内角绘制圆弧

objective-c - 在 UIButton 的背景图片上自定义绘图

c# - JavaScript - 从使用 Json.Net 创建的 JSON 对象中提取数据

c#-4.0 - 如何在 C# 中捕获未处理的异常?

c# - 在 ASP.NET Core MVC 的 View 中格式化日期