C#-将形状移动到距窗体顶部一半的位置
在这里,我必须使用画线方法创建一个菱形,并使其沿着距离表单顶部一半的路径水平移动。
我创建了一个菱形,它正在水平移动,但我希望它从距离表单顶部一半的位置开始移动。
这是创建钻石的代码,
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 / 2
和 diamondMiddleOfTheForm = 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要找到钻石的高度。取菱形中的最高点:0,并添加菱形中的最低点:10
height = 10
然后找到菱形的中间,垂直:
middle = height / 2
code>然后找到表单的中间:
middleForm = form.Height / 2
然后通过将菱形从表单中间“向上”移动菱形高度的一半来计算菱形的位置:
diamondMiddleOfTheForm = middleForm - middleddle
“diamondMiddleOfTheForm”变量告诉您在哪里偏移“y”值
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