Getting the mouse position outside my application in C#
2 replies, posted
(I'm fairly new to this whole C thing and programming applications)
This is the way I'm doing it:
[cpp]
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point point);
public static Point CursorPos()
{
Point point = new Point();
GetCursorPos(ref point);
return point;
}
public Point getMousePoint(bool global)
{
if (global)
{
return CursorPos();
}
return Mouse.GetPosition(null);
}
[/cpp]
The problem with this is that the Point is being weird and its X value returns a [b]float[/b] from 0 to 10 or something. It's also affected when I move Y, as if it's all in one number being calculated in a weird way.
Example:
X: 5.7506085956503e-312
Y: 0
I was following this tutorial on how to do it:
[url]http://www.geekpedia.com/tutorial146_Get-screen-cursor-coordinates.html[/url]
I tried to compile that program and it seemed to work like it should.
Am I being blind? Is the mistake obvious? :confused:
Well, it doesn't look like this is a Visual application (GUI). But if im mistakin, and it is, you can just use [CODE]Cursor.Position = new Point (x,y);[/CODE]
Looking at the Cursor class it doesn't have Position in it.
The problem with the regular way of getting the cursor position is that it doesn't update outside my drawn stuff. (I'm drawing things, not using any window forms or anything like that)
[editline]04:08AM[/editline]
I solved this.
[cpp]
using System.Windows;
namespace Libraries
{
class Mouse
{
Interface I;
public Mouse(Interface I)
{
this.I = I;
I.L.RegisterFunction("MousePos", this, this.GetType().GetMethod("MousePos"));
}
struct pos
{
public double x;
public double y;
}
public object MousePos()
{
pos returnpos = new pos();
returnpos.x = System.Windows.Forms.Cursor.Position.X;
returnpos.y = System.Windows.Forms.Cursor.Position.Y;
return returnpos;
}
}
}
[/cpp]
Sorry, you need to Log In to post a reply to this thread.