c# 調用win32類比點擊的兩種方法

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   div   art   

 

第一種

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsApplication2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            this.MouseClick+=new MouseEventHandler(Form1_MouseClick);        }        void Form1_MouseClick(object sender, MouseEventArgs e)        {            MessageBox.Show("X :" + e.X.ToString() + "Y :" + e.Y.ToString());        }        private void button1_Click(object sender, EventArgs e)        {            int x=58;            int y=32;            //寫法1            PostMessage(this.Handle, WM_LBUTTONDOWN, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000);            PostMessage(this.Handle, WM_LBUTTONUP, 0, (x & 0xFFFF) + (y & 0xFFFF) * 0x10000);            //簡化下  其實就是Y座標左移16位,然後再加上X座標            PostMessage(this.Handle, WM_LBUTTONDOWN, 0, x + (y<<16));            PostMessage(this.Handle, WM_LBUTTONUP, 0, x+(y<<16));        }          uint WM_LBUTTONDOWN  = 0x201;          uint WM_LBUTTONUP  = 0x202;                [DllImport("user32.dll", SetLastError = true)]        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);    }}

 

查看Win32Api, point.Y << 16這個是移位操作(即yy×2的16次方),下面的point.X | (point.Y << 16),滑鼠的xy座標值做與操作,所以兩個是一樣的。

滑鼠的座標是以螢幕的像素點來計算的,從左上方分別為(x0,y0)。

原來或操作 高16位存放y軸座標,低15位存放x軸座標

第二種

 

 

Virtual mouse click c#Int32 word = MakeLParam(x, y); private int MakeLParam(int LoWord, int HiWord)     {         return ((HiWord << 16) | (LoWord & 0xffff));     }       public static class SimInput{    [DllImport("user32.dll")]    static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);     [Flags]    public enum MouseEventFlags : uint    {        Move = 0x0001,        LeftDown = 0x0002,        LeftUp = 0x0004,        RightDown = 0x0008,        RightUp = 0x0010,        MiddleDown = 0x0020,        MiddleUp = 0x0040,        Absolute = 0x8000    }     public static void MouseEvent(MouseEventFlags e, uint x, uint y)    {        mouse_event((uint)e, x, y, 0, UIntPtr.Zero);    }     public static void LeftClick(Point p)    {        LeftClick((double)p.X, (double)p.Y);    }     public static void LeftClick(double x, double y)    {        var scr = Screen.PrimaryScreen.Bounds;        MouseEvent(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp | MouseEventFlags.Move | MouseEventFlags.Absolute,            (uint)Math.Round(x / scr.Width * 65535),            (uint)Math.Round(y / scr.Height * 65535));    }     public static void LeftClick(int x, int y)    {        LeftClick((double)x, (double)y);    }}       public static void MouseLeftClick(Point p, int handle = 0){    //build coordinates    int coordinates = p.X | (p.Y << 16);    //send left button down    SendMessage(handle, 0x201, 0x1, coordinates);    //send left button up    SendMessage(handle, 0x202, 0x1, coordinates);}

 

 

 

c

c# 調用win32類比點擊的兩種方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.