15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

WPF ❘ ChaPTer 35 cOre wpf<br />

TransformaTion<br />

Because WPF is vector-based, you can resize every element. The vector-based graphics are now scaled,<br />

rotated, <strong>and</strong> skewed. Hit testing (for example with mouse moves <strong>and</strong> mouse clicks) is still working without<br />

the need for manual position calculation.<br />

Adding the ScaleTransform element to the LayoutTransform property of the Canvas element, as shown,<br />

resizes the content of the complete canvas by 2 in the X <strong>and</strong> Y directions:<br />

<br />

<br />

<br />

code snippet TransformationDemo/MainWindow.xaml<br />

Rotation can be done in a similar way as scaling. Using the RotateTransform element you can define the<br />

Angle for the rotation:<br />

<br />

<br />

<br />

For skewing, you can use the SkewTransform element. With skewing you can assign angles for the X <strong>and</strong> Y<br />

directions:<br />

<br />

<br />

<br />

To rotate <strong>and</strong> skew together, it is possible to define a TransformGroup that contains both<br />

RotateTransform <strong>and</strong> SkewTransform. It is also possible to define a MatrixTransform where the Matrix<br />

element specifies the properties M11 <strong>and</strong> M22 for stretch <strong>and</strong> M12 <strong>and</strong> M21 for skew:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Figure 35-7 shows the results of all the transformations. The figures are placed inside a StackPanel.<br />

Starting from the left side, the first figure is resized, the second figure rotated, the third figure skewed, <strong>and</strong><br />

the fourth figure uses a matrix for its transformation. To see the difference more easily, the Background<br />

property of the Canvas elements are set to different colors.<br />

figure 35-7<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!