C# 鼠标在固定位置移动

来源:互联网 发布:java telnet登录失败 编辑:程序博客网 时间:2024/05/16 10:42

  [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
        public extern static int ClipCursor(ref   RECT lpRect);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
        public extern static int GetWindowRect(int hwnd, ref   RECT lpRect);

        public struct RECT//声明参数的值
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        public void Lock(System.Windows.Forms.Form ObjectForm)
        {
            RECT _FormRect = new RECT();
            GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
            ClipCursor(ref _FormRect);
        }
        
        //恢复移动
        public void UnLock()
        {
            RECT _ScreenRect = new RECT();
            _ScreenRect.top = 0;
            _ScreenRect.left = 0;
            _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
            _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
            ClipCursor(ref   _ScreenRect);
        }

原创粉丝点击