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
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfDrawing
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
FrameworkElement obj1 = null;
FrameworkElement obj2 = null;
Line ln = null;
bool isDown;
double tolerance = 0.5;
Point pt = new Point();
//un binding
Binding b = null;
//pour test de deplacement
FrameworkElement selectedObj = null;
bool isAdding;
bool isMoving;
private void cnv_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed) //boutton gauche presse?
{
if ((bool)Move.IsChecked)
{
isMoving = true;
}
else
{
isAdding = true;
}
pt = e.GetPosition(this.cnv);
foreach (FrameworkElement item in this.cnv.Children)
{
double left = Canvas.GetLeft(item);
double top = Canvas.GetTop(item);
double dx = pt.X - left;
double dy = pt.Y - top;
if (dx <= item.Width + tolerance && dy <= item.Height + tolerance
&& dx > tolerance && dy > tolerance)
{
obj1 = item;
obj1.Focus();
}
}
if (obj1 != null)
{
isDown = true;
if (isMoving && obj1.GetType() != typeof(Line))
{
selectedObj = obj1;
}
else if (isAdding)
{
ln = new Line();
ln.Stroke = Brushes.Red;
ln.StrokeThickness = 2.0;
//point origine et dest centre par rapport à l'image
ln.X1 = Canvas.GetLeft(obj1) + obj1.ActualWidth / 2;
ln.Y1 = Canvas.GetTop(obj1) + obj1.ActualHeight / 2;
ln.X2 = Canvas.GetLeft(obj1) + obj1.ActualWidth / 2;
ln.Y2 = Canvas.GetTop(obj1) + obj1.ActualHeight / 2;
//ici binding de (x1,y1) => à (left, top) de obj1
b = new Binding("(Canvas.Left)");
b.Source = obj1;
BindingOperations.SetBinding(ln, Line.X1Property, b);
b = new Binding("(Canvas.Top)");
b.Source = obj1;
BindingOperations.SetBinding(ln, Line.Y1Property, b);
//on l'ajoute à canvas =>sinon on le voit pas
//quitte à l'enlever si on ne clique pas bouton droit
this.cnv.Children.Add(ln);
}
}
}
}
private void cnv_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (isDown && e.LeftButton == MouseButtonState.Pressed)//boutton gauche presse?
{
pt = e.GetPosition(this.cnv);
if (isMoving)
{
Canvas.SetLeft(selectedObj, pt.X);
Canvas.SetTop(selectedObj, pt.Y);
}
else if (isAdding)
{
// l'user promene sa souris sur le canvas
ln.X2 = pt.X;
ln.Y2 = pt.Y;
}
}
}
private void cnv_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (isDown && e.RightButton == MouseButtonState.Pressed) //bouton droit presse ?
{
pt = e.GetPosition(this.cnv);
if (isMoving)
{
Canvas.SetLeft(selectedObj, pt.X);
Canvas.SetTop(selectedObj, pt.Y);
//reset important
isDown = false;
pt = new Point();
obj1 = null;
obj2 = null;
b = null;
selectedObj = null;
isMoving = false;
this.Move.IsChecked = isMoving;
}
else if (isAdding)
{
foreach (FrameworkElement item in this.cnv.Children)
{
double left = Canvas.GetLeft(item);
double top = Canvas.GetTop(item);
double dx = pt.X - left;
double dy = pt.Y - top;
if (dx <= item.Width + 5.0 && dy <= item.Height + 5.0
&& dx > 0.5 && dy > 0.5)
{
obj2 = item;
obj2.Focus();
}
}
if (obj2 != null && obj2 != obj1)
{
//=> on met à jour x2 & y2
ln.X2 = Canvas.GetLeft(obj2) + obj2.ActualWidth / 2;
ln.Y2 = Canvas.GetTop(obj2) + obj2.ActualHeight / 2;
//ici binding de (x2,y2) => à (left, top) de obj2
b = new Binding("(Canvas.Left)");
b.Source = obj2;
BindingOperations.SetBinding(ln, Line.X2Property, b);
b = new Binding("(Canvas.Top)");
b.Source = obj2;
BindingOperations.SetBinding(ln, Line.Y2Property, b);
//reset important
isDown = false;
pt = new Point();
obj1 = null;
obj2 = null;
b = null;
selectedObj = null;
isAdding = false;
this.Move.IsChecked = isAdding ;
}
else
{
//=> on remove le line
this.cnv.Children.Remove(ln);
//reset important
isDown = false;
pt = new Point();
obj1 = null;
obj2 = null;
b = null;
selectedObj = null;
isAdding = false;
this.Move.IsChecked = isAdding;
}
}
}
}
}
} |
Partager