//被拖动的控件 private void MyCanvas_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { dragging = true; //标记鼠标按下 mousePoint = e.GetPosition(this.MyCanvas); //获取鼠标在但前canvas内的位置 //if (e.Source.GetType().Name == "NodeBox") //{ // return; //} //mouseCtrl = (Control)e.Source; //获得事件触发的源,即哪个控件 //mouseCtrl = (Canvas)e.Source; //获得事件触发的源,即哪个控件 mouseCtrl = (Canvas)sender; VisualBrush v; v = new VisualBrush(mouseCtrl);//利用VisualBrush得到控件的影像 shadow.Width = mouseCtrl.Width; shadow.Height = mouseCtrl.Height; shadow.Fill = v;//将影像填充给矩形 Canvas.SetLeft(shadow, Canvas.GetLeft(mouseCtrl)); Canvas.SetTop(shadow, Canvas.GetTop(mouseCtrl)); shadow.Visibility = Visibility.Visible; //使矩形可见 //Canvas.SetZIndex(shadow, 0);//可以通过SetZIndex设置阴影的z方向位置 MyCanvas.CaptureMouse(); //强制捕获鼠标。这在对于背景透明的窗体里面是必须的 } }
private void MyCanvas_OnMouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { _startPoint = Mouse.GetPosition(MyCanvas); MyCanvas.CaptureMouse(); _isDragging = true; CreateRectangle(); } }
//左クリック時 private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { IsDrawing = true; //描画フラグ MyCanvas.CaptureMouse(); //マウスがCanvas外に出でも感知できるように //Polyline作成してCanvasに追加表示 MyPolyline = new Polyline(); MyPolyline.Stroke = Brushes.Blue; MyPolyline.StrokeThickness = 20; MyPolyline.StrokeLineJoin = PenLineJoin.Round; MyListPolyline.Add(MyPolyline); MyCanvas.Children.Add(MyPolyline); }
void MyCanvas_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { if (e.Source == MyCanvas) { } else { _isDown = true; _startPoint = e.GetPosition(MyCanvas); _originalElement = e.Source as UIElement; MyCanvas.CaptureMouse(); e.Handled = true; } }
//マウス右クリックしたとき private void MyCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { Point mp = e.GetPosition(MyCanvas); //マウスがCanvas外に出でも感知できるように MyCanvas.CaptureMouse(); //polyline TempPolyline = InitializePolyline(); TempPolyline.Points.Add(mp); MyCanvas.Children.Add(TempPolyline); IsDrawing = true;//描画フラグ }
private void OnLeftDown(object sender, MouseButtonEventArgs e) { if (!MyCanvas.IsMouseCaptured) { Point startPoint = e.GetPosition(MyCanvas); maskDrawer = new MaskDrawer(MyCanvas); maskDrawer.bezierType = beziertype; maskDrawer.fillRule = fillrule; maskDrawer.downsampleRate = downsampleRate; maskDrawer.StartDrawingFreehandPath(startPoint); MyCanvas.CaptureMouse(); } }
private void MyCanvas_MouseMove(object sender, MouseEventArgs e) { if (action.Equals("connect") && (state1 != null) && _isMoving) { if (line != null) { MyCanvas.Children.Remove(line); line = null; } line = new Line(); line.X1 = state1.X + 50; line.Y1 = state1.Y + 25; Console.WriteLine(Mouse.GetPosition(MyCanvas)); Point point = Mouse.GetPosition(MyCanvas); line.X2 = point.X - 5; line.Y2 = point.Y; line.StrokeThickness = 2; line.Stroke = Brushes.Black; MyCanvas.Children.Add(line); MyCanvas.CaptureMouse(); } else if (action.Equals("select") && movingState != null) { MyCanvas.Children.Remove(movingGrid as UIElement); Point mousePos = Mouse.GetPosition(MyCanvas); Console.WriteLine("Position: " + mousePos.ToString()); State state = new State { X = mousePos.X - 25, Y = mousePos.Y - 25, Width = 50, Height = 50, Id = Int32.Parse(movingState.Content.Substring(1)) }; States.Add(state); Ellipse stateel = new Ellipse { Height = 50, Width = 50, Fill = Brushes.LightBlue, Stroke = Brushes.Black }; Label lbl = new Label { Content = "S" + Int32.Parse(movingState.Content.Substring(1)), Height = 50, Width = 50, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; Grid stateGrid = new Grid(); stateGrid.Children.Add(stateel); stateGrid.Children.Add(lbl); stateGrid.Width = 50; stateGrid.Height = 50; stateGrid.Cursor = Cursors.Hand; Canvas.SetLeft(stateGrid, state.X); Canvas.SetTop(stateGrid, state.Y); MyCanvas.Children.Add(stateGrid); movingGrid = stateGrid; MyCanvas.CaptureMouse(); } }