Save and Export Mouse co-ordinates Visual Studio C# - c#

I am trying to create an application using which i will be able to click and drag the mouse any where inside a frame and the corresponding mouse coordinates should be saved in to a stack or a list and i should be able to export the list onto a database or excel file.
At present i am able to retrieve the mouse coordinates using,
base.OnMouseMove(e);
x = e.X;
y = e.Y;
toolStripStatusLabelXY.Text = x.ToString();
toolStripStatusLabel1.Text = y.ToString();
Is it possible to do this in a C# win32 form application.
Thankyou

Add this reference to code
using System.Runtime.InteropServices;
and add this code to your code
[DllImport("user32.dll", CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons,uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public void DoLeftMouseClick(int x int y)
{
//this function perfoms left click a position you want
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
public void DoLeftMouseClick(int x int y)
{
//this function perfoms right click a position you want
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
}
If you want to drag use theese;
public void DoLeftMouseClickDown(int x int y)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
}
public void DoLeftMouseClickUp(int x int y)
{
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
public void DoLeftMouseClickDown(int x int y)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN , x, y, 0, 0);
}
public void DoLeftMouseClickUp(int x int y)
{
mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
}
Use theese like this;
DoLeftMouseClickDown(positionToClick.X,positionToClick.Y);
DoLeftMouseClickUp(positionToDrag.X,positionToDrag.Y)
this drags the item which is at positionToClick to positionToDrag

Related

Get the coordinates of a selected text in Chrome

I have a function that triggers a click when passing coordinates to it. How can I get the coordinates of the selected text after doing a search with CTRL + F?
I am launching it with a console application.
EDIT: Code added.
[DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
// LEFT CLICK
public static void SimularClickIzq(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
// RIGHT CLICK
public static void SimularClickDer(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_RIGHTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, xpos, ypos, 0, 0);
}
#### FOCUS ON TEXT ####
SendKeys.SendWait("^f");
Thread.Sleep(2000);
SendKeys.SendWait("Estado de la Solicitud");
Thread.Sleep(2000);
SendKeys.SendWait("{ESC}");
Thread.Sleep(2000);
MISSING COORDINATES HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#### WINDOWS FORM ####
lbl_coordinates.Text = Cursor.Position.ToString();

simulate mouse left click on a target and then move mouse to the previous position in C# .net

I needed to simulate mouse left click in some scenario in the code (c# .net).
this simulation must be done but it must be invisible for users. so when left click is going to occur I save cursor position. After doing left click I should set cursor position to the save value.
there is a problem.
int save_cursur_x = Cursor.Position.X;
int save_cursur_y = Cursor.Position.Y;
int current_node_x = newGraphEditor.getCurrentNodeGlobalPosition_x();
int current_node_y = newGraphEditor.getCurrentNodeGlobalPosition_y();
LeftMouseClick(current_node_x, current_node_y);
Cursor.Position = new Point(save_cursur_x, save_cursur_y);
When the last line ( Cursor.Position = new Point(save_cursur_x, save_cursur_y)) is removed left click happen correctly on the target position. But when I add that line, left click happen on a unknown position (or maybe left click doesn't happen)
This is LeftMouseClick method :
//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
Update:
There is a selection delegate where target position set. this delegate should run exactly when you click on target. I think problem is here . After left click on target this delegate isn't run . After setting Cursor position to previous value delegate is run. so target position doesn't set correctly because now cursor isn't on target.(Its position has changed)
ItemEventHandler<IModelItem> selectionDelegate =
delegate (object source, ItemEventArgs<IModelItem> args)
{
var tag = ((INode)(args.Item)).Tag;
if (tag is HostInnerNodeTag)
{
currentNodeGlobalPosition_x = Cursor.Position.X;
currentNodeGlobalPosition_y = Cursor.Position.Y;
}
}
You can use SendMessage instead.
[DllImport("user32.dll")]
public static extern int SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
//This simulates a left mouse click
public static void LeftMouseClick(IntPtr hwnd, uint xpos, uint ypos)
{
SendMessage(hwnd, WM_LBUTTONDOWN, new IntPtr(xpos), new IntPtr(ypos));
SendMessage(hwnd, WM_LBUTTONUP, new IntPtr(xpos), new IntPtr(ypos));
}
private void send()
{
LeftMouseClick(newGraphEditor.Handle, 10, 10);
}

Onscreen cursor positioning with user's input [duplicate]

I want to simulate mouse movement every x seconds. For that, I'll use a timer (x seconds) and when the timer ticks I'll make the mouse movement.
But, how can I make the mouse cursor move using C#?
Take a look at the Cursor.Position Property. It should get you started.
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
First Add a Class called Win32.cs
public class Win32
{
[DllImport("User32.Dll")]
public static extern long SetCursorPos(int x, int y);
[DllImport("User32.Dll")]
public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
public POINT(int X, int Y)
{
x = X;
y = Y;
}
}
}
You can use it then like this:
Win32.POINT p = new Win32.POINT(xPos, yPos);
Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);

Attempting to Simulate Mouse Click / Drag

So I'm trying to simulate the left mouse click and the left mouse release to do some automated dragging and dropping.
It's currently in a C# Winforms (Yes, winforms :|) and is being a bit of a goose.
Basically, once a Click is sent, I want it to update the cursor position based upon the Kinect input. The Kinect side of things is fine but i'm not sure how to find if the button is still pressed or not.
here's the code i'm currently using + some psuedocode to help better explain myself (the do while).
class MouseImpersonator
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int leftDown = 0x02;
private const int leftUp = 0x04;
public static void Grab(int xPos, int yPos)
{
Cursor.Position = new Point(xPos + 25, yPos + 25);
mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);
//do
//{
//Cursor.Position = new Point(KinectSettings.movement.LeftHandX, KinectSettings.movement.LeftHandY);
//} while (the left mouse button is still clicked);
}
public static void Release(int xPos, int yPos)
{
Cursor.Position = new Point(xPos + 25, yPos + 25);
mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
}
}
I've had a hunt of the google and can't find anything for what I need except for a WPF equivalent: http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.aspx
I'm a bit out of my depth, but any help is greatly appreciated.
Lucas.
-
The Easiest answer was infact to use a bool and just check to see what's going on.
I started it on a new thread so it didn't break everything else.
Idealy you'd tidy this up a little bit.
public static void Grab(int xPos, int yPos)
{
_dragging = true;
Cursor.Position = new Point(xPos, yPos + offSet);
mouse_event(leftDown, (uint) xPos, (uint) yPos, 0, 0);
var t = new Thread(CheckMouseStatus);
t.Start();
}
public static void Release(int xPos, int yPos)
{
_dragging = false;
Cursor.Position = new Point(xPos, yPos + offSet);
mouse_event(leftUp, (uint) xPos, (uint) yPos, 0, 0);
}
private static void CheckMouseStatus()
{
do
{
Cursor.Position = new Point(KinectSettings.movement.HandX, KinectSettings.movement.HandY + offSet);
}
while (_dragging);
}
The following code should return true if the left mouse button is down, false if it is up, Control being System.Windows.Forms.Control:
Control.MouseButtons.HasFlag(MouseButtons.Left)
p.s. documentation for this can be found on MSDN here.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(uint dwFlags, int dx, int dy, uint cButtons, uint dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_MOVE = 0x0001;
static void Drag(int startX,int startY,int endX,int endY)
{
endX = endX - startX;
endY = endY - startY;
SetCursorPos(startX, startY);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_MOVE, endX, endY, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

How to move mouse cursor using C#?

I want to simulate mouse movement every x seconds. For that, I'll use a timer (x seconds) and when the timer ticks I'll make the mouse movement.
But, how can I make the mouse cursor move using C#?
Take a look at the Cursor.Position Property. It should get you started.
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
First Add a Class called Win32.cs
public class Win32
{
[DllImport("User32.Dll")]
public static extern long SetCursorPos(int x, int y);
[DllImport("User32.Dll")]
public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
public POINT(int X, int Y)
{
x = X;
y = Y;
}
}
}
You can use it then like this:
Win32.POINT p = new Win32.POINT(xPos, yPos);
Win32.ClientToScreen(this.Handle, ref p);
Win32.SetCursorPos(p.x, p.y);

Categories