2016-03-06 111 views
0

我正在做一个项目来控制鼠标,在下面的代码中,我有点迷路。this.Cursor not working properly?

我需要声明的对象命名空间:

using System.Windows; 
using System.Windows.Forms; 
using System.Drawing; 

,并在这里代码:

this.Cursor = new Cursor(Cursor.Current.Handle); 
Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y); 
Cursor.Clip = new Rectangle(this.Location, this.Size); 

它告诉我,光标不会在上下文中存在,但只有在this.Cursor 。同样适用于this.Locthis.Size。有人知道为什么我错过了一个命名空间吗?

编辑:确切的代码:

public class MouseMove 
{ 
    [DllImport("user32.dll")] //TODO add block feature on screens that need it 
    private static extern bool BlockInput(bool block); 

    public static void Main() 
    { 
     this.Cursor = new Cursor(Cursor.Current.Handle); 
     Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y); 
     Cursor.Clip = new Rectangle(this.Location, this.Size); 
    } 
} 
+1

你能提供写代码的方法和类吗? – Valentin

+1

你的班级有一个名为'Cursor'的字段吗? –

回答

1

PositionClipCursor静态属性。您无法使用实例访问它们。为了使用静态变量,您需要使用以下语法:classname.variablename。你的情况的代码应该类似于:

static void Main(string[] args) 
{ 
    Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y); 
    Cursor.Clip = new Rectangle(location, size); 
} 

正如我认为,你从MSDN花了一个例子,但在本例中有与具有光标形式的WinForm应用程序 - this.Cursor。 并在Cursor.PositionCursor是一个类名称,而不是一个实例。

private void MoveCursor() 
{ 
    //here Cursor is a form's property 
    this.Cursor = new Cursor(Cursor.Current.Handle); 
    // here Cursor is a class name, Position is a static variable. 
    Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50); 
    // here Cursor is a class name, Clip is a static variable. 
    Cursor.Clip = new Rectangle(this.Location, this.Size); 
} 
1

你在做什么用自身替换系统的光标...

我建议是这样的:

public static void Main() 
{ 
    Cursor myCursor = new Cursor(Cursor.Current.Handle); 
    myCursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y); 
    myCursor.Clip = new Rectangle(this.Location, this.Size); 
} 

这样,它是安全的。但即使如此,我不知道你想要完成什么......

+1

你不能在Main()中调用'myCursor',因为'myCursor'不是静态的 – Sakura

+0

它只是用于我正在制作的游戏中的一个教程阶段,它为你操作游标。我所要做的就是让它移动到我想要的地方。我会试试这个,谢谢 –

+0

@Sakura:正确。感谢您指出了这一点。 – MPelletier