0

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

1 Answer 1

0

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

4
  • I appreciate your response, my question is not getting the mouse co-ordinates, but to export the coordinates into a list. Is it possible?
    – raj3209812
    Feb 12, 2014 at 9:19
  • yes you can use List<Point> points = new List<Point>(); and for saving points.add(e.Location); if you want to write them a file it's a diffrent thing that I cant write into that comment. Feb 12, 2014 at 9:23
  • after adding points to the list, is it possible to view the list?
    – raj3209812
    Feb 12, 2014 at 10:14
  • Yes of course. 'foreach(Point o in points){ listBox1.Items.Add("X ="+o.X+" Y="+o.Y);} Feb 12, 2014 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.