纳金网

标题: Unity3d 控制鼠标的移动范围 [打印本页]

作者: 烟雨    时间: 2015-9-29 00:53
标题: Unity3d 控制鼠标的移动范围

有二种方法,一个是调用windows 系统的 user32.dll的ClipCursor函数。再一种就是 通过Cursor.SetCursor函数。


[DllImport( “user32.dll”, CharSet = CharSet.Auto, ExactSpelling = true )]

[return: MarshalAs( UnmanagedType.Bool )]

public static extern bool ClipCursor( ref RECT rcClip );

[DllImport( “user32.dll” )]

[return: MarshalAs( UnmanagedType.Bool )]

public static extern bool GetClipCursor( out RECT rcClip );

[DllImport( “user32.dll” )]

static extern int GetForegroundWindow( );

[DllImport(“user32.dll”)]

[return: MarshalAs( UnmanagedType.Bool )]

static extern bool GetWindowRect( int hWnd, ref RECT lpRect );

[StructLayout( LayoutKind.Sequential )]

public struct RECT

{

public int Left;

public int Top;

public int Right;

public int Bottom;

public RECT( int left, int top, int right, int bottom )

{

Left = left;

Top = top;

Right = right;

Bottom = bottom;

}

}

RECT currentClippingRect;

RECT originalClippingRect = new RECT( );

void Start()

{

hndl = GetForegroundWindow( );

GetWindowRect( hndl, ref currentClippingRect );

GetClipCursor( out originalClippingRect );

ClipCursor( ref currentClippingRect);

}

void OnApplicationQuit()

{

ClipCursor( ref originalClippingRect );

}




欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5