4

Please let me elaborate what I am doing.

I am creating a screen share application. Where there are 2 apps first is a windows application (whose screen is getting shared) and the other is a browser web app (On which the user is watching the screen). The browser web app is sending client x and y coordinates of the mouse to the windows app. The windows app is using the x and y coordinates to move the mouse using the given below code.

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

        public static void MoveCursorToPoint(int x, int y)
        {
            SetCursorPos(x, y);
        }

The browser app is also sending mouse clicks and the windows app is performing the clicks using the given below code.

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long 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;

        public static void DoMouseClick()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }

Problem/Challenge

The problem or a challenge here is to perform the drag operation. For an example how will I drag the window of any application from one place to the another place from C# code.

Many thanks for your attention.

Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100

1 Answers1

0

Finally, here is the code that will do all the system mouse events. In order to perform the drag and drop operation, you will need to First Send MouseDown(Mouse Click) and keep it Clicked While Changing the Mouse Position than Send MouseUp(Release Click) something like this.

  MouseHelper mh = new MouseHelper();
            mh.sendMouseDown();
            Cursor.Position = new Point(30, 30);
            mh.sendMouseUp();

Here is the whole helper file

    public class MouseHelper
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        //public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

        private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
        private const uint MOUSEEVENTF_LEFTUP = 0x04;
        private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const uint MOUSEEVENTF_RIGHTUP = 0x10;

        public void sendMouseRightclick(Point p)
        {
            mouse_event((uint)MOUSEEVENTF_RIGHTDOWN | (uint)MOUSEEVENTF_RIGHTUP, (uint)p.X, (uint)p.Y, (uint)0, (uint)0);
        }

        public void sendMouseDoubleClick(Point p)
        {
            mouse_event((uint)MOUSEEVENTF_LEFTDOWN | (uint)MOUSEEVENTF_LEFTUP, (uint)p.X, (uint)p.Y, (uint)0, (uint)0);

            Thread.Sleep(150);

            mouse_event((uint)MOUSEEVENTF_LEFTDOWN | (uint)MOUSEEVENTF_LEFTUP, (uint)p.X, (uint)p.Y, 0, 0);
        }

        public void sendMouseRightDoubleClick(Point p)
        {
            mouse_event((uint)MOUSEEVENTF_RIGHTDOWN | (uint)MOUSEEVENTF_RIGHTUP, (uint)p.X, (uint)p.Y, 0, 0);

            Thread.Sleep(150);

            mouse_event((uint)MOUSEEVENTF_RIGHTDOWN | (uint)MOUSEEVENTF_RIGHTUP, (uint)p.X, (uint)p.Y, 0, 0);
        }

        public void sendMouseDown()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, 50, 50, 0, 0);
        }

        public void sendMouseUp()
        {
            mouse_event(MOUSEEVENTF_LEFTUP, 50, 50, 0, 0);
        }
    }

Source The given code at this source link will give an error

Additional Information: A call to PInvoke function 'KinectHandTrackmyApping!myApp.MainWindow::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

In order resolve the error I have changed

public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

to this

public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

The error was usage of long because long gives an exception because it's a 64-bit, uint works because it's 32-bit -- but it won't work for negative coordinates (which is a common monitor setup in Windows).

Community
  • 1
  • 1
Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100