Perform a mouse Click event on another Application using C#

后端 未结 1 1737
予麋鹿
予麋鹿 2021-01-03 15:28

what i need to do is that I need to control another application installed on the same machine using my custom application. For example if i needed to use the standard window

相关标签:
1条回答
  • 2021-01-03 15:51

    Maybe this could help you

    Code

    The task

    • Getting the mouse's current position
    • Sending the mouse event

    Windows forms

    ...
    using System.Runtime.InteropServices;
    
    namespace yournamespace
    {
    
     public partial class yourclassname
     {
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;
    
        int X = Cursor.Position.X;
        int Y = Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
      }
    }   
    

    WPF

    Things are a bit harder in WPF

    double mousePointX;
    double mousePointY;
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(out POINT lpPoint);
    
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
      public int X;
      public int Y;
    
      public POINT(int x, int y)
      {
          this.X = x;
          this.Y = y;
      }
    }
    
    private void WritePoint(object sender, RoutedEventArgs e)
    {
        POINT p;
        if (GetCursorPos(out p))
        {
            System.Console.WriteLine(Convert.ToString(p.X) + ";" + Convert.ToString(p.Y));
        }
    }
    [DllImport("User32.dll")]
    static extern IntPtr GetDC(IntPtr hwnd);
    
    [DllImport("gdi32.dll")]
    static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
    
    [DllImport("user32.dll")]
    static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
    
    private Point ConvertPixelsToUnits(int x, int y)
    {
        // get the system DPI
        IntPtr dDC = GetDC(IntPtr.Zero); // Get desktop DC
        int dpi = GetDeviceCaps(dDC, 88);
        bool rv = ReleaseDC(IntPtr.Zero, dDC);
    
        // WPF's physical unit size is calculated by taking the 
        // "Device-Independant Unit Size" (always 1/96)
        // and scaling it by the system DPI
        double physicalUnitSize = (1d / 96d) * (double)dpi;
        Point wpfUnits = new Point(physicalUnitSize * (double)x,
        physicalUnitSize * (double)y);
    
        return wpfUnits;          
    }
    private void WriteMouseCoordinatesInWPFUnits()
    {
        POINT p;
        if (GetCursorPos(out p))
        {
        Point wpfPoint = ConvertPixelsToUnits(p.X, p.Y);
        System.Console.WriteLine(Convert.ToString(wpfPoint.X) + ";" +   Convert.ToString(wpfPoint.Y));
    
        mousePointY = wpfPoint.Y;
        mousePointX = wpfPoint.X
        }
    }
    

    Now the most important part of the code

     [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;
    
        ...    
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,   Convert.ToUInt32(mousePointX), Convert.ToUInt32(mousePointY), 0, 0); 
        ...
    

    Warning

    • The code is tested
    • The code is not a "copy & paste code
    0 讨论(0)
提交回复
热议问题