I'm trying to cancel the while(true) in the code:
while(true)
{
int t = Cursor.Potion.X;
}
I tried to use this:
unsafe
{
int* p = &Cursor.Potion.X;
}
But it raises an error: Cannot take the address of the given expression
I also tried to use Cursor.Handle.ToPointer() but it lead me no where.
What can I do to not use while but still keep track at Cursor.Potion.X?
sorry I forgot to mention I don't wanna use event.
You must understand that X is just a part of a Point structure which in C# is a value type.
Obtaining a reference to the structure (creating a pointer to it) will not accomplish much for you since what you are interested is in receiving updates as the position of the cursor changes. This is not idiomatic of C# and would simply not work since the Postion structure is only created and set with an instantaneous value. It will not be continuously updated. In fact, being a value type it should be immutable.
What you need is to observe a MouseMove event for the form or control you are interested in.
Perhaps something like this:
Then, define the
MouseMoveHandler
function to be called when the mouse position changes:Finally, here's an update with a more detailed explanation of why you can't do what you want to do, despite being told that it serves no purpose.
In .NET the
Cursor.Position
is of type Point which defines two properties: X and Y. The keyword there is properties - they are not fields. So, just to illustrate the concept, thePoint
structure might be defined like this:Now, if you try to write some code like this:
It will fail because you are trying to take a pointer to a property (
X
) of the structure and not to the actual data stored in the backing field for that property (fieldX
in this case).If you had access to the
Point
struct and you could change it to expose the backing field, either by making it public or expoisng a pointer to it - which would be wrong and inadvisable but for the sake of the argument - then you could do something like this:So hopefully now you understand exactly why you are getting the error in the first place.
There is no way to get a pointer to the internal member of the struct unless the struct exposes it.