windows 简单api应用

//调用系统函数 将鼠标移动到相应位置
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
public extern static bool SetCursorPos(int x, int y);
//获取当前鼠标的绝对位置
[StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
        }
[DllImport("User32")]
public extern static bool GetCursorPos(out POINT p);
//是否显示鼠标箭头
[DllImport("User32")]
public extern static int ShowCursor(bool bShow);
 //调用系统函数 模拟鼠标事件函数
        [DllImport("user32", EntryPoint = "mouse_event")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

例如:
 private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
        private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起 
        private const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下 
        private const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
        private const int MOUSEEVENTF_ABSOLUTE = 0x8000;//鼠标绝对位置
mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0);//点击  其中 int dx, int dy没有效果
 [DllImport("user32.dll")]
        static extern IntPtr SetActiveWindow(IntPtr hWnd);//激活窗体
[DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);//把窗体顶层显示
//获取窗体的句柄,其中第一个参数可以为null,第二个参数是任务管理器中“应用程序的 名称”
[DllImport("user32.dll")]
        static extern IntPtr FindWindow(string strClass, string strWindow);
//获取窗口的相对的位置 高宽
 [DllImport("user32.dll")]
        private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

   [DllImport("user32.dll")]
        private static extern bool IsZoomed(IntPtr hWnd);//判断窗口最大化,最大化则返回true
//以什么样的方式显示窗体
[DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

//ShowWindow参数
 private const int SW_SHOWNORMAL = 1;
 private const int SW_RESTORE = 9;
 private const int SW_SHOWNOACTIVATE = 4;
 //SendMessage参数
 private const int WM_KEYDOWN = 0X100;
 private const int WM_KEYUP = 0X101;
 private const int WM_SYSCHAR = 0X106;
 private const int WM_SYSKEYUP = 0X105;
 private const int WM_SYSKEYDOWN = 0X104;
 private const int WM_CHAR = 0X102;
ShowWindow(ParenthWnd, SW_RESTORE); //还原窗口

  鼠标滚轮事件

[DllImport("user32.dll")]
        static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
 
        const int MOUSEEVENTF_MOVE = 0x1;
        const int MOUSEEVENTF_LEFTDOWN = 0x2;
        const int MOUSEEVENTF_LEFTUP = 0x4;
        const int MOUSEEVENTF_RIGHTDOWN = 0x8;
        const int MOUSEEVENTF_RIGHTUP = 0x10;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
        const int MOUSEEVENTF_MIDDLEUP = 0x40;
        const int MOUSEEVENTF_WHEEL = 0x800;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
 
 
        protected override bool ProcessCmdKey(ref Message msg, Keys key)
        {
            const int WM_KEYDOWN = 0x100;
            const int WM_SYSKEYDOWN = 0x104;
            if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
            {
                switch (key)
                {
                    case Keys.Down:
                        mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -100, 0);
                        break;
                    case Keys.Up:
                        mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 100, 0);
                        break;
                }
            }
            return base.ProcessCmdKey(ref msg, key);
        }

  

posted @ 2014-02-08 17:44  Bonker  阅读(817)  评论(0编辑  收藏  举报