549

就是我了...

导航

<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

公告

IT闲人 点击发送消息给对方点此发送QQ消息

统计

文章分类

档案

随笔分类

相册

Borland

myself

程序相关

朋友

登录

2007年7月2日 #

Mouse类,没有技术含量,不好意思承认是自己写的。

using System;
using System.Runtime.InteropServices;

namespace MouseMoving
{
    class Mouse
    {

        struct NativePOINT {
            public int x;
            public int y;
        }

        [Flags]
        enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        struct NativeRECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [DllImport("user32.dll")]
        private static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll")]
        private static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        [DllImport("user32.dll")]
        private static extern bool GetCursorPos(out NativePOINT Point);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string strClass, string strWindow);

        [DllImport("user32.dll")]
        private static extern IntPtr FindWindowEx(HandleRef  hwndParent, int hwndChildAfter, string strClass, string strWindow);

        [DllImport("user32.dll")]
        private static extern bool GetWindowRect(HandleRef  hwnd, out NativeRECT rect);

        [DllImport("user32.dll")]
        private static extern bool ClipCursor( ref NativeRECT rect);


       

        public int X
        {
           
            get
            {
                NativePOINT PrivateMousePoint;
                if (GetCursorPos(out PrivateMousePoint) != true)
                {
                    PrivateMousePoint.x = -1;
                }
                return PrivateMousePoint.x;
            }
            set
            {
                if (value < 0)
                    return;
                SetCursorPos(value, this.Y);
            }
           
        }
        public int Y
        {
            get
            {
                NativePOINT PrivateMousePoint;
                if (GetCursorPos(out PrivateMousePoint) != true)
                {
                    PrivateMousePoint.y = -1;
                }
                return PrivateMousePoint.y;
            }
            set
            {
                if (value < 0)
                    return;
                SetCursorPos(this.X, value);
            }

        }

        public void Click()
        {
            mouse_event(MouseEventFlag.LeftDown, this.X, this.Y, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, this.X, this.Y, 0, UIntPtr.Zero);
        }

        public void DoubleClick()
        {
            Click();
            Click();
        }

        public void RightClick()
        {
            mouse_event(MouseEventFlag.RightDown, this.X, this.Y, 0, UIntPtr.Zero);
            mouse_event(MouseEventFlag.RightUp, this.X, this.Y, 0, UIntPtr.Zero);
        }

        public void MoveToFormCenter(string formCaption)
        {
            NativeRECT rect;

            IntPtr hwnd = FindWindow(null, formCaption);
            if (hwnd == IntPtr.Zero)
            {
                throw new Exception("Form " + formCaption + " not found!");
            }
            GetWindowRect(new HandleRef(this, hwnd), out rect);
            X = (rect.left + rect.right) / 2;
            Y = (rect.top + rect.bottom) / 2;
        }
        public void MoveToFormTitle(string formCaption)
        {
            NativeRECT rect;

            IntPtr hwnd = FindWindow(null, formCaption);
            if (hwnd == IntPtr.Zero)
            {
                throw new Exception("Form " + formCaption + " not found!");
            }
            GetWindowRect(new HandleRef(this, hwnd), out rect);
            X = (rect.left + rect.right) / 2;
            Y = rect.top + 10;
        }

        public void LockMouse()
        {
            NativeRECT rect;
            rect.left = X;
            rect.right = X + 1;
            rect.top = Y;
            rect.bottom = Y + 1;
            ClipCursor(ref rect);
        }

        public void UnLockMouse()
        {
            NativeRECT rect;
            rect.left = 0;
            rect.right = int.MaxValue;
            rect.top = 0;
            rect.bottom = int.MaxValue;
            ClipCursor(ref rect);
        }

    }
}
//说是写,还不如说是翻译更合适。给比我还懒的人用。里面有个FindWindowEx没用,不过扩充的时候也许你会用到,这里就暂时保留了。

17:45 | 评论 (1)


请不要发表可能给我们带来伤害的政治言论,谢谢配合