用中心点绘制一个矩形. [英] Draw a rectangle with Center point.

查看:237
本文介绍了用中心点绘制一个矩形.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在WPF中,我想从代码隐藏中绘制一个矩形.
在这里我想给出中心点,即在画布上的位置,代码中的高度和宽度,然后绘制一个矩形.

例如:
height = 10,width = 20,point(60,40)在画布上的位置.
现在当我绘制矩形时,矩形的高度,宽度应为10、20,而point(60,40)应为矩形的中心点.

Hello,

In wpf i want to draw a rectangle from codebehind.
Here i want to give the center point i.e location on the canvas , height and width in code and then draw a rectangle.

ex:
height=10,width=20,point(60,40) location on the canvas.
Now when i draw a rectangle, rectangle height, width should be 10, 20 and point(60,40) should be the centre point of the rectangle.

Rectangle myrect=new Rectangle();
myrect.Height=10;
myrect.Width=20;
// How to give center point(60,40) to the Rectangle.

找不到通过指定中心点绘制矩形的任何示例.

我希望有人能提出一个很好的例子来理解.

谢谢,

I have not find any examples to draw a rectangle by specifying the centre point.

I hope some one will come up with a good example to understand.

Thanks,

推荐答案

从中心点减去矩形的一半宽度和高度.
例如
Subract half of your rectangles width and height from your center point.
E.g.
Point centerPoint = new Point(60,40);

Rectangle myRect = new Rectangle();
myRect.Height=10;
myRect.Width=20;

Canvas.SetLeft(myRect, centerPoint.X - myRect.Width/2);
Canvas.SetTop(myRect, centerPoint.Y - myRect.Height/2)

Canvas.Children.Add(myRect);


这篇关于用中心点绘制一个矩形.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