Windows Phone 8.1 画圆形 画正方形 画直线

network | | 访问(50)
<Grid x:Name="ContentPanel">
    <TextBlock x:Name="PageTitle" Text="画圆形" FontSize="60"  Margin="24,50,0,0" />
</Grid>
<Page.BottomAppBar>
    <CommandBar IsOpen="False">
        <CommandBar.SecondaryCommands>
            <AppBarButton Icon="Copy" Label="画圆形" Click="OnAppbarSelectGraphClick"/>
            <AppBarButton Icon="Cut" Label="画正方形" Click="OnAppbarSelectGraphClick"/>
            <AppBarButton Icon="Paste" Label="画直线" Click="OnAppbarSelectGraphClick"/>
        </CommandBar.SecondaryCommands>
    </CommandBar>
</Page.BottomAppBar>
public sealed partial class MainPage: Page {
    Random rand = new Random();
    bool isDrawing,
    isDragging;
    Path path;
    string graph = "画圆形";
    string draggingGraph = "";
    EllipseGeometry ellipseGeo;
    RectangleGeometry rectangleGeo;
    LineGeometry lineGeo;

    public MainPage() {
        InitializeComponent();
        base.ManipulationMode = ManipulationModes.All;
    }

    protected override void OnManipulationStarted(ManipulationStartedRoutedEventArgs e) {
        if (e.OriginalSource is Path) {
            if ((e.OriginalSource as Path).Data is EllipseGeometry) {
                ellipseGeo = (e.OriginalSource as Path).Data as EllipseGeometry;
                draggingGraph = "圆形";
            } else if ((e.OriginalSource as Path).Data is RectangleGeometry) {
                rectangleGeo = (e.OriginalSource as Path).Data as RectangleGeometry;
                draggingGraph = "正方形";
            } else if ((e.OriginalSource as Path).Data is LineGeometry) {
                lineGeo = (e.OriginalSource as Path).Data as LineGeometry;
                draggingGraph = "线形";
            }
            isDragging = true;
        } else {
            if (graph == "画圆形") {
                ellipseGeo = new EllipseGeometry();
                ellipseGeo.Center = e.Position;
                path = new Path();
                path.Stroke = new SolidColorBrush(Colors.Gold);
                path.Data = ellipseGeo;
            } else if (graph == "画正方形") {
                rectangleGeo = new RectangleGeometry();
                Rect re = new Rect(e.Position, e.Position);
                rectangleGeo.Rect = re;
                path = new Path();
                path.Stroke = new SolidColorBrush(Colors.Gold);
                path.Data = rectangleGeo;
            } else if (graph == "画直线") {
                Color clr = Color.FromArgb(255, (byte) rand.Next(256), (byte) rand.Next(256), (byte) rand.Next(256));
                lineGeo = new LineGeometry();
                lineGeo.StartPoint = e.Position;
                lineGeo.EndPoint = e.Position;
                path = new Path();
                path.Stroke = new SolidColorBrush(clr);
                path.Data = lineGeo;
            }
            path.ManipulationMode = ManipulationModes.All;
            ContentPanel.Children.Add(path);
            isDrawing = true;
        }
        e.Handled = true;
    }

    protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) {
        if (isDragging) {
            if (draggingGraph == "圆形") {
                Point center = ellipseGeo.Center;
                center.X += e.Delta.Translation.X;
                center.Y += e.Delta.Translation.Y;
                ellipseGeo.Center = center;
            } else if (draggingGraph == "正方形") {
                Rect re = rectangleGeo.Rect;
                re.X += e.Delta.Translation.X;
                re.Y += e.Delta.Translation.Y;
                rectangleGeo.Rect = re;
            } else if (draggingGraph == "线形") {
                Point start = lineGeo.StartPoint;
                start.X += e.Delta.Translation.X;
                start.Y += e.Delta.Translation.Y;
                lineGeo.StartPoint = start;

            }
        } else if (isDrawing) {
            Point translation = e.Cumulative.Translation;
            double radius = Math.Max(Math.Abs(translation.X), Math.Abs(translation.Y));
            if (graph == "画圆形") {

                ellipseGeo.RadiusX = radius;
                ellipseGeo.RadiusY = radius;
            } else if (graph == "画正方形") {
                Rect re = rectangleGeo.Rect;
                Rect re2 = new Rect(re.X, re.Y, radius, radius);
                rectangleGeo.Rect = re2;
            } else if (graph == "画直线") {
                Point end = lineGeo.StartPoint;
                end.X += translation.X;
                end.Y += translation.Y;
                lineGeo.EndPoint = end;
            }
        }
        e.Handled = true;
    }

    protected override void OnManipulationCompleted(ManipulationCompletedRoutedEventArgs e) {
        if (isDragging) {
            isDragging = false;
        } else if (isDrawing) {
            Color clr = Color.FromArgb(255, (byte) rand.Next(256), (byte) rand.Next(256), (byte) rand.Next(256));
            path.Fill = new SolidColorBrush(clr);
            isDrawing = false;
        }
        e.Handled = true;
    }

    private void OnAppbarSelectGraphClick(object sender, RoutedEventArgs e) {
        AppBarButton item = sender as AppBarButton;
        graph = item.Label;
        PageTitle.Text = item.Label;
    }

}