Pointer to System.Windows.Forms.Cursor.Position.X

932 views Asked by At

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.

1

There are 1 answers

5
Mike Dinescu On

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:

this.MouseMove += (System.Windows.Forms.MouseEventHandler)MouseMoveHandler;

Then, define the MouseMoveHandler function to be called when the mouse position changes:

private void MouseMoveHandler(object sender, System.Windows.Forms.MouseEventArgs e)
{      
   int mouseX = e.X;
   int mouseY = e.Y;

   // do what you need to do with the mouse position.. 
}

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, the Point structure might be defined like this:

 struct Point 
 {
      private int fieldX;
      private int fieldY;

      public Point(int x, int y) 
      {
          fieldX = x;
          fieldY = y;
      }

      public int X 
      {
         get {  return fieldX; }
      }

      public int Y
      {
         get {  return fieldY; }
      }
 }

Now, if you try to write some code like this:

 Point p = new Point(10, 20);
 unsafe
 {
     int* pX = &(p.X);
 }

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:

 unsafe
 {
    int* px = &(p.fieldX);
 }

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.