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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.