Home » .Net FrameworkRSS

How to work with DllImport, mouse simulation, and variables in C#?

using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace WpfApplication1
{
publicpartialclass MainWindow : Window
  {
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    publicstaticexternint SetCursorPos(int x, int y);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    staticexternvoid mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
public MainWindow()
    {
      InitializeComponent();
    }

    privatevoid image1_MouseUp(object sender, MouseButtonEventArgs e)
    {
      SetCursorPos(1650, 30);
      mouse_event(0x0002 | 0x0004, 0, 0, 0, 0);
    }
  }
}

This is a WPF program written in C# that references several old components. There is also DllImport of user32.dll to a allow the program simulate mouse. There is an image in the program's window (image1) releasing a mouse button on which will result in a mouse cursor moving to a position x = 1650, y = 30 on the screen, and simulating a left click.

Other than SetCursorPos(); there is also a similar command in user32.dll that does the opposite: it is GetCursorPos(); and it follows the cursor of the mouse driven by a human. My question is how to use GetCursorPos(); so that each change in the cursor position is recorded as variables double x1, double x2, double x3...; and double y1, double y2, double y3... starting after releasing a mouse button on image1. Is there an opposite command to mouse_event(); that just like GetCursorPos(); records every left (0x0002 | 0x0004) or right (0x0008 | 0x0010) clicks and positions where events happened?

I am a beginner in C#, but I learn fast. Still, there are problems following me everywhere. Sometimes overloads don't match delegates. I would be very thankful to anyone who would explain how to record mouse positions into a number of variables not specified in the program, that is only up to the user, and if it is even possible. I would be very thankful to anyone who would explain me how to record mouse buttons pressed in the same undefined namber of variables. All of these variables are for future use in the code of the program. Thanks for any answers.


<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter