Touch manipulation move on initial offset issue - c#

I'm using a ManipulationDelta event to drag a Canvas as follows:
private Point lastMovePosition;
private void MoveCanvas(ManipulationDeltaEventArgs e)
{
var position = e.ManipulationOrigin;
if (CanvasShareSwarm.Scale > 1) //Force zoom out limit to view all
{
CanvasShareSwarm.Offset -= position - lastMovePosition;
lastMovePosition = position;
}
}
This works but when the drag starts the Canvas always jumps back to what seems to be a previous position before moving. I suspect it has to do with my lastMovePosition point.
What could cause this?

I guess you would have to initialize the lastMovePosition field in a ManipulationStarted event handler:
private void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
lastMovePosition = e.ManipulationOrigin;
}

Related

How can I get a relative position to my Canvas when the mouse is outside of the application Window in WPF

public MainWindow()
{
InitializeComponent();
_hook = Hook.GlobalEvents();
_hook.MouseMove += DrawMouseMove;
}
private void DrawMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point GetMousePos() => Mouse.GetPosition(DrawCanvas);
Point pos = GetMousePos();
CoordinateLabel.Content = $"{Math.Ceiling(pos.X)}, {Math.Ceiling(pos.Y)}px";
//... code down here is draw a shape with pos, a relative position to DrawCanvas
}
I'm using hook for global event, but i stuck at getting relative position to DrawCanvas when the mouse is outside of my application window. The thing I want to achive is just like mspaint, you can still draw shape even when the mouse moved out of the window

Brush print is not consecutive

I'm trying to implement the Brushing function to my Paint project.
My idea is everywhen I move and press mouse left button on canvas, I'll add a ellipse to Free-brush (like MSPaint)
Everything was good until I move mouse faster. >> the brush print is separated.
Can anyone explain to me and give me some hints to solve this?
Here is my code:
Point _startPoint, _endPoint;
private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(MyCanvas);
Ellipse MyBrush_Ellipse = new Ellipse();
MyBrushing.CreateBrush(_cl1, _cl2, ref MyBrush_Ellipse, Mybrush_type);
Canvas.SetTop(MyBrush_Ellipse, _startPoint.Y);
Canvas.SetLeft(MyBrush_Ellipse, _startPoint.X);
}
private void MyCanvas_MouseMove(object sender, MouseEventArgs e)
{
_endPoint = e.GetPosition(MyCanvas);
if (e.LeftButton == MouseButtonState.Pressed)
{
Ellipse MyBrush_Ellipse = new Ellipse();
MyBrushing.CreateBrush(_cl1, _cl2, ref MyBrush_Ellipse, Mybrush_type);
Canvas.SetTop(MyBrush_Ellipse, _endPoint.Y);
Canvas.SetLeft(MyBrush_Ellipse, _endPoint.X);
MyCanvas.Children.Add(MyBrush_Ellipse);
MyCanvas.CaptureMouse();
_myUndoRedo.PushToStackForBrush(MyBrush_Ellipse);
}
}
private void MyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MyCanvas.ReleaseMouseCapture();
}
The mouse is not moving constantly over the screen. When moving faster from A to B the mouse actually does not neccessarily move over every part of the screen in between.
To solve your problem draw a line instead of drawing an ellipse at the mouse's position. Therefore you have to store the point the mouse has been before. After a movement draw a line from the stored, last known point to the actual point.

C# WPF RenderTransform resets on mousedown

I am having a problem with this code. When I start the program Ruler is in the center of the page. When I mousemove when MouseDown is true, the Rectangle (Ruler) is dragable as I want. However, this only works on the first drag. The next time I go to drag it the Ruler jumps back to it's original position, then when you mouse over it the distance from where it was to where it jumped back is calculated and it jumps off the screen as the mouseup event doesn't fire as the rectangle has moved. I basically want to be able to drag the object around the screen however many times I want, but the XStart and YStart need to take the new rendered values on each click.
I think the reason has to do with the e.GetPosition(this).X; as 'this' refers to the Grid that is the rulers parent.
Do I need to commit the RenderTransform to the program? or is there an error in my logic?
It would honestly make more sense if it didn't work at all, but to work perfectly once, then screw up makes no sense.
Here is the code:
private void Rectangle_MouseDown(object sender, MouseButtonEventArgs e)
{
XStart = e.GetPosition(this).X;
YStart = e.GetPosition(this).Y;
Console.WriteLine("X: " + XStart + ", Y: " + YStart);
MouseDown = true;
}
private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
if(MouseDown)
{
X = e.GetPosition(this).X - XStart;
Y = e.GetPosition(this).Y - YStart;
Ruler.RenderTransform = new TranslateTransform(X, Y);
}
}
private void Ruler_MouseUp(object sender, MouseButtonEventArgs e)
{
MouseDown = false;
}
Looks like Mouse.GetPosition doesn't work when dragging like you would expect.
This example seems relevant, but he uses the DragOver event instead of MouseMove, so I'm not entirely sure if it's the same situation.

