Category: C #, VS2015
Date Created: 2016-06-19
Use of teaching materials: (Twelve-Five National planning Materials) "C # Programming and application of the Course" (3rd edition) One, the main points
This example uses the basic knowledge introduced in the 13th chapter of the textbook. Second, the design steps
1. Create a new WPF Application project named MyTest1.
2. Change the MainWindow.xaml to the following content.
<Windowx:class= "Mytest1.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC= "http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local= "Clr-namespace:mytest1"mc:ignorable= "D"Title= "Drawsinwindow"Height= "+"Width= "The "Background= "#FFDCECE5"> <window.resources> <StyleTargetType= "Path"> <Setter Property= "StrokeThickness"Value= "2" /> <Setter Property= "RenderTransform"> <Setter.value> <TransformGroup> <TranslateTransformX= " the"Y= " -110" /> <ScaleTransformScaleY= "-1"ScaleX= "0.7" /> </TransformGroup> </Setter.value> </Setter> </Style> </window.resources> <CanvasName= "CANVAS1"Width= "$"Height= "The "Margin= " the"> <!--Drawing Axes - <PathName= "path1"Stroke= "Red"Data= "m-385,0 l385,0 375 5 m385,0 l375,-5 m0,-100 l0,105-5,95 m0,105 l5,95"> </Path> <!--plotting a sinusoidal curve - <PathName= "path2"Stroke= "Black"/> <PathName= "Path3"Stroke= "Blue"/> </Canvas></Window>
3. Change the MainWindow.xaml.cs to the following content.
usingSystem;usingSystem.Windows;usingSystem.Windows.Media;namespacemytest1{ Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); GeometryGroup group1=NewGeometryGroup (); varG1 = Getsingeometry (1, -); Group1. Children.add (G1); Path2. Data=group1; GeometryGroup group2=NewGeometryGroup (); varG2 = Getsingeometry ( -, -); Group2. Children.add (G2); Path3. Data=group2; } PublicStreamGeometry Getsingeometry (intDxintdy) {StreamGeometry g=NewStreamGeometry (); using(Streamgeometrycontext CTX =G.open ()) { intx0 = the; Doubley0 = Math.sin (-x0 * Math.PI/180.0); CTx. BeginFigure (NewPoint (-x0, dy * y0),false,false); for(intx =-x0; x < x0; X + =dx) {Doubley = Math.sin (x * Math.PI/180.0); CTx. LineTo (NewPoint (x, dy * y),true,true); }} g.freeze (); returnG; } }}
4. Press the <F5> key to debug and run, you will see the following results:
"C #" 3rd Supplement (i) How to draw a sinusoidal curve in WPF