Click here to Skip to main content
11,928,078 members (28,741 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Forms Mouse
So far, my program manage to receive values from my sensor and put it in variables. now i want to control mouse movement using that data. the variable consist x1 and y1 for mouse position, click which is 0 = none, 1 = left click, 2 = right click. I've tried to modified Johnny Lee wiiWhiteboard Source Code where removing any wii lib referance and set the input from those variable.. but didn't works. here's my code so far..

public void mouseMovement()
            if (spot !=0)
                int x = x1;
                int y = y1;
                float warpedX = x;
                float warpedY = y;
                warper.warp(x, y, ref warpedX, ref warpedY);
                if (spot == 1)//mouse down
                    if (cursorControl)
                        INPUT[] buffer = new INPUT[2];
                        buffer[0].type = INPUT_MOUSE;
                        buffer[0].mi.dx = (int)(warpedX *65535.0f/screenWidth);
                        buffer[0].mi.dy = (int)(warpedY * 65535.0f / screenHeight);
                        buffer[0].mi.mouseData = 0;
                        buffer[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
                        buffer[0].mi.time = 0;
                        buffer[0].mi.dwExtraInfo = (IntPtr)0;
                        SendInput(2, buffer, Marshal.SizeOf(buffer[0]));

i though this line will enough to move the pointer.. but no.. nothing happens. please help and sorry for bad english. thanks
Posted 4-Jun-12 20:25pm
Edited 4-Jun-12 20:34pm
SAKryukov 5-Jun-12 1:30am
It depends on your platform running the code you show. You need to tag your application type and UI to be used (WPF? Forms? what?)
FehnrirX 5-Jun-12 1:31am
my bad. i using forms.
SAKryukov 5-Jun-12 1:31am
Why 65535?! How is this code supposed to work?
FehnrirX 5-Jun-12 1:33am
errr.. since the received data from wiimote was raw, i thing it need to multiply it first...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It's always bad to use platform-specific features. Besides, I cannot see how your code could work. Please see my comments to the question.

One way of doing such things is this: you could move mouse with System.Windows.Forms using the class System.Windows.Forms.Cursor. Please see:[^].


Basic usage:

int newX = //...
int newY = //...
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(newX, newY);

Please see:[^].

However, study the whole class by its documentation. Always do it while learning a library or doing research as part of development.

FehnrirX 5-Jun-12 1:47am
actually, i still new with c# and every programming language... so, i even don't know what should i write on my program. even i've read MSDN, but still...
Can you provide some source code that allowing to move mouse pointer by input some value at textbox maybe and when i press the button, the mouse will move to that value?? thanks.
SAKryukov 5-Jun-12 11:26am
Sure, please see the update, after [EDIT], but you need to be able to read documentation and find what you can do and what you can use for your purpose.

If you find it reasonable enough, please accept this answer formally (green button) -- thanks.

If it's not clear yet, feel free to ask a follow-up question.
Good luck,
losmac 5-Jun-12 14:59pm
Good answer, my 5!
SAKryukov 5-Jun-12 15:14pm
Thank you, Maciej.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If I understood you, you want to control your mouse clicks. In c# you can use the following code to control mouse clicks on Windows:
using System.Runtime.InteropServices;
[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;
public void DoaClick(int a)
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            if (a==1)
                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToUInt32(X), Convert.ToUInt32(Y), Convert.ToUInt32(0), Convert.ToUInt32(0));
            else if (a==2)
                mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, Convert.ToUInt32(X), Convert.ToUInt32(Y), Convert.ToUInt32(0), Convert.ToUInt32(0));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 5 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100