我试图在画布中显示几个rects。但不知何故,x / y的反转不能正常工作。
如果检查此图像,蓝色部分应该是两个红色底部的位置。另一个应该始终在中心。如果我通过XAML直接将缩放添加到Canvas,它可以工作,但整个画布旋转45度。
http://i62.tinypic.com/fz9oi0.jpg
还有另一种方法让rect pos以origo开始,350/2(新点(175,175))?
XAML
<Canvas Width="1920" Height="1080" Background="Transparent" x:Name="OverlayWindow">
<Canvas.Children>
<Canvas x:Name="Map" Width="350" Height="350" Background="Black" Canvas.Right="30" Canvas.Top="50">
</Canvas>
</Canvas.Children>
</Canvas>
C#代码背后。
foreach(MapItem Ac in GetMapitems)
{
Dispatcher.Invoke(() =>
{
int x = (int)(Ac.Point().X) - (int)(Player.Point().X);
int y = (int)(Ac.Point().Y) - (int)(Player.Point().Y);
// These values are correct , int x and y.
Rectangle p = new Rectangle
{
Width = 10,
Height = 10,
Fill = new SolidColorBrush(Colors.Red),
};
ScaleTransform b = new ScaleTransform();
b.ScaleX = -1;
var group = new TransformGroup();
group.Children.Add(b);
group.Children.Add(new RotateTransform(-45));
p.RenderTransform = group;
Map.Children.Add(p);
Canvas.SetLeft(p,x);
Canvas.SetTop(p, y);
});
}