How move ellipse by mouse on window in WPF.
private void ellipse_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
Ellipse ellipse = sender as Ellipse;
if (ellipse != null && e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(ellipse,
ellipse.Fill.ToString(),
System.Windows.DragDropEffects.Copy);
}
}
How to create method ellipse_MouseClick?
There is no MouseClick event on Ellipses, however there is the MouseDown and MouseUp events. I assume you are looking for something like this.
WPF:
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525"
MouseMove="Any_MouseMove"
>
<Canvas>
<Ellipse Fill="Lavender" Height="100" Width="100"
MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"
MouseLeftButtonUp="Ellipse_MouseLeftButtonUp"
MouseMove="Any_MouseMove" />
</Canvas>
</Window>
Code Behind:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication
{
public partial class MainWindow : Window
{
private UIElement _lastClickedUIElement;
private Point? _clickOffset;
public MainWindow() { InitializeComponent(); }
private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_lastClickedUIElement = sender as UIElement;
_clickOffset = e.GetPosition(_lastClickedUIElement);
}
private void Ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_lastClickedUIElement = null;
}
private void Any_MouseMove(object sender, MouseEventArgs e)
{
if (_lastClickedUIElement == null)
return;
_lastClickedUIElement.SetValue(Canvas.LeftProperty, e.GetPosition(this).X - _clickOffset.Value.X);
_lastClickedUIElement.SetValue(Canvas.TopProperty, e.GetPosition(this).Y - _clickOffset.Value.Y);
}
}
}
Click on the circle to move it around. This will work on any UI element as long as long as you give them those methods. feel free to add a Rectangle to the canvas also.
<Rectangle Fill="Lavender" Height="100" Width="100"
MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"
MouseLeftButtonUp="Ellipse_MouseLeftButtonUp"
MouseMove="Any_MouseMove" />
I use three events to accomplish this task:
XAML:
<Window x:Class="WpfPainting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="700" WindowStartupLocation="CenterScreen">
<DockPanel>
<Canvas Background="White" Name="CanvasArea"/>
</DockPanel>
</Window>
Code:
private UIElement source;
private bool captured;
double x_shape, x_canvas, y_shape, y_canvas;
private void ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Mouse.OverrideCursor = Cursors.SizeAll;
source = (UIElement)sender;
Mouse.Capture(source);
captured = true;
x_shape = Canvas.GetLeft(source);
x_canvas = e.GetPosition(CanvasArea).X;
y_shape = Canvas.GetTop(source);
y_canvas = e.GetPosition(CanvasArea).Y;
}
private void ellipse_MouseMove(object sender, MouseEventArgs e)
{
if (captured)
{
double x = e.GetPosition(CanvasArea).X;
double y = e.GetPosition(CanvasArea).Y;
x_shape += x - x_canvas;
Canvas.SetLeft(source, x_shape);
x_canvas = x;
y_shape += y - y_canvas;
Canvas.SetTop(source, y_shape);
y_canvas = y;
}
}
private void ellipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(null);
captured = false;
Mouse.OverrideCursor = null;
}
To use the events on a painted ellipse I refer to them like this:
ellipse.MouseLeftButtonDown += conveyor_MouseLeftButtonDown;
ellipse.MouseMove += conveyor_MouseMove;
ellipse.MouseLeftButtonUp += conveyor_MouseLeftButtonUp;
Related
I am trying to drag a shape inside a scroll viewer.
My solution was to temporarily disable the scroll viewer when touching the shape, then re-enabling when releasing. Feels a bit hacky, but couldn't find a better solution. Has anyone a better recommendation?
The XAML code below.
<Page
x:Class="testdragshape.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:testdragshape"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="390.667">
<Grid>
<ScrollViewer Name="scrollViewer" >
<Grid Name="pageGrid">
<TextBlock Name="log" Text="Position:"/>
<Canvas Name="myCanvas" Width="500" Height="500">
<Rectangle Name="myTarget" Width="272" Height="272" Fill="Red" Canvas.Left="41" Canvas.Top="53" />
</Canvas>
</Grid>
</ScrollViewer>
</Grid>
The C# UWP code below
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
namespace testdragshape
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
myCanvas.RenderTransform = new TranslateTransform();
myTarget.PointerPressed += Target_PointerPressed;
myTarget.PointerMoved += Target_PointerMoved;
myTarget.PointerReleased += MyTarget_PointerReleased;
}
Point _drag0;
private void Target_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// disable the scroll mode
scrollViewer.HorizontalScrollMode = ScrollMode.Disabled;
scrollViewer.VerticalScrollMode = ScrollMode.Disabled;
_drag0 = e.GetCurrentPoint(myCanvas).Position;
log.Text = "Position - X: " + _drag0.X + " Y: " + _drag0.Y;
}
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (!e.Pointer.IsInContact) return;
var point = e.GetCurrentPoint(myCanvas).Position;
var t = new Point(point.X - _drag0.X, point.Y - _drag0.Y);
TranslateTransform previous = (TranslateTransform)myCanvas.RenderTransform;
myCanvas.RenderTransform = new TranslateTransform()
{
X = previous.X + t.X,
Y = previous.Y + t.Y
};
log.Text = "Position - X: " + point.X + " Y: " + point.Y;
}
private void MyTarget_PointerReleased(object sender, PointerRoutedEventArgs e)
{
// re-enable scroll mode
scrollViewer.HorizontalScrollMode = ScrollMode.Auto;
scrollViewer.VerticalScrollMode = ScrollMode.Auto;
}
}
}
You are almost there, except you should disable the direct manipulation of ScrollViewer instead of disabling scrolling, and re-enable it once you are done.
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
scrollViewer.CancelDirectManipulations();
}
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
TryStartDirectManipulation(e.Pointer);
}
I have a small undecorated xaml window for a touch screen. The user must be able to move the window using touch and drag. Currently on touch and drag, the window moves in the direction of the drag, but only part way; and there appears to be two windows rather than one, making the touch and drag appear jumpy.
This behavior manifests on the development system (Surface Pro 3 using Visual Studio Professional 2015) as well as on the production system (Windows 7, no keyboard or mouse).
I based this C# on Microsoft's example.
using System.Windows;
using System.Windows.Input;
namespace XAMLApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private TouchDevice windowTouchDevice;
private Point lastPoint;
private void Circle_TouchUp(object sender, TouchEventArgs e)
{
// Do stuff.
}
private void Window_TouchDown(object sender, TouchEventArgs e)
{
e.TouchDevice.Capture(this);
if (windowTouchDevice == null)
{
windowTouchDevice = e.TouchDevice;
lastPoint = windowTouchDevice.GetTouchPoint(null).Position;
}
e.Handled = true;
}
private void Window_TouchMove(object sender, TouchEventArgs e)
{
if (e.TouchDevice == windowTouchDevice)
{
var currentTouchPoint = windowTouchDevice.GetTouchPoint(null);
var deltaX = currentTouchPoint.Position.X - lastPoint.X;
var deltaY = currentTouchPoint.Position.Y - lastPoint.Y;
Top += deltaY;
Left += deltaX;
lastPoint = currentTouchPoint.Position;
e.Handled = true;
}
}
private void Window_TouchLeave(object sender, TouchEventArgs e)
{
if (e.TouchDevice == windowTouchDevice)
windowTouchDevice = null;
e.Handled = true;
}
}
}
And here's some xaml for the window.
<Window x:Name="AppWindow" x:Class="XAMLApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XAMLApp"
mc:Ignorable="d"
Title="XAML Application"
Height="25" Width="25"
AllowsTransparency="True" WindowStyle="None" ResizeMode="NoResize"
ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ShowInTaskbar="False" ToolTip="XAML Application" Topmost="True" UseLayoutRounding="True"
MaxHeight="25" MaxWidth="25" MinHeight="25" MinWidth="25"
Left="0" Top="0" Background="Transparent"
TouchDown="Window_TouchDown"
TouchMove="Window_TouchMove"
TouchLeave="Window_TouchLeave">
<Grid>
<Ellipse x:Name="Circle" Fill="Black" HorizontalAlignment="Left"
Height="24" Margin="0" Stroke="Black" VerticalAlignment="Top"
Width="24" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"
TouchUp="Circle_TouchUp" />
</Grid>
</Window>
I've tried replacing the Grid with a Canvas. That made no difference. I also tried using Manipulation as demonstrated by Microsoft. When trying to drag the window, I was told that the transformation was invalid for the window.
How can I make the touch and drag behave the same as DragMove() using a left-mouse-click-and-drag?
The TouchPoint retrieved through the TouchDevice.GetTouchPoint method and the window's Top and Left attributes do not share the same coordinate system. All you need to do is convert the X and Y values retrieved to screen coordinates:
private void Window_TouchMove(object sender, TouchEventArgs e)
{
if (e.TouchDevice == windowTouchDevice)
{
var currentTouchPoint = windowTouchDevice.GetTouchPoint(null);
var locationOnScreen = this.PointToScreen(new Point(currentTouchPoint.Position.X, currentTouchPoint.Position.Y));
var deltaX = locationOnScreen.X - lastPoint.X;
var deltaY = locationOnScreen.Y - lastPoint.Y;
Top += deltaY;
Left += deltaX;
lastPoint = locationOnScreen;
e.Handled = true;
}
}
EDIT:
Of course, the thing about different coordinate systems applies to the whole project, so the Window_TouchDown event handler method needs to be adapted in a similar way:
private void Window_TouchDown(object sender, TouchEventArgs e)
{
e.TouchDevice.Capture(this);
if (windowTouchDevice == null)
{
windowTouchDevice = e.TouchDevice;
var currentTouchPoint = windowTouchDevice.GetTouchPoint(null);
var locationOnScreen = this.PointToScreen(new Point(currentTouchPoint.Position.X, currentTouchPoint.Position.Y));
lastPoint = locationOnScreen;
}
e.Handled = true;
}
Instead of
Top += deltaY;
Left += deltaX;
I just needed
Top += currentTouchPoint.Position.Y;
Left += currentTouchPoint.Position.X;
Ugh.
I want to move the label by using the Mouse_Move/Mouse_Down events.
I tried to do it like this:
private void control_MouseDown(object sender, MouseButtonEventArgs e)
{
Label l = e.Source as Label;
if (l != null)
{
l.CaptureMouse();
moving = true;
PositionInLabel = e.GetPosition(l);
}
}
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (moving)
{
Point p = e.GetPosition(null);
DeltaX = p.X - BasePoint.X - PositionInLabel.X;
DeltaY = p.Y - BasePoint.Y - PositionInLabel.Y;
RaisePropertyChanged("XPosition");
RaisePropertyChanged("YPosition");
}
}
Suppose your label is on a Canvas:
public MainWindow()
{
InitializeComponent();
this.label.MouseLeftButtonDown += control_MouseLeftButtonDown;
this.label.MouseMove += control_MouseMove;
this.label.MouseLeftButtonUp += control_MouseLeftButtonUp;
}
// Keep track of the Canvas where this element is placed.
private Canvas canvas;
// Keep track of when the element is being dragged.
private bool isDragging = false;
// When the element is clicked, record the exact position
// where the click is made.
private Point mouseOffset;
private void control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Label l = e.Source as Label;
if (l == null)
return;
// Find the Canvas.
if (canvas == null)
canvas = (Canvas)VisualTreeHelper.GetParent(l);
// Dragging mode begins.
isDragging = true;
// Get the position of the click relative to the element
// (so the top-left corner of the element is (0,0).
mouseOffset = e.GetPosition(l);
// Capture the mouse. This way you'll keep receiving
// the MouseMove event even if the user jerks the mouse
// off the element.
l.CaptureMouse();
}
private void control_MouseMove(object sender, MouseEventArgs e)
{
Label l = e.Source as Label;
if (l == null)
return;
if (isDragging)
{
// Get the position of the element relative to the Canvas.
Point point = e.GetPosition(canvas);
// Move the element.
l.SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y);
l.SetValue(Canvas.LeftProperty, point.X - mouseOffset.X);
}
}
private void control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Label l = e.Source as Label;
if (l == null)
return;
if (isDragging)
{
l.ReleaseMouseCapture();
isDragging = false;
}
}
Let containerCanvas be a Canvas which contains the Label; then you can utilize the _MouseMove(object sender, MouseEventArgs e) to move the label along with your mouse pointer:
Xaml code:
<Canvas Name="containerCanvas" MouseMove="containerCanvas_MouseMove" Background="Aqua" Width="525" Height="350" >
<Label Name="floatingLabel" Height="28" Width="161" Background="AliceBlue"></Label>
</Canvas>
C# code:
private void containerCanvas_MouseMove(object sender, MouseEventArgs e)
{
Point p = Mouse.GetPosition(Application.Current.MainWindow);
floatingLabel.Margin = new Thickness(p.X, p.Y, 0, 0);
}
I have done a sample code. Check this. It should work.
XAML :
<UserControl HorizontalAlignment="Left" x:Class="WPFDiagramDesignerControl.Components.TestApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="100" Width="100" IsEnabled="True">
<Grid >
<Canvas x:Name="MyDesigner">
<TextBox x:Name="txtBox" IsEnabled="True" Background="AntiqueWhite" Margin="10,10,10,10" TextWrapping="Wrap"> </TextBox>
</Canvas>
</Grid>
Code Behind :
public TestApp()
{
InitializeComponent();
txtBox.MouseDoubleClick+=new MouseButtonEventHandler(control_MouseDoubleClick);
txtBox.MouseMove+=new MouseEventHandler(control_MouseMove);
txtBox.PreviewMouseDown+=new MouseButtonEventHandler(control_PreviewMouseDown);
txtBox.PreviewMouseUp+=new MouseButtonEventHandler(control_PreviewMouseUp);
txtBox.Cursor = Cursors.SizeAll;
}
private void control_MouseMove(object sender, RoutedEventArgs e)
{
if (isClicked)
{
Point mousePos = Mouse.GetPosition(parentCanvas);
parentItem = this.Parent as DesignerItem;
parentCanvas = parentItem.Parent as DesignerCanvas;
Point relativePosition = Mouse.GetPosition(parentCanvas);
DesignerCanvas.SetLeft(this, DesignerCanvas.GetLeft(this) - (startPoint.X - mousePos.X));
DesignerCanvas.SetTop(this, DesignerCanvas.GetTop(this) - (startPoint.Y - mousePos.Y));
}
}
private void control_PreviewMouseDown(object sender, RoutedEventArgs e)
{
if (!isClicked)
{
isClicked = true;
parentItem = this.Parent as DesignerItem;
parentCanvas = parentItem.Parent as DesignerCanvas;
startPoint = Mouse.GetPosition(parentCanvas);
}
}
private void control_PreviewMouseUp(object sender, RoutedEventArgs e)
{
isClicked = false;
}
Well, because I have nothing better else to do than getting familiar with C# and WPF I decided to make some simple app with graph with visualisation. So I want to put "nodes" on canvas, and move it, connect by edges e.t.c.
But I don't know how to detect if some "node" is clicked.
I noticed that there's .MouseDown in Ellipse object but don't know how to use it (just if (ellipse.MouseDown) is mot correct) in this situation
xaml:
<Window x:Class="GraphWpf.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="View" Height="400" Width="700" ResizeMode="NoResize">
<Grid>
<Canvas x:Name="canvas" MouseDown="Canvas_MouseDown" Background="#227FF2C5" Margin="0,0,107,0" />
<CheckBox Content="Put nodes" Height="32" HorizontalAlignment="Left" Margin="591,67,0,0" Name="putNodes" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
cs code:
public partial class View : Window
{
public View()
{
InitializeComponent();
}
private Point startPoint;
private Ellipse test;
List<Ellipse> nodesOnCanv = new List<Ellipse>();
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(canvas);
if ((bool)putNodes.IsChecked)
{
test = new Ellipse();
test.Fill = new SolidColorBrush(Colors.Blue);
test.Width = 20;
test.Height = 20;
var x = startPoint.X;
var y = startPoint.Y;
Canvas.SetLeft(test, x);
Canvas.SetTop(test, y);
canvas.Children.Add(test);
nodesOnCanv.Add(test);
}
else {
foreach(Ellipse element in nodesOnCanv){ //such statment is incorrect
if (element.MouseDown()) {
//do something
}
}
}
}
}
You could use IsMouseOver Property
foreach (Ellipse element in nodesOnCanv)
{
if (element.IsMouseOver)
{
element.Fill = Brushes.Red;
}
}
or handle MouseDown for each node
test = new Ellipse();
test.MouseDown += new MouseButtonEventHandler(test_MouseDown);
.
void test_MouseDown(object sender, MouseButtonEventArgs e)
{
(sender as Ellipse).Fill = Brushes.Red;
}
I would prefer the latter.
I have added the following parameters to my Window:
WindowStyle="None"
WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
ResizeMode="NoResize" Background="Transparent"
And now I can't move the Window, so I have added the following part of code to my Window:
#region Window: Moving
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
#endregion
Also I must specify that my XAML code in my Window is the following (the Window look like the Polygon):
<Window Title="New Science"
Height="588" Width="760" MinHeight="360" MinWidth="360"
WindowStyle="None" WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
ResizeMode="NoResize" Background="Transparent"
xmlns:my="clr-namespace:Bourlesque.Lib.Windows.Media;assembly=Bourlesque.Lib.Windows.Media">
<Grid>
<my:UniPolygon DefaultRadiusIn="10" DefaultRadiusOut="10" Fill="#FF92C2F2" Name="m_tPlgOuter" Offset="0" Points=" 0;26;; 10;19;10;; 10;0;; 265;0;20;; 290;20;20;; -60,1;20;3;; -60,1;5;10;; -40,1;5;10;; -40,1;20;2.5;; -35,1;20;2.5;; -35,1;5;10;; -15,1;5;10;; -15,1;20;3;; 0,1;20;; 0,1;0,1;; 0;0,1;; " Stretch="None" Stroke="#FF535353" StrokeThickness="0.1" />
</Grid>
</Window>
I would like to know what should I do to make the Window change it's position on mouse drag and what to add to resize the window with the condition that the controls and other things I will add will resize too(I have found this code to resize and I would like to know if is good here).
I used the event MouseDown:
<Window .....
MouseDown="Window_MouseDown" >
with this code:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Left)
this.DragMove();
}
Found a example:
http://cloudstore.blogspot.com.br/2008/06/moving-wpf-window-with-windowstyle-of.html
Anyway, to move a Window in WinForms I used in a project the following code, can be useful if you are having problems:
private bool clicado = false;
private Point lm = new Point();
void PnMouseDown(object sender, MouseEventArgs e)
{
clicado = true;
this.lm = MousePosition;
}
void PnMouseUp(object sender, MouseEventArgs e)
{
clicado = false;
}
void PnMouseMove(object sender, MouseEventArgs e)
{
if(clicado)
{
this.Left += (MousePosition.X - this.lm.X);
this.Top += (MousePosition.Y - this.lm.Y);
this.lm = MousePosition;
}
}
I've tried another solution and worked (not sure if it is the most correct though)
private void GridOfWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var move = sender as System.Windows.Controls.Grid;
var win = Window.GetWindow(move);
win.DragMove();
}
where GridOfWindow is the name of the Grid
<Grid x:Name="GridOfWindow" MouseLeftButtonDown="GridOfWindow_MouseLeftButtonDown">
good code to the answer, but buggy. it will get your moving out of control.
try my modify:
private bool clicado = false;
private Point lm = new Point();
void PnMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
{
clicado = true;
this.lm = System.Windows.Forms.Control.MousePosition;
this.lm.X = Convert.ToInt16(this.Left) - this.lm.X;
this.lm.Y = Convert.ToInt16(this.Top) - this.lm.Y;
}
void PnMouseUp(object sender, System.Windows.Input.MouseEventArgs e)
{
clicado = false;
}
void PnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (clicado)
{
this.Left = (System.Windows.Forms.Control.MousePosition.X + this.lm.X);
this.Top = (System.Windows.Forms.Control.MousePosition.Y + this.lm.Y);
}
}
it will get your moving stick to your cursor.(///▽///)
#Marcio there is no Windows.Forms in WPF.
I got this version to work (steady) with WPF,
private bool clicked = false;
private Point lmAbs = new Point();
void PnMouseDown(object sender, System.Windows.Input.MouseEventArgs e)
{
clicked = true;
this.lmAbs = e.GetPosition(this);
this.lmAbs.Y = Convert.ToInt16(this.Top) + this.lmAbs.Y;
this.lmAbs.X = Convert.ToInt16(this.Left) + this.lmAbs.X;
}
void PnMouseUp(object sender, System.Windows.Input.MouseEventArgs e)
{
clicked = false;
}
void PnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (clicked)
{
Point MousePosition = e.GetPosition(this);
Point MousePositionAbs = new Point();
MousePositionAbs.X = Convert.ToInt16(this.Left) + MousePosition.X;
MousePositionAbs.Y = Convert.ToInt16(this.Top) + MousePosition.Y;
this.Left = this.Left + (MousePositionAbs.X - this.lmAbs.X);
this.Top = this.Top + (MousePositionAbs.Y - this.lmAbs.Y);
this.lmAbs = MousePositionAbs;
}
}
Kind regards,
Lex
I had to fix above's a bit to work properly... working as charm now! Use this with: MouseLeftButtonDown at your main Window.
private void EnableDrag(object sender, MouseButtonEventArgs e)
{
var move = sender as Window;
if (move != null)
{
Window win = Window.GetWindow(move);
win.DragMove();
}
}