The task is to draw a line on the selection of the corresponding button on the toolbar.
In my code, I try to implement the following algorithm : click LMB on the workspace, move the cursor to another place, hold LMB pressed, release LMB - draw a line according to the coordinates of the place where the button was pressed and where it was released.
I declare global variables:
Point EndPosition; Point StartPosition;
Code:
private void inkCanvas1_MouseDown(object sender, MouseButtonEventArgs e) { FrameworkElement fe = sender as FrameworkElement; StartPosition = e.MouseDevice.GetPosition(fe); } private void Line_Click(object sender, RoutedEventArgs e) { linePressed = true; } private void inkCanvas1_MouseUp(object sender, MouseButtonEventArgs e) { FrameworkElement fe = sender as FrameworkElement; EndPosition = e.MouseDevice.GetPosition(fe); if (linePressed) { this.inkCanvas1.EditingMode = InkCanvasEditingMode.Ink; Line l = new System.Windows.Shapes.Line(); l.X1 = StartPosition.X; l.X2 = EndPosition.X; l.Y1 = StartPosition.Y; l.Y2 = EndPosition.Y; l.Stroke = Brushes.Red; l.StrokeThickness = 5; inkCanvas1.Children.Add(l); } } As a result, lines are drawn as follows:
How can I fix this?
PS I tried to display the EndPosition coordinates at the time of drawing in the console, but without success.
fe? - VladDFrameworkElement fe = sender as FrameworkElement;Interestingly, theMouseDownevent never fires. If the author would use the debugger, he would immediately see it - Andrew NOPe.MouseDevice.GetPosition(fe);one.MouseDevice.GetPosition(inkCanvas1);- the same result - Moonlight