Get the coordinates of a selected text in Chrome - c#

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();

Related

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);
}

C# mouse_event click doesn't work in my case

I'm using a c# code to click on other process window,It's work great but it doesn't work when i want to click in Bluestack(Android Emulator) window.
any idea ?
the code :
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long 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 DoMouseClick()
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
Maybe you need to make a virtual mouse device to work correctly
It will be difficult to make maybe try to use external DLLs

Save and Export Mouse co-ordinates Visual Studio 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

Directing mouse events [DllImport("user32.dll")] click, double click

I tried
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
and it works pretty fine to move the cursor to the desired point. I have never tried such kind of a DLL import before but it works :). However I want more what else can I extract?
Mainly I want to make double click, click or use wheel options without any mouse input, just the code how can I do that? and how can I check what else is included in user32dll?
Thanx
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
private const uint MOUSEEVENTF_RIGHTUP = 0x10;
You should Import and Define these Constant's to work with Mouse using Win32API
Use method's below to do Mouse Operation's
void sendMouseRightclick(Point p)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
}
void sendMouseDoubleClick(Point p)
{
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
Thread.Sleep(150);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
}
void sendMouseRightDoubleClick(Point p)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
Thread.Sleep(150);
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, p.X, p.Y, 0, 0);
}
void sendMouseDown()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 50, 50, 0, 0);
}
void sendMouseUp()
{
mouse_event(MOUSEEVENTF_LEFTUP, 50, 50, 0, 0);
}
If you want to do a Mouse Drag you should First Send MouseDown(Mouse Click) and keep it Clicked While Changing the Mouse Position than Send MouseUp(Release Click) something like this .
sendMouseDown();
Cursor.Position = new Point(30,30);
sendMouseUp();
Using long type raises an "PInvoke" error.
We should use int type:
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy,
int dwData, int dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
public static void LeftClick(int x, int y)
{
Cursor.Position = new System.Drawing.Point(x, y);
mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}
source: http://www.pinvoke.net/default.aspx/user32.mouse_event
Take a look at pinvoke.net for a listing of the available APIs. Code examples and the import statements are included. You can also expand the selection on the left to see the available APIs for each DLL.

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);
}

Categories