Click here to Skip to main content
Rate this: bad
good
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()
        {
            mut.WaitOne();
 
            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
v2
Comments
SAKryukov at 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?)
--SA
FehnrirX at 5-Jun-12 1:31am
   
my bad. i using forms.
SAKryukov at 5-Jun-12 1:31am
   
Why 65535?! How is this code supposed to work?
--SA
FehnrirX at 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
good
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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.aspx[^].
 
[EDIT]
 
Basic usage:
 
int newX = //...
int newY = //...
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(newX, newY);
 
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx[^].
 
However, study the whole class by its documentation. Always do it while learning a library or doing research as part of development.
 
—SA
  Permalink  
v3
Comments
FehnrirX at 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 at 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,
--SA
losmac at 5-Jun-12 14:59pm
   
Good answer, my 5!
SAKryukov at 5-Jun-12 15:14pm
   
Thank you, Maciej.
--SA
Rate this: bad
good
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));
        }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 428
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 5 Jun 2012
Copyright © CodeProject, 1999-2014
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