public override void OnApplyTemplate() { PART_SBPicker = (FrameworkElement)GetTemplateChild("PART_SBPicker"); PART_SBHost = (FrameworkElement)GetTemplateChild("PART_SBHost"); PART_SBHost.PreviewMouseLeftButtonDown += delegate(object s, MouseButtonEventArgs e) { PART_SBHost.CaptureMouse(); CalculateSB(e); }; PART_SBHost.PreviewMouseLeftButtonUp += (s, e) => PART_SBHost.ReleaseMouseCapture(); PART_SBHost.PreviewMouseMove += (s, e) => CalculateSB(e); PART_HuePicker = (FrameworkElement)GetTemplateChild("PART_HuePicker"); PART_HueHost = (FrameworkElement)GetTemplateChild("PART_HueHost"); PART_HueHost.PreviewMouseLeftButtonDown += delegate(object s, MouseButtonEventArgs e) { PART_HueHost.CaptureMouse(); CalculateHue(e); }; PART_HueHost.PreviewMouseLeftButtonUp += (s, e) => PART_HueHost.ReleaseMouseCapture(); PART_HueHost.PreviewMouseMove += (s, e) => CalculateHue(e); base.OnApplyTemplate(); }
public void ProcessKey(FrameworkElement uie, KeyEventArgs e) { bool Alt = (Keyboard.Modifiers & ModifierKeys.Alt) != 0; bool Control = (Keyboard.Modifiers & ModifierKeys.Control) != 0; bool Shift = (Keyboard.Modifiers & ModifierKeys.Shift) != 0; if (uie.IsMouseCaptured) uie.ReleaseMouseCapture(); //TODO: Might be able to remove this: http://www.ageektrapped.com/blog/using-commands-in-babysmash/ if (Alt && Control && Shift && e.Key == Key.O) { ShowOptionsDialog(); e.Handled = true; return; } string s = e.Key.ToString(); if (s.Length == 2 && s[0] == 'D') s = s[1].ToString(); //HACK: WTF? Numbers start with a "D?" as in D1? AddFigure(uie, s); }
public void SetResize(FrameworkElement _resize) { var start = new Point(); _resize.SetBinding(VisibilityProperty, new Binding { Source = _fe, Path = new PropertyPath("CanScale"), Converter = new BooleanToVisibilityConverter() }); _resize.MouseDown += (e, s) => { start = s.GetPosition(_svi); s.Handled = true; s.MouseDevice.Capture(_resize); }; _resize.MouseMove += (e, s) => { if (s.LeftButton == MouseButtonState.Pressed) { var p = s.GetPosition(_svi); var difx = p.X - start.X; var dify = p.Y - start.Y; var nw = _svi.ActualWidth + difx; if (nw > _fe.SwitchWidth) _svi.Width = nw; _svi.Height = _svi.ActualHeight + dify; start = p; s.Handled = true; } }; _resize.MouseUp += (e, s) => _resize.ReleaseMouseCapture(); _resize.TouchDown += (e, s) => { start = s.GetTouchPoint(_svi).Position; s.Handled = true; s.TouchDevice.Capture(_resize); }; _resize.TouchMove += (e, s) => { var p = s.GetTouchPoint(_svi).Position; var difx = p.X - start.X; var dify = p.Y - start.Y; var nw = _svi.ActualWidth + difx; if (nw > _fe.SwitchWidth) _svi.Width = nw; _svi.Height = _svi.ActualHeight + dify; start = p; s.Handled = true; }; }
internal void OnDragEnd(FrameworkElement child, Point origin, Point position) { if (child == null) return; Dispatcher.Invoke(() => { // Remove empty pages for (int i = pages.Count - 1; i >= 0; i--) { if (pages[i].Count == 0) pages.RemoveAt(i); } // Reset opacity child.Opacity = DefaultOpacity; child.SetValue(ZIndexProperty, TransitionZ); child.ReleaseMouseCapture(); dragging = null; UpdateFluidLayout(true); }); }
/// <summary> /// 添加右键对单元移动 /// </summary> /// <param name="body">移动单元</param> /// <param name="element">移动单元所在的容器</param> void AddMoblieBodyMouseAction(FrameworkElement body, FrameworkElement element) { MouseButtonEventHandler rightButtonDown = null; MouseEventHandler mouseMove = null; MouseButtonEventHandler rightButtonUp = null; Point ptDown = new Point(); Point ptMove = new Point(); Panel parentCanvas = (body.Parent as Panel); rightButtonDown = (s, e) => { body.MouseRightButtonDown -= rightButtonDown; body.MouseRightButtonUp += rightButtonUp; body.MouseMove += mouseMove; ptDown = e.GetPosition(body); body.CaptureMouse(); BringUIElementTop(body); }; mouseMove = (s, e) => { ptMove = e.GetPosition(element); //显示当前单元在容器中的位置 Point currentPoint = new Point(ptMove.X - ptDown.X, ptMove.Y - ptDown.Y); Canvas.SetLeft(body, currentPoint.X); Canvas.SetTop(body, currentPoint.Y); //Point releativePoint = parentCanvas.TranslatePoint(currentPoint, BackgroundCanvasA); //debugText.Text = string.Format("({0},{1})", Math.Round(releativePoint.X), Math.Round(releativePoint.Y)); }; rightButtonUp = (s, e) => { body.MouseRightButtonDown += rightButtonDown; body.MouseRightButtonUp -= rightButtonUp; body.MouseMove -= mouseMove; body.ReleaseMouseCapture(); }; body.MouseRightButtonDown += rightButtonDown; }
private void RegisterBorderEvents(WindowBorderEdge borderEdge, FrameworkElement border) { border.MouseEnter += (sender, e) => { if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize) { switch (borderEdge) { case WindowBorderEdge.Left: case WindowBorderEdge.Right: border.Cursor = Cursors.SizeWE; break; case WindowBorderEdge.Top: case WindowBorderEdge.Bottom: border.Cursor = Cursors.SizeNS; break; case WindowBorderEdge.TopLeft: case WindowBorderEdge.BottomRight: border.Cursor = Cursors.SizeNWSE; break; case WindowBorderEdge.TopRight: case WindowBorderEdge.BottomLeft: border.Cursor = Cursors.SizeNESW; break; } } else { border.Cursor = Cursors.Arrow; } }; border.MouseLeftButtonDown += (sender, e) => { if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize) { Point cursorLocation = e.GetPosition(this); Point cursorOffset = new Point(); switch (borderEdge) { case WindowBorderEdge.Left: cursorOffset.X = cursorLocation.X; break; case WindowBorderEdge.TopLeft: cursorOffset.X = cursorLocation.X; cursorOffset.Y = cursorLocation.Y; break; case WindowBorderEdge.Top: cursorOffset.Y = cursorLocation.Y; break; case WindowBorderEdge.TopRight: cursorOffset.X = (Width - cursorLocation.X); cursorOffset.Y = cursorLocation.Y; break; case WindowBorderEdge.Right: cursorOffset.X = (Width - cursorLocation.X); break; case WindowBorderEdge.BottomRight: cursorOffset.X = (Width - cursorLocation.X); cursorOffset.Y = (Height - cursorLocation.Y); break; case WindowBorderEdge.Bottom: cursorOffset.Y = (Height - cursorLocation.Y); break; case WindowBorderEdge.BottomLeft: cursorOffset.X = cursorLocation.X; cursorOffset.Y = (Height - cursorLocation.Y); break; } this.cursorOffset = cursorOffset; border.CaptureMouse(); } }; border.MouseMove += (sender, e) => { if (WindowState != WindowState.Maximized && border.IsMouseCaptured && ResizeMode == ResizeMode.CanResize) { Point cursorLocation = e.GetPosition(this); double nHorizontalChange = (cursorLocation.X - cursorOffset.X); double pHorizontalChange = (cursorLocation.X + cursorOffset.X); double nVerticalChange = (cursorLocation.Y - cursorOffset.Y); double pVerticalChange = (cursorLocation.Y + cursorOffset.Y); switch (borderEdge) { case WindowBorderEdge.Left: if (Width - nHorizontalChange <= MinWidth) break; Left += nHorizontalChange; Width -= nHorizontalChange; break; case WindowBorderEdge.TopLeft: if (Width - nHorizontalChange <= MinWidth) break; Left += nHorizontalChange; Width -= nHorizontalChange; if (Height - nVerticalChange <= MinHeight) break; Top += nVerticalChange; Height -= nVerticalChange; break; case WindowBorderEdge.Top: if (Height - nVerticalChange <= MinHeight) break; Top += nVerticalChange; Height -= nVerticalChange; break; case WindowBorderEdge.TopRight: if (pHorizontalChange <= MinWidth) break; Width = pHorizontalChange; if (Height - nVerticalChange <= MinHeight) break; Top += nVerticalChange; Height -= nVerticalChange; break; case WindowBorderEdge.Right: if (pHorizontalChange <= MinWidth) break; Width = pHorizontalChange; break; case WindowBorderEdge.BottomRight: if (pHorizontalChange <= MinWidth) break; Width = pHorizontalChange; if (pVerticalChange <= MinHeight) break; Height = pVerticalChange; break; case WindowBorderEdge.Bottom: if (pVerticalChange <= MinHeight) break; Height = pVerticalChange; break; case WindowBorderEdge.BottomLeft: if (Width - nHorizontalChange <= MinWidth) break; Left += nHorizontalChange; Width -= nHorizontalChange; if (pVerticalChange <= MinHeight) break; Height = pVerticalChange; break; } } }; border.MouseLeftButtonUp += (sender, e) => { border.ReleaseMouseCapture(); }; }
/// <summary> /// The actual method to make the FrameworkElement Draggable /// </summary> /// <param name="element">The element who.</param> /// <param name="elementParent">The element parent.</param> /// <param name="status">The status.</param> private static void MakeDraggable( FrameworkElement element, Panel elementParent, TextBlock status) { if (IsDraggableType(element)) { if (IsDraggableType(element)) { // If this element already has a DragInfo associated with it then // enable the IsDraggable and return. var di = (((element as FrameworkElement).Parent as Canvas).Tag as DragInfo); di.IsDraggable = true; return; } } var canvasWrapper = new Canvas { Background = new SolidColorBrush(Colors.Transparent), }; elementParent.Children.Remove(element); canvasWrapper.Tag = new DragInfo(); // Keep a DragInfo instance in FrameworkElement.Tag for further reference canvasWrapper.Children.Add(element); elementParent.Children.Add(canvasWrapper); // Attach to the MouseLeftButtonDown event. element.MouseLeftButtonDown += (o, e) => { var dragInfo = canvasWrapper.Tag as DragInfo; if (dragInfo != null) { // Check if we can drag if (dragInfo.IsDraggable) { // Mark that we're doing a drag dragInfo.IsDragging = true; // Ensure that the mouse can't leave element being dragged element.CaptureMouse(); // Determine where the mouse 'grabbed' // to use during MouseMove dragInfo.Offset = e.GetPosition(element); } } }; // Attach to the MouseLeftButtonUp event. element.MouseLeftButtonUp += (o, e) => { var dragInfo = canvasWrapper.Tag as DragInfo; if (dragInfo != null) { // Check if we can drag if (dragInfo.IsDraggable) { if (dragInfo.IsDragging) { // Turn off Dragging dragInfo.IsDragging = false; // Free the Mouse element.ReleaseMouseCapture(); } } } }; // Attach to the MouseMove event. element.MouseMove += (o, e) => { var dragInfo = canvasWrapper.Tag as DragInfo; if (dragInfo != null) { // Check if we can drag if (dragInfo.IsDraggable) { if (dragInfo.IsDragging) { // Where is the mouse now? Point newPosition = e.GetPosition(canvasWrapper); if (status != null) { status.Text = string.Format( "Position: {0},{1} and {2},{3}", newPosition.X, newPosition.Y, dragInfo.Offset.X, dragInfo.Offset.Y); } // Move the element via the new position less the Offset element.SetValue( Canvas.LeftProperty, newPosition.X - dragInfo.Offset.X); element.SetValue( Canvas.TopProperty, newPosition.Y - dragInfo.Offset.Y); } } } }; }
public void ProcessKey(FrameworkElement uie, KeyEventArgs e) { bool Alt = (Keyboard.Modifiers & ModifierKeys.Alt) != 0; bool Control = (Keyboard.Modifiers & ModifierKeys.Control) != 0; bool Shift = (Keyboard.Modifiers & ModifierKeys.Shift) != 0; if (uie.IsMouseCaptured) { uie.ReleaseMouseCapture(); } //TODO: Might be able to remove this: http://www.ageektrapped.com/blog/using-commands-in-babysmash/ if (Alt && Control && Shift && e.Key == Key.O) { ShowOptionsDialog(); e.Handled = true; return; } char displayChar = GetDisplayChar(e.Key); AddFigure(uie, displayChar); }
private void WireUIElementForDragAndDrop(FrameworkElement target, Func<Rect, bool> isInBounds) { Point origin = default(Point); var mouseDown = Observable.FromEventPattern<MouseButtonEventArgs>(target, "MouseDown"); mouseDown.Subscribe(p => { target.CaptureMouse(); origin = p.EventArgs.GetPosition(target); }); var mouseUp = Observable.FromEventPattern<MouseButtonEventArgs>(target, "MouseUp") .Publish(); var mouseLeave = Observable.FromEventPattern<MouseEventArgs>(target, "MouseLeave") .Publish(); //hot observables mouseUp.Connect(); mouseLeave.Connect(); var mouseMove = Observable.FromEventPattern<MouseEventArgs>(target, "MouseMove"); //var mouseHitTest = Observable.Create(mouseMove => // { // return Task.Factory.StartNew<Action>(p => { return true; }); // }); var mouseUpOrLeave = Observable.CombineLatest(mouseUp, mouseLeave, (a, b) => { return true; }); mouseUpOrLeave .Do(_ => Console.WriteLine("MouseUpOrLeave fired {0}", Guid.NewGuid())) .Subscribe(p => { target.ReleaseMouseCapture(); }); mouseMove .Select(x => x.EventArgs.GetPosition(target)) .SkipUntil(mouseDown).Repeat() .TakeUntil(mouseUpOrLeave).Repeat() .Subscribe(p=> { var dX = p.X - origin.X; var dY = p.Y - origin.Y; var moveTo = new Point(Canvas.GetLeft(target)+ dX, Canvas.GetTop(target)+ dY); if (isInBounds(new Rect(moveTo, new Point(moveTo.X + target.ActualWidth, moveTo.Y + target.ActualHeight)))) { Canvas.SetTop(target, moveTo.Y); Canvas.SetLeft(target, moveTo.X); } }); }
private void MouseLeftButtonUpMethod(FrameworkElement element) { element.ReleaseMouseCapture(); isMouseCaptured = false; double scale = 1; if (element.IsActivity()) { scale = ((ScaleResizingAdorner)elements[element]).Scale; } OnWidgetDragging(element, new WidgetDraggingEventArgs(new Point( Canvas.GetLeft(element) + element.ActualWidth * scale, Canvas.GetTop(element) + element.ActualHeight * scale))); }
private void RegisterBorderEvents(DirectionOfResize DirectionOfResize, FrameworkElement border) { border.MouseLeftButtonDown += (sender, e) => { Point cursorLocation = e.GetPosition(window); Point cursorOffset = new Point(); switch (DirectionOfResize) { case DirectionOfResize.Left: cursorOffset.X = cursorLocation.X; break; case DirectionOfResize.TopLeft: cursorOffset.X = cursorLocation.X; cursorOffset.Y = cursorLocation.Y; break; case DirectionOfResize.Top: cursorOffset.Y = cursorLocation.Y; break; case DirectionOfResize.TopRight: cursorOffset.X = (window.Width - cursorLocation.X); cursorOffset.Y = cursorLocation.Y; break; case DirectionOfResize.Right: cursorOffset.X = (window.Width - cursorLocation.X); break; case DirectionOfResize.BottomRight: cursorOffset.X = (window.Width - cursorLocation.X); cursorOffset.Y = (window.Height - cursorLocation.Y); break; case DirectionOfResize.Bottom: cursorOffset.Y = (window.Height - cursorLocation.Y); break; case DirectionOfResize.BottomLeft: cursorOffset.X = cursorLocation.X; cursorOffset.Y = (window.Height - cursorLocation.Y); break; } this.cursorOffset = cursorOffset; border.CaptureMouse(); }; border.MouseMove += (sender, e) => { if (border.IsMouseCaptured) { window.SizeToContent = SizeToContent.Manual; Point cursorLocation = e.GetPosition(window); double nHorizontalChange = (cursorLocation.X - cursorOffset.X); double pHorizontalChange = (cursorLocation.X + cursorOffset.X); double nVerticalChange = (cursorLocation.Y - cursorOffset.Y); double pVerticalChange = (cursorLocation.Y + cursorOffset.Y); switch (DirectionOfResize) { case DirectionOfResize.Left: if (window.Width - nHorizontalChange <= window.MinWidth) break; window.Left += nHorizontalChange; window.Width -= nHorizontalChange; break; case DirectionOfResize.TopLeft: if (window.Width - nHorizontalChange <= window.MinWidth) break; window.Left += nHorizontalChange; window.Width -= nHorizontalChange; if (window.Height - nVerticalChange <= window.MinHeight) break; window.Top += nVerticalChange; window.Height -= nVerticalChange; break; case DirectionOfResize.Top: if (window.Height - nVerticalChange <= window.MinHeight) break; window.Top += nVerticalChange; window.Height -= nVerticalChange; break; case DirectionOfResize.TopRight: if (pHorizontalChange <= window.MinWidth) break; window.Width = pHorizontalChange; if (window.Height - nVerticalChange <= window.MinHeight) break; window.Top += nVerticalChange; window.Height -= nVerticalChange; break; case DirectionOfResize.Right: if (pHorizontalChange <= window.MinWidth) break; window.Width = pHorizontalChange; break; case DirectionOfResize.BottomRight: if (pHorizontalChange <= window.MinWidth) break; window.Width = pHorizontalChange; if (pVerticalChange <= window.MinHeight) break; window.Height = pVerticalChange; break; case DirectionOfResize.Bottom: if (pVerticalChange <= window.MinHeight) break; window.Height = pVerticalChange; break; case DirectionOfResize.BottomLeft: if (window.Width - nHorizontalChange <= window.MinWidth) break; window.Left += nHorizontalChange; window.Width -= nHorizontalChange; if (pVerticalChange <= window.MinHeight) break; window.Height = pVerticalChange; break; } } }; border.MouseLeftButtonUp += (sender, e) => { border.ReleaseMouseCapture(); }; }
public TouchListener(FrameworkElement element) { _multitouchtuio = new MIG.Client.Devices.MultiTouch.MultitouchTuio(); _propagateinput = true; _targetelement = element; (_targetelement as Panel).Children.Add(_infolayer); _cursors = new List<MIG.Client.Devices.MultiTouch.Cursor>(); _targetelement.Dispatcher.BeginInvoke(() => { _targetsize = new Size(_targetelement.ActualWidth, _targetelement.ActualHeight); // // Standard Mouse Input mapped to touch // _targetelement.MouseLeftButtonDown += new MouseButtonEventHandler(delegate(object o, MouseButtonEventArgs e) { _targetelement.CaptureMouse(); MouseCursorAdd(e); }); _targetelement.MouseLeftButtonUp += new MouseButtonEventHandler(delegate { MouseCursorDel(); _targetelement.ReleaseMouseCapture(); }); _targetelement.MouseMove += new MouseEventHandler(delegate(object o, MouseEventArgs e) { MouseCursorSet(e); }); // // Multitouch TUIO input // _multitouchtuio.FingerDown += new MIG.Client.Devices.MultiTouch.MultitouchTuio.FingerDownHandler(MultiTouchListener_FingerDown); _multitouchtuio.FingerUp += new MIG.Client.Devices.MultiTouch.MultitouchTuio.FingerUpHandler(MultiTouchListener_FingerUp); _multitouchtuio.FingerMove += new MIG.Client.Devices.MultiTouch.MultitouchTuio.FingerMoveHandler(MultiTouchListener_FingerMove); _multitouchtuio.AccelerationUpdate += new MIG.Client.Devices.MultiTouch.MultitouchTuio.AccelerationUpdateHandler(MultiTouchListener_AccelerationUpdate); // // Windows 7 WM_TOUCH events fw // Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported); }); }