How can I convert the mouse cursor coordinates when click on pictureBox are to the screen relative mouse cursor coordinates?

In my program I added this code so when I move my mouse all over the screen I will get the mouse cursor coordinates in real time:
Form1 Load:
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
t1.Interval = 50;
t1.Tick += new EventHandler(timer1_Tick);
t1.Enabled = true;;
}
Then the method that get the mouse position:
public static Point GetMousePosition()
{
var position = System.Windows.Forms.Cursor.Position;
return new Point(position.X, position.Y);
}
Then the timer1 tick event:
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = string.Format("X={0}, Y={1}", GetMousePosition().X, GetMousePosition().Y);
}
Then I ran some application and moved the mouse over a specific location on the screen where the application window is and I found this coordinates:
358, 913
Now I have in my program a listBox with items each item present application screenshot. And if I click on the pictureBox for example in this case on the BATTLEFIELD 3 area I get the mouse cursor coordinates according to the pictureBox area.
So I did:
Point screenCoordinates;
Point pictureBoxSnapCoordinates;
private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
{
screenCoordinates = pictureBoxSnap.PointToScreen(e.Location);
pictureBoxSnapCoordinates = e.Location;
}
Now when I click in the pictureBox at the same location as I found the coordinates 358, 913 but on the pictureBox so the results are:
screenCoordinates 435, 724
pictureBoxSnapCoordinates 23,423
The screenCoordinates isn't the same coordinates as I found with the mouse move 358, 913 it's not even close. There is a big difference between 358,913 and 437,724
e.Location is relative to the Control's top left corner. If you want to use e.Location to get the screen coordinates, then you have to first do pictureBoxSnap.PointToScreen(Point.Empty); and then offset by the e.Location.
Also, Cursor.Position returns a Point object, so making a new Point(...) is pointless.
I must add, if you are dealing with images, and you need to interact with mouse, and do any task related with offset, scroll, etc, I recommend you that library, it is open source and have a lot of examples and methods that will help you
https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox

Windows Forms calculating delta mouse movement negative values

I'm trying to create a simple app to send mouse input over the network to remotely control another machine, using C# and .NET Framework 4.5
I'm having a weird issue when I try to calculate the mouse cursor delta movement in the MouseMove event: below is a sample code I made to isolate this issue:
public partial class Form1 : Form
{
private Point previousPosition;
public Form1()
{
InitializeComponent();
previousPosition = MousePosition;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Point mousePos = MousePosition;
int deltaX = (mousePos.X - previousPosition.X);
int deltaY = (mousePos.Y - previousPosition.Y);
label1.Text = "X: " + deltaX.ToString() + " Y: " + deltaY.ToString();
label2.Text = "previousX: " + previousPosition.X + " currentX: " + mousePos.X;
previousPosition = MousePosition;
}
}
I'm storing the previous cursor position in a class variable and on the MouseMove event I calculate the difference in positions and update the previousPosition variable.
Now the weird thing is that this is working only for positive deltas (when I move the mouse to the right).
I put some labels in the form to show in real time the coordinates and, to my surprise, when i move the cursor left, the previous coordinate and the current one always stays the same, resulting in a zero delta!
Am I missing something stupid and obvious here? Why is the previousPosition variable equal to the MousePosition when I move the cursor left, but when moved right it works as expected?
I replicated your issue, and fixed it by adding a check for duplicate move events to the code:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Point mousePos = MousePosition;
if (mousePos == previousPosition)
return;
// .. your other code here
previousPosition = mousePos;
}
I'm not sure why there are multiple mouse-move events for the same position only when moving left.

Categories