I have a form and inside it I got a small Panel called Player. How can I "link" the panel to user's mouse, so it will move as the mouse moves?
I've already subscribed Player_MouseMove to Player.MouseMove event but I can't figure out how much has the mouse actually moved. Only way I can come up with is to have a such:
private Point previousLocation;
private void Player_MouseMove(object sender, MouseEventArgs e)
{
int differenceX, differenceY;
differenceX = e.X - previousLocation.X;
differenceY = e.Y - previousLocation.Y;
previousLocation = e.Location;
}
This seems pretty stupid, having an extra variable and calculating the difference everytime. Perfect way would be like Player.LinkToCursor(); or such, but if there's no automated way, is there atleast a better way?
Looking at http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.aspx I can't see anything that would help you get this done any better.
There is however one thing you could do:
Point difference = e.Location - (Size)previousLocation;
Vector-arithmetics ;)
Related
[please see update at the end]
I'm an old stack overflow user (as most developers on, you know, Earth), but that's my first question here.
I'm trying to use an "air mouse" for gaming (pointing it to the screen), but as the mouse sensor is a gyroscope, there's some problems with off-screen movement that I'd like to try to fix by software.
With this gyro mouse, when the user moves its arm pointing outside the screen, the cursor stops at the screen limit, which is no problem. However, when he moves his arm back, no matter the distance from the screen, the cursor immediately moves on-screen. This causes a giant difference between the air mouse real position and the cursor.
This could be fixed with a simple control over the number of pixels, and direction, travelled off-screen, in conjunction with some event handling. If I could sum the number of "offscreen" pixels traveled in -X, +X, -Y and +Y, it would be possible to prevent/cancel the mouse move event - or set the cursor to its previous position, at the edge of the screen - until the control tells me that the physical mouse is pointing back to the screen. Just then I'd allow the cursor to move freely.
Maybe this isn't that usefull, but it's an interesting problem, and see this working would be fun as hell!
Currently, based on this great project, I can just state when the mouse is off-screen, but cannot control how far and in which direction it is moving, to properly implement what I'm trying. It seems to me that such kind of information would be too low-level for my current Windows knowledge.
So, to be clear: how can I, in C# (other languages accepted, but I'd have to learn a lot ;), get any kind of "delta position" information or direction of movement, when the cursor is at the limit of screen?
With all due respect, I'm not interested on using different kinds of controllers, as well as "you shouldn't do this" answers. I have this problem to solve, with these elements, and it would be great to make this work!
UPDATE:
Ok, here's my progress up to now.
In order to get raw mouse data and know about the mouse movement 'offscreen', I had to register my application, using some windows API functions, to receive WM_INPUT messages.
Based on a very old code I found, I was able to get mouse raw data and implement exactly what I wanted. Except for the fact that this code is based on a WdnProc callback, and so it only works when my application has the focus. And I need it to also work when the focus is elsewhere - after all, I'm trying to improve the pointing provided by a gyro mouse, for third party games.
It seems that I should use a hook (a good example here), but I have no idea how to hook for input messages. Tried to merge the code of the two links above, but the first one needs the message.LParam that is passed to the WdnProc - which seems to be unavailable when merely hooking mouse events.
Now I'm way out of my league to make some real progress. Any ideas?
One of the simplest solution to get cursor position and then detect its movement regardless where the cursor is to use win32 API from the user32.dll.
Here is a simple code which gets the cursor position in every 10ms using the timer in the C# Windows Form application and displays it as the title of the window.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
public partial class Form1 : Form
{
Timer timer = new Timer();
public Form1()
{
InitializeComponent();
timer.Interval = 10;
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
// do here whatever you want to do
// just for testing...
GetCursorPos(out Point lpPoint);
this.Text = lpPoint.X + ", " + lpPoint.Y;
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point p);
}
I have a Drag() method on form_MouseDown event. I also have a click event on the form. The problem is that if I click on the form, MouseDown event gets triggered and it never gets the chance to trigger click event.
What is the best way to solve this issue? I was thinking counting pixels if the form is actually dragged, but there has to be a better way. Any suggestions?
I was thinking counting pixels if the form is actually dragged, but there has to be a better way.
Nope, that's exactly how you have to do it.
This isn't just a software limitation; it's very much a practical one as well. If you think through the problem from a user's perspective, you'll immediately see the problem as well as the solution. Ask yourself, what is the difference between a click and a drag?
Both of them start with the mouse button going down over the object, but one of them ends with the mouse button going back up over the object in the same position and the other one ends with the mouse button going back up in a completely different position.
Since time machines haven't been perfected yet, you have no way of knowing this in advance.
So yes, you need to maintain some kind of a distance threshold, and if the pointer moves outside of that distance threshold while it is down over the object, then you consider it a drag. Otherwise, you consider it a click.
That distance threshold should not be 0. The user should not be required to hold the mouse completely still in order to initiate a click. A lot of users are sub-par mousers. They are very likely to twitch slightly when trying to click. If the threshold is 0, they'll end up doing a lot of inadvertent dragging when they try to click.
Of course, you don't actually have to worry about any of this or compute the drag threshold yourself. Instead, use the Windows default values, obtainable by calling the GetSystemMetrics function and specifying either SM_CXDRAG or SM_CYDRAG. (These might be exposed somewhere by the WinForms framework, but I don't think so. It's just as easy to P/Invoke them yourself.)
const int SM_CXDRAG = 68;
const int SM_CYDRAG = 69;
[DllImport("user32.dll")]
static extern int GetSystemMetrics(int index);
Point GetDragThreshold()
{
return new Point(GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
}
In the field of UX/UI, this sort of thing is called hysteresis or debouncing, by analogy to the use of these terms in physics and electronics.
I found this solution, although it is for a double-click and a mouse down events:
void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Clicks ==1)
{
PictureBox pb = (PictureBox)sender;
DoDragDrop((ImageData)pb.Tag, DragDropEffects.Copy);
}
}
source: http://code.rawlinson.us/2007/04/c-dragdrop-and-doubleclick.html
Unfortunatelly, at the point of time when "button-is-pressed" you don't know yet if the desired action is just a click or a drag-drop. You will find it out it later.
For a click, the determinant is "no movement" and "button up".
For a drag, the determinant is "movement" and "button up".
Hence, to disambiguate those interactions, you have to track not only the buttons, but also the movement. You do not need to track the overall movement, only the movement between button-down and button-up is interesting.
Those events are therefore a good place to start/stop the Mouse.Capture mechanisms (to dynamically present drag adorners and drop location hints), or, in simplier form - to store the origin and target of movement vector and check if the distance is > D (even if movement occurred, there should be some safe minimal distance within which the DRAG is canceleed. The mouse is "jaggy" sometimes, and people would really don't like your app to start dragging when they double click at the end of fast mouse pointer movement :) )
I need to update a label every time the pointer mouse moves, but not only in the form, i need to catch the move in all the desktop.
Is it possible?
Thank you.
Do you just need the label to say "Mouse Moved" or is it dependent on where it moved to. I guess either way you could user a Timer that goes off every once-in-a-while and checks the mouse x and y positions and compares them to the previous positions.
prevMouseX = MouseX;
prevMouseY = MouseY;
mouseX = System.Windows.Forms.Cursor.Position.X;
mouseY = System.Windows.Forms.Cursor.Position.Y;
Then compare is mouseX = prevMouseX etc...
You may want to look at Mouse Capture. I'm not sure of all the circumstances you're working with, but take a look at this MSDN article for an overview of mouse capture. This answer also provides some information.
I have a Rectangle which can be dragged within some boundaries. This works perfect with the mouse.
As soon as I set IsManipulationEnabled to true the mouse-events don't work anymore.
However I need this to get touch-events on the rectangle. Therefore I set it to true.
I'm trying to compute all changes in the ManipulationDelta-event like in the following function.
Scaling works already pretty good, but moving the object via dragging it with the finger is very choppy + plus sometimes the Rectangle jumps back and forth.
private void UserControl_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
//Scaling works already pretty good
RangeBar.Width *= e.DeltaManipulation.Scale.X;
//Moving the element is very choppy, what am I doing wrong here?
this.startX = this.startX + e.DeltaManipulation.Translation.X;
RangeBar.SetValue(Canvas.LeftProperty, startX);
}
I'd try using the CumulativeManipulation instead.
Whenever I need to do a UI element movement via dragging, I don't try to reuse the same variable and modify it by the delta and then reuse that same variable for setting position. It almost always gives me stability issues regardless of platform. Instead, try storing a variable when dragging starts and adding the delta to that variable only when you need to update position. So something more like this:
Point origin;
void MouseDown(Point location)
{
origin = location;
}
void MouseDrag(Vector cumulativeOffset)
{
SetControlLocation(origin+cumulativeOffset);
}
Also, what's the source of the ManipulationEvent? You'll definitely want to make sure it's not the current rectangle, or that will definitely cause the issues you're seeing.
I'm working with a WPF app, more specifically a Canvas with draggable elements.
Once an item is being dragged, I'd like to limit the scope of cursor movement to inside the canvas where the items are being dragged about.
The event which can start a drag is shown below
private void WidgetCanvas_PreviewHeaderLeftMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
... logic to figure out if this is a valid drag, blah blah blah ...
this.IsDragging = true;
// TODO: clip the available cursor movement to actual width of Canvas
}
On the Preview-MouseUp, I'd like to simply "unclip" the cursor movement back to it's normal state.
I'll be monitoring the movement of the mouse once I start dragging (PreviewMouseMove), so worst case, I could manually check the position of the mouse and keep it constrained to the canvas, but that seems a little ugly.
Anyone have a better way to limit the cursor boundaries?
There's no clean way to do this and the not-so-clean ways will make your mouse cursor "jitter" at the border of the clipping area.
Moreover, I'd question if this is really a good idea. The user should really own the mouse and he or she generally gets frustrated when you try to artificially limit things that he or she owns.
If you want to provide feedback when the mouse leaves your canvas, maybe you could leave the item being dragged stuck along the border while the mouse button is still down? This would tell the user that he or she has left the target area without trying to impose limitations on where the mouse can go.
Good luck!
You should be able to do it using the ClipCursor native API.