1.使用DrawLine画4条首尾相接的线段
2.是DrawPolygon来画多边形
3.使用Region类来画
4.如果该菱形正好是正方形,画一个正方形,然后旋转
如果是WPF是简单些。
可以先通过把Height绑定到ActualWidth上的方式,用Rectangle画个正方形,然后用RenderTransform转45度,外面加个Grid,再对Grid做ScaleTransform。代码如下:
<Grid RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="2" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Grid.RenderTransform>
<Rectangle Fill="#FFFFFFFF" Stroke="#FF000000" Width="187" Height="{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}" StrokeThickness="4" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="45"/>
<TranslateTransform X="20" Y="0"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
Silverlight没有RelativeSource,可以把Width和Height都Binding到一个Resource上。代码如下:
<UserControl.Resources>
<System:Double x:Key="size">191</System:Double>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" RenderTransformOrigin="0.5,0.5" >
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1.3"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Rectangle VerticalAlignment="Stretch" Width="{StaticResource size}" Fill="#FFFFFFFF" Stroke="#FF000000" StrokeThickness="8" Height="{StaticResource size}" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="45"/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>