如何用Rectangle画一个菱型?

 

如果用Rectangle画一个图中菱型(不能用poly)只能用Rectangle


千万里不及你
浏览 754回答 2
2回答

慕丝7291255

1.使用DrawLine画4条首尾相接的线段2.是DrawPolygon来画多边形3.使用Region类来画4.如果该菱形正好是正方形,画一个正方形,然后旋转

GCT1015

如果是WPF是简单些。可以先通过把Height绑定到ActualWidth上的方式,用Rectangle画个正方形,然后用RenderTransform转45度,外面加个Grid,再对Grid做ScaleTransform。代码如下:&nbsp;&nbsp;<Grid RenderTransformOrigin="0.5,0.5">&nbsp;&nbsp;&nbsp;<Grid.RenderTransform>&nbsp;&nbsp;&nbsp;&nbsp;<TransformGroup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ScaleTransform ScaleX="2" ScaleY="1"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SkewTransform AngleX="0" AngleY="0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<RotateTransform Angle="0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TranslateTransform X="0" Y="0"/>&nbsp;&nbsp;&nbsp;&nbsp;</TransformGroup>&nbsp;&nbsp;&nbsp;</Grid.RenderTransform>&nbsp;&nbsp;&nbsp;<Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" Width="187" Height="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}" StrokeThickness="4" RenderTransformOrigin="0.5,0.5">&nbsp;&nbsp;&nbsp;&nbsp;<Rectangle.RenderTransform>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TransformGroup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ScaleTransform ScaleX="1" ScaleY="1"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SkewTransform AngleX="0" AngleY="0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<RotateTransform Angle="45"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TranslateTransform X="20" Y="0"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TransformGroup>&nbsp;&nbsp;&nbsp;&nbsp;</Rectangle.RenderTransform>&nbsp;&nbsp;&nbsp;</Rectangle>&nbsp;&nbsp;</Grid>&nbsp;Silverlight没有RelativeSource,可以把Width和Height都Binding到一个Resource上。代码如下:&nbsp;&nbsp;<UserControl.Resources>&nbsp;&nbsp;<System:Double x:Key="size">191</System:Double>&nbsp;</UserControl.Resources>&nbsp;<Grid x:Name="LayoutRoot" Background="White" RenderTransformOrigin="0.5,0.5" >&nbsp;&nbsp;<Grid.RenderTransform>&nbsp;&nbsp;&nbsp;<TransformGroup>&nbsp;&nbsp;&nbsp;&nbsp;<ScaleTransform ScaleX="1.3"/>&nbsp;&nbsp;&nbsp;&nbsp;<SkewTransform/>&nbsp;&nbsp;&nbsp;&nbsp;<RotateTransform/>&nbsp;&nbsp;&nbsp;&nbsp;<TranslateTransform/>&nbsp;&nbsp;&nbsp;</TransformGroup>&nbsp;&nbsp;</Grid.RenderTransform>&nbsp;&nbsp;<Rectangle VerticalAlignment="Stretch" Width="{StaticResource size}" Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="8" Height="{StaticResource size}" RenderTransformOrigin="0.5,0.5">&nbsp;&nbsp;&nbsp;<Rectangle.RenderTransform>&nbsp;&nbsp;&nbsp;&nbsp;<TransformGroup>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<ScaleTransform/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SkewTransform/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<RotateTransform Angle="45"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TranslateTransform/>&nbsp;&nbsp;&nbsp;&nbsp;</TransformGroup>&nbsp;&nbsp;&nbsp;</Rectangle.RenderTransform>&nbsp;&nbsp;</Rectangle>&nbsp;</Grid>
打开App,查看更多内容
随时随地看视频慕课网APP