-1

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.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90

1 Answers1

3

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.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • sorry I forgot to mention i dont wanna use evnt. – user1840422 Dec 12 '13 at 21:07
  • @user1840422 - why don't you want to us events? What is it exactly that you'd like to do? – Mike Dinescu Dec 12 '13 at 23:38
  • I would like to use pointer... I cant use this event because the project is my event and I need to right the event so i cant use the MouseMove event. – user1840422 Dec 13 '13 at 15:13
  • Are you trying to create something similar to the events mechanism of the WinForms framework for mouse movement? If so you will have to better explain what you are trying to do. Clearly obtaining a pointer to a value-type that doesn't change over time is going to be useless and it is not the right approach. – Mike Dinescu Dec 13 '13 at 15:54
  • It cound be that this way isn't the right way but i love to do things my way and not the normal... so i need that line that can provide the pointer to the cursor X place – user1840422 Dec 13 '13 at 17:58