1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Xml;
using System.Windows.Markup;
namespace Interactive2DDrawing
{
public partial class Interactive2DDrawing : Window
{
private List<Path> paths = new List<Path>();
private Point startPoint = new Point();
private Shape rubberBand = null;
Point currentPoint = new Point();
private bool isDragging = false;
private bool isDown = false;
private Path originalElement = new Path();
private Path movingElement = new Path();
private Path path1 = new Path();
private Path path2 = new Path();
private SolidColorBrush fillColor =
new SolidColorBrush();
private SolidColorBrush borderColor =
new SolidColorBrush();
private SolidColorBrush selectFillColor =
new SolidColorBrush();
private SolidColorBrush selectBorderColor =
new SolidColorBrush();
public Interactive2DDrawing()
{
InitializeComponent();
fillColor.Color = Colors.LightGray;
fillColor.Opacity = 0.5;
borderColor.Color = Colors.Gray;
selectFillColor.Color = Colors.LightCoral;
selectFillColor.Opacity = 0.5;
selectBorderColor.Color = Colors.Red;
}
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!canvas1.IsMouseCaptured)
{
startPoint = e.GetPosition(canvas1);
canvas1.CaptureMouse();
if (rbCombine.IsChecked == true)
{
if (canvas1 == e.Source)
return;
SetCombineShapes(e);
}
else if (rbSelect.IsChecked == true)
{
if (canvas1 == e.Source)
return;
isDown = true;
originalElement = (Path)e.Source;
e.Handled = true;
}
else if (rbDelete.IsChecked == true)
{
if (canvas1 == e.Source)
return;
originalElement = (Path)e.Source;
DeleteShape(originalElement);
}
}
}
private void DeleteShape(Path path)
{
path.Stroke = selectBorderColor;
string msg =
"Do you really want to delete this shape?";
string title = "Delete Shape?";
MessageBoxButton buttons = MessageBoxButton.YesNo;
MessageBoxImage icon = MessageBoxImage.Warning;
MessageBoxResult result =
MessageBox.Show(msg, title, buttons, icon);
if (result == MessageBoxResult.Yes)
canvas1.Children.Remove(path);
else
{
path.Stroke = borderColor;
return;
}
}
private void SetCombineShapes(MouseButtonEventArgs e)
{
if (path1.Name != "path1Selected")
{
path1 = (Path)e.Source;
path1.Cursor = Cursors.Hand;
path1.Stroke = selectBorderColor;
path1.Name = "path1Selected";
}
else
{
if (path2 != null)
{
path2.Stroke = borderColor;
path2.Cursor = Cursors.Arrow;
}
path2 = (Path)e.Source;
path2.Cursor = Cursors.Hand;
path2.Stroke = selectBorderColor;
ContextMenu cm = new ContextMenu();
path2.ContextMenu = cm;
MenuItem mi = new MenuItem();
mi.Header = "Union";
mi.Click +=
new RoutedEventHandler(Union_Click);
cm.Items.Add(mi);
mi = new MenuItem();
mi.Header = "Xor";
mi.Click +=
new RoutedEventHandler(Xor_Click);
cm.Items.Add(mi);
mi = new MenuItem();
mi.Header = "Intersect";
mi.Click +=
new RoutedEventHandler(Intersect_Click);
cm.Items.Add(mi);
mi = new MenuItem();
mi.Header = "Exclude";
mi.Click +=
new RoutedEventHandler(Exclude_Click);
cm.Items.Add(mi);
}
}
private void Union_Click(object sender, RoutedEventArgs e)
{
CombineShapes(path1, path2, "Union");
path1.Name = "";
}
private void Xor_Click(object sender,RoutedEventArgs e)
{
CombineShapes(path1, path2, "Xor");
path1.Name = "";
}
private void Intersect_Click(object sender,RoutedEventArgs e)
{
CombineShapes(path1, path2, "Intersect");
path1.Name = "";
}
private void Exclude_Click(object sender,RoutedEventArgs e)
{
CombineShapes(path1, path2, "Exclude");
path1.Name = "";
}
private void CombineShapes(Path p1, Path p2, string s)
{
Path myPath = new Path();
myPath.Fill = fillColor;
myPath.Stroke = borderColor;
CombinedGeometry cg = new CombinedGeometry();
if (s == "Union")
cg.GeometryCombineMode =
GeometryCombineMode.Union;
else if (s == "Xor")
cg.GeometryCombineMode =
GeometryCombineMode.Xor;
else if (s == "Intersect")
cg.GeometryCombineMode =
GeometryCombineMode.Intersect;
else if (s == "Exclude")
cg.GeometryCombineMode =
GeometryCombineMode.Exclude;
cg.Geometry1 = p1.Data;
cg.Geometry2 = p2.Data;
myPath.Data = cg;
paths.Add(myPath);
canvas1.Children.Add(paths[paths.Count - 1]);
canvas1.Children.Remove(p1);
canvas1.Children.Remove(p2);
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (canvas1.IsMouseCaptured)
{
currentPoint = e.GetPosition(canvas1);
if (rubberBand == null)
{
rubberBand = new Rectangle();
rubberBand.Stroke = Brushes.LightCoral;
rubberBand.StrokeDashArray =
new DoubleCollection(new double[] { 4, 2 });
if (rbSquare.IsChecked == true ||
rbRectangle.IsChecked == true ||
rbCircle.IsChecked == true ||
rbEllipse.IsChecked == true)
{
canvas1.Children.Add(rubberBand);
}
}
double width = Math.Abs(
startPoint.X - currentPoint.X);
double height = Math.Abs(
startPoint.Y - currentPoint.Y);
double left = Math.Min(
startPoint.X, currentPoint.X);
double top = Math.Min(
startPoint.Y, currentPoint.Y);
rubberBand.Width = width;
rubberBand.Height = height;
Canvas.SetLeft(rubberBand, left);
Canvas.SetTop(rubberBand, top);
if (rbSelect.IsChecked == true)
{
if (isDown)
{
if (!isDragging && Math.Abs(currentPoint.X - startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance &&
Math.Abs(currentPoint.Y - startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance)
DragStarting();
if (isDragging)
DragMoving();
}
}
}
}
private void OnMouseLeftButtonUp(object sender,MouseButtonEventArgs e)
{
if (rbSquare.IsChecked == true)
AddSquare(startPoint, currentPoint);
else if (rbRectangle.IsChecked == true)
AddRectangle(startPoint, currentPoint);
else if (rbCircle.IsChecked == true)
AddCircle(startPoint, currentPoint);
else if (rbEllipse.IsChecked == true)
AddEllipse(startPoint, currentPoint);
if (rubberBand != null)
{
canvas1.Children.Remove(rubberBand);
rubberBand = null;
canvas1.ReleaseMouseCapture();
}
if (rbSelect.IsChecked == true)
{
if (isDown)
{
DragFinishing(false);
e.Handled = true;
}
}
}
private void AddRectangle(Point pt1, Point pt2)
{
Path path = new Path();
path.Fill = fillColor;
path.Stroke = borderColor;
RectangleGeometry rg = new RectangleGeometry();
double width = Math.Abs(pt1.X - pt2.X);
double height = Math.Abs(pt1.Y - pt2.Y);
double left = Math.Min(pt1.X, pt2.X);
double top = Math.Min(pt1.Y, pt2.Y);
rg.Rect = new Rect(left, top, width, height);
path.Data = rg;
paths.Add(path);
canvas1.Children.Add(paths[paths.Count - 1]);
}
private void AddSquare(Point pt1, Point pt2)
{
Path path = new Path();
path.Fill = fillColor;
path.Stroke = borderColor;
RectangleGeometry rg = new RectangleGeometry();
double width = Math.Abs(pt1.X - pt2.X);
double height = Math.Abs(pt1.Y - pt2.Y);
double left = Math.Min(pt1.X, pt2.X);
double top = Math.Min(pt1.Y, pt2.Y);
double side = width;
if (width > height)
side = height;
rg.Rect = new Rect(left, top, side, side);
path.Data = rg;
paths.Add(path);
canvas1.Children.Add(paths[paths.Count - 1]);
}
private void AddCircle(Point pt1, Point pt2)
{
Path path = new Path();
path.Fill = fillColor;
path.Stroke = borderColor;
EllipseGeometry eg = new EllipseGeometry();
double width = Math.Abs(pt1.X - pt2.X);
double height = Math.Abs(pt1.Y - pt2.Y);
double left = Math.Min(pt1.X, pt2.X);
double top = Math.Min(pt1.Y, pt2.Y);
double side = width;
if (width > height)
side = height;
eg.Center = new Point(left + side / 2,
top + side / 2);
eg.RadiusX = side / 2;
eg.RadiusY = side / 2;
path.Data = eg;
paths.Add(path);
canvas1.Children.Add(paths[paths.Count - 1]);
}
private void AddEllipse(Point pt1, Point pt2)
{
Path path = new Path();
path.Fill = fillColor;
path.Stroke = borderColor;
EllipseGeometry eg = new EllipseGeometry();
double width = Math.Abs(pt1.X - pt2.X);
double height = Math.Abs(pt1.Y - pt2.Y);
double left = Math.Min(pt1.X, pt2.X);
double top = Math.Min(pt1.Y, pt2.Y);
eg.Center = new Point(left + width / 2,
top + height / 2);
eg.RadiusX = width / 2;
eg.RadiusY = height / 2;
path.Data = eg;
paths.Add(path);
canvas1.Children.Add(paths[paths.Count - 1]);
}
private void DragStarting()
{
isDragging = true;
movingElement = new Path();
movingElement.Data = originalElement.Data;
movingElement.Fill = selectFillColor;
movingElement.Stroke = selectBorderColor;
canvas1.Children.Add(movingElement);
}
private void DragMoving()
{
currentPoint = Mouse.GetPosition(canvas1);
TranslateTransform tt = new TranslateTransform();
tt.X = currentPoint.X - startPoint.X;
tt.Y = currentPoint.Y - startPoint.Y;
movingElement.RenderTransform = tt;
}
private void DragFinishing(bool cancel)
{
Mouse.Capture(null);
if(isDragging)
{
if (!cancel)
{
currentPoint = Mouse.GetPosition(canvas1);
TranslateTransform tt0 = new TranslateTransform();
TranslateTransform tt = new TranslateTransform();
tt.X = currentPoint.X - startPoint.X;
tt.Y = currentPoint.Y - startPoint.Y;
Geometry geometry =
(RectangleGeometry)new RectangleGeometry();
string s = originalElement.Data.ToString();
if (s == "System.Windows.Media.EllipseGeometry")
geometry = (EllipseGeometry)originalElement.Data;
else if (s == "System.Windows.Media.RectangleGeometry")
geometry = (RectangleGeometry)originalElement.Data;
else if (s == "System.Windows.Media.CombinedGeometry")
geometry = (CombinedGeometry)originalElement.Data;
if (geometry.Transform.ToString()!= "Identity")
{
tt0 = (TranslateTransform)geometry.Transform;
tt.X += tt0.X;
tt.Y += tt0.Y;
}
geometry.Transform = tt;
canvas1.Children.Remove(originalElement);
originalElement = new Path();
originalElement.Fill = fillColor;
originalElement.Stroke = borderColor;
originalElement.Data = geometry;
canvas1.Children.Add(originalElement);
}
canvas1.Children.Remove(movingElement);
movingElement = null;
}
isDragging = false;
isDown = false;
}
}
} |
Partager