/// <summary> /// Добавление фигуры на поле Canvas в месте клика мыши /// </summary> private void paint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Point clickpoint = e.GetPosition((Canvas)sender); Shape s = new Shape { ShapePositionX = clickpoint.X, ShapePositionY = clickpoint.Y, StrokeThickness = shape.StrokeThickness, OuterRadius = shape.OuterRadius, InnerRadius = shape.InnerRadius, RColor = shape.RColor, GColor = shape.GColor, BColor = shape.BColor, AColor = shape.AColor, SRColor = shape.SRColor, SGColor = shape.SGColor, SBColor = shape.SBColor, SAColor = shape.SAColor }; s.InitShape(paint); shapes.Add(s); }
/// <summary> /// Открытие файла и добавление элементов на поле /// </summary> private void Open_Executed(object sender, ExecutedRoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Файлы (xml)|*.xml|Все файлы|*.*"; var result = ofd.ShowDialog(); if (result == true) { try { shapes.Clear(); paint.Children.Clear(); XDocument doc = XDocument.Load(ofd.FileName); // Выделить root var root = doc.Root; // Обойти коллекцию элементов внутри root foreach (var element in root.Elements()) { var shape = new Shape(); shape.ShapePositionX = int.Parse(element.Attribute("posX").Value); shape.ShapePositionY = int.Parse(element.Element("posY").Value); shape.StrokeThickness = int.Parse(element.Element("stroke").Value); shape.InnerRadius = int.Parse(element.Element("innerradius").Value); shape.OuterRadius = int.Parse(element.Element("outerradius").Value); shape.RColor = byte.Parse(element.Element("rcolor").Value); shape.GColor = byte.Parse(element.Element("gcolor").Value); shape.BColor = byte.Parse(element.Element("bcolor").Value); shape.AColor = byte.Parse(element.Element("acolor").Value); shape.SRColor = byte.Parse(element.Element("srcolor").Value); shape.SGColor = byte.Parse(element.Element("sgcolor").Value); shape.SBColor = byte.Parse(element.Element("sbcolor").Value); shape.SAColor = byte.Parse(element.Element("sacolor").Value); // Сохранить объект Car shape.InitShape(paint); shapes.Add(shape); } this.Title = "Фигуры - " + ofd.FileName; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/// <summary> /// Открытие файла и добавление элементов на поле /// </summary> private void Open_Executed(object sender, ExecutedRoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Файлы (dat)|*.dat|Все файлы|*.*"; var result = ofd.ShowDialog(); if (result == true) { try { // создаем объект BinaryReader using (BinaryReader reader = new BinaryReader(File.Open(ofd.FileName, FileMode.Open))) { shapes.Clear(); paint.Children.Clear(); // пока не достигнут конец файла // считываем каждое значение из файла while (reader.PeekChar() > -1) { double ShapePosX = reader.ReadDouble(); double ShapePosY = reader.ReadDouble(); int StrokeThickness = reader.ReadInt32(); double OuterRadius = reader.ReadDouble(); double InnerRadius = reader.ReadDouble(); byte BR = reader.ReadByte(); byte BG = reader.ReadByte(); byte BB = reader.ReadByte(); byte BA = reader.ReadByte(); byte SR = reader.ReadByte(); byte SG = reader.ReadByte(); byte SB = reader.ReadByte(); byte SA = reader.ReadByte(); Shape s = new Shape { ShapePositionX = ShapePosX, ShapePositionY = ShapePosY, StrokeThickness = StrokeThickness, OuterRadius = OuterRadius, InnerRadius = InnerRadius, RColor = BR, GColor = BG, BColor = BB, AColor = BA, SRColor = SR, SGColor = SG, SBColor = SB, SAColor = SA }; s.InitShape(paint); shapes.Add(s); } } this.Title = "Фигуры - " + ofd.FileName; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }