Get mouse co-ordinates continuously while mouse move onmousedown - c#

I can get mouse co-ordinates when mouse is down and up as
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
mouseClickedX = e.X;
mouseClickedY = e.Y;
}
private void panel2_MouseUp(object sender, MouseEventArgs e)
{
mouseReleaseX = e.X;
mouseReleaseY = e.Y;
}
But I need the mouse co-ordinates continuously when mouse is down and move until mouse is up. I don't need co-ordinates when mouse move only but I need co-ordinates when mouse is down and move. How to do that?
EDIT:
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
while (isDragging) {
mouseMoveX = e.X;
mouseMoveY = e.Y;
label1.Text = mouseMoveX.ToString();
label2.Text = mouseMoveY.ToString();
}
}
I am using isDragging true or false onmosueup and down but this just hang the application. Should I use timer or thread?

You need to handle MouseMove and check whether the mouse is down.

There are a few things you should do:
Add to your class a private boolean field called bool isDragging
In the MouseDown handler, set isDragging = true and this.Capture = true
In the MouseUp handler, set isDragging = false and this.Capture = false
Add a MouseMove handler. In it, check if (isDragging) and if it is true, respond as you wish. Your MouseMove handler will be supplied with the current mouse coords.
The use of Capture is important, because otherwise you can lose MouseMove and MouseUp messages.

Related

Touch manipulations with AddManipulator

When using AddManipulator, are touches routed to the new Manipulation? When I do the following, it all works but touches are left after the TouchLeave event. So for example if I do a single finger rotate, leave the touched element and touch it again, it becomes a two finger zoom.
private void HV3DTouchDown(object sender, TouchEventArgs e)
{
Canvas canvas = sender as Canvas
Manipulation.AddManipulator(canvas, e.TouchDevice.AsManipulator());
e.Handled = true;
CaptureTouch(e.TouchDevice);
}
private void HV3DTouchLeave(object sender, TouchEventArgs e)
{
Canvas canvas = sender as Canvas
Manipulation.RemoveManipulator(canvas, e.TouchDevice.AsManipulator());
e.Handled = true;
ReleaseTouchCapture(e.TouchDevice);
}

How can I capture mouse events that occur outside of a (WPF) window?

I have a Window element that has WindowStyle="None" and AllowsTransparency="True", therefore it has no title bar and supports transparency.
I want the user to be able to move the Window to any position on the screen by left-clicking anywhere within the Window and dragging. The Window should drag along with the mouse as long as the left mouse button is pressed down.
I was able to get this functionality to work with one exception: when the mouse moves outside of the Window (such as when the left mouse button was pressed down near the edge of the Window and the mouse is moved quicly), the Window no longer captures the mouse position and does not drag along with the mouse.
Here is the code from the code-behind that I use to get the job done:
public Point MouseDownPosition { get; set; }
public Point MousePosition { get; set; }
public bool MouseIsDown { get; set; }
private void window_MyWindowName_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MouseDownPosition = e.GetPosition(null);
MouseIsDown = true;
}
private void window_MyWindowName_MouseMove(object sender, MouseEventArgs e)
{
if (MouseIsDown)
{
MousePosition = e.GetPosition(null);
Left += MousePosition.X - MouseDownPosition.X;
Top += MousePosition.Y - MouseDownPosition.Y;
}
}
private void window_MyWindowName_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MouseIsDown = false;
}
I think you are looking for this: Processing Global Mouse and Keyboard Hooks in C#
Url: Processing Global Mouse and Keyboard Hooks in C#
This class allows you to tap keyboard and mouse and/or to detect their activity even when an application runs in the background or does not have any user interface at all.
This class raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need.
There is an example and explain and demo to use.
Great tutorial!
Example:
UserActivityHook actHook;
void MainFormLoad(object sender, System.EventArgs e)
{
actHook= new UserActivityHook(); // crate an instance
// hang on events
actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
actHook.KeyDown+=new KeyEventHandler(MyKeyDown);
actHook.KeyPress+=new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp+=new KeyEventHandler(MyKeyUp);
}
Now, an example of how to process an event:
public void MouseMoved(object sender, MouseEventArgs e)
{
labelMousePosition.Text=String.Format("x={0} y={1}", e.X, e.Y);
if (e.Clicks>0) LogWrite("MouseButton - " + e.Button.ToString());
}
I believe you are reinventing the wheel. Search for "Window.DragMove".
Example:
private void title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
Try it this way:
// method to convert from 'old' WinForms Point to 'new' WPF Point structure:
public System.Windows.Point ConvertToPoint(System.Drawing.Point p)
{
return new System.Windows.Point(p.X,p.Y);
}
// some locals you will need:
bool mid = false; // Mouse Is Down
int x=0, y=0;
// Mouse down event
private void MainForm_MouseDown(object sender, MouseButtonEventArgs e)
{
mid=true;
Point p = e.GetPosition(this);
x = (int)p.X;
y = (int)p.Y;
}
// Mouse move event
private void MainForm_MouseMove(object sender, MouseButtonEventArgs e)
{
if(mid)
{
int x1 = e.GetPosition(this).X;
int y1 = e.GetPosition(this).Y;
Left = x1-x;
Top = y1-y;
}
}
// Mouse up event
private void MainForm_MouseUp(object sender, MouseButtonEventArgs e)
{
mid = false;
}

Moving a control within another control's visible area

I have a PictureBox that is inside a TabPage, and of course this TabPage is part of a TabView and this TabView is inside a Form. I want users be able to move this picture box within the tab page. For this I am using the MouseDown, MouseMove and MouseUp events of the picture box:
private void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e)
{
if (!_mapPackageIsMoving)
{
_mapPackageIsMoving = true;
}
}
private void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e)
{
if(_mapPackageIsMoving)
{
pictureBoxPackageView.Location = MousePosition; //This is not exact at all!
return;
}
//Some other code for some other stuff when picturebox is not moving...
}
private void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e)
{
if (_mapPackageIsMoving)
{
_mapPackageIsMoving = false; //Mouse button is up, end moving!
return;
}
}
But my problem lies in the MouseMove event. As soon as I move mouse after button down, the picture box jumps out of tab page's visible area.
I need to know how to handle the move only within the rectangle of the tab page, and if picture box is being dragged out of tab view's visible area, it shouldn't move anymore unless user brings the mouse inside the tab view's visible rectangle.
Any helps/tips will be appriciated!
You need a variable to hold the original position of the PictureBox:
Modified from a HansPassant answer:
private Point start = Point.Empty;
void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e) {
_mapPackageIsMoving = false;
}
void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) {
if (_mapPackageIsMoving) {
pictureBoxPackageView.Location = new Point(
pictureBoxPackageView.Left + (e.X - start.X),
pictureBoxPackageView.Top + (e.Y - start.Y));
}
}
void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e) {
start = e.Location;
_mapPackageIsMoving = true;
}

How to move PictureBox in C#?

i have used this code to move picture box on the pictureBox_MouseMove event
pictureBox.Location = new System.Drawing.Point(e.Location);
but when i try to execute the picture box flickers and the exact position cannot be identified. can you guys help me with it. I want the picture box to be steady...
You want to move the control by the amount that the mouse moved:
Point mousePos;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
mousePos = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
int dx = e.X - mousePos.X;
int dy = e.Y - mousePos.Y;
pictureBox1.Location = new Point(pictureBox1.Left + dx, pictureBox1.Top + dy);
}
}
Note that this code does not update the mousePos variable in MouseMove. Necessary since moving the control changes the relative position of the mouse cursor.
You have to do several things
Register the start of the moving operation in MouseDown and remember the start location of the mouse.
In MouseMove see if you are actually moving the picture. Move by keeping the same offset to the upper left corner of the picture box, i.e. while moving, the mouse pointer should always point to the same point inside the picture box. This makes the picture box move together with the mouse pointer.
Register the end of the moving operation in MouseUp.
private bool _moving;
private Point _startLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_moving = true;
_startLocation = e.Location;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_moving = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_moving) {
pictureBox1.Left += e.Location.X - _startLocation.X;
pictureBox1.Top += e.Location.Y - _startLocation.Y;
}
}
Try to change SizeMode property from AutoSize to Normal

Mouse Cursor position changes

Hi I have a windows form application which i want to move my mouse then dragdrop will function but i have tried using mousemove mouse event to do it , but seems like dragdrop is very sensitive.So what i'm asking is whether is it possible to detect if the mouse cursor moves at least a certain distance away from the current cursor then it does the dragdrop code.
I understand you have a drag & drop code you want to execute only if the mouse has moved a certain distance. If so:
You can hook to the mouse move event once the user has performed the initial action (mouse down on the item to drag ?). Then compare the mouse coordinates on the mouse move event and trigger the "dragdrop code" once the coordinates difference is higher then the arbitrary value you set.
private int difference = 10;
private int xPosition;
private int yPosition;
private void item_MouseDown(object sender, MouseEventArgs e)
{
this.MouseMove += new MouseEventHandler(Form_MouseMove);
xPosition = e.X;
yPosition = e.Y;
}
private void Form_MouseMove(object sender, MouseEventArgs e)
{
if (e.X < xPosition - difference
|| e.X > xPosition + difference
|| e.Y < yPosition - difference
|| e.Y > yPosition + difference)
{
//Execute "dragdrop" code
this.MouseMove -= Form_MouseMove;
}
}
This would execute dragdrop when the cursor moves out of a virtual 10x10 square.
I do not really get your question and the effect you are trying to acheive.
BTW, if my interpretation is correct, you oare trying to do something only if the "drag distance" is grather than a certain amount.
this code below do not use the drag&drop event, but the mouseup, mousedown and mousemove events.
It keeps track of the distance the mouse travels while the left button is pressed.
When the distance is grater than a fixed amount, it changes the mouseCursor(during the dragaction)
When the mouseButton is released, if the dstance traveled is greater than the minimum offset, we perform our fake drop action.
Create a new windows form project and substitute the Form1 autogenerated Class with the one below
Hope this helps.
public partial class Form1 : Form
{
private bool isDragging; //We use this to keep track that we are FakeDragging
private Point startPosition; //The start position of our "Fake" dragging action
private double dragDistance = 0; //The distance(absolute) from the drag action starting point
private const int MINIMUM_DRAG_DISTANCE = 100; //minimum FakeDrag distance.
private Label label1 = new Label();
public Form1()
{
#region Support form generation code
InitializeComponent();
this.label1 = new System.Windows.Forms.Label();
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(54, 242);
this.label1.Name = "Distance:";
this.label1.Size = new System.Drawing.Size(35, 13);
this.Controls.Add(label1);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
#endregion
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//if the mouse button is pressed then
//there's the chanche a dragAction
//is being performed and we take note
//of the position of the click
//(we will use this later on the mouseMove event
//to calculate the distance mouse has traveled
if (e.Button.Equals(MouseButtons.Left))
startPosition = e.Location;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
///at the mouse up event we can execute code that does something
if (isDragging && dragDistance > MINIMUM_DRAG_DISTANCE)
{
MessageBox.Show("we have fakedragged!\nDo something useful here");
//Do your Drag & Drop code here.
}
///but WE MUST signal our system that the Fake Drag has ended
///and reset our support variables.
this.Cursor = Cursors.Default;
isDragging = false;
dragDistance = 0;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
///when the mouse moves we control if the mouse button is still pressed.
///if it is so, we surely are FakeDragging and we set our boolean to true
///(it will be useful in the mouseUP event).
///Then we calculate the absolute distance the mouse has traveld since the
///user has pressed the left mouseButton.
Point currentPosition = e.Location;
if (e.Button.Equals(MouseButtons.Left))
{
isDragging = true;
dragDistance =
Math.Abs(
Math.Sqrt(
(
Math.Pow(
(currentPosition.X - startPosition.X), 2)
+
Math.Pow(
(currentPosition.Y - startPosition.Y), 2)
)));
}
//we set the label text displaying the distance we just calculated
label1.Text =
String.Format(
"Distance: {0}",
dragDistance.ToString());
Application.DoEvents();
///At last, if the distance il greater than our offset, we change the
///mouse cursor(this is not required in a real case scenario)
if (dragDistance > MINIMUM_DRAG_DISTANCE)
this.Cursor = Cursors.Hand;
else
this.Cursor = Cursors.Default;
}
}

Categories

Resources