Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have to make a cartoon in C# and have to make his eyes moving according to the cursor movement
C#
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data;
using System.Drawing; 
using System.Linq; 
using System.Text;
 using System.Windows.Forms; 
 namespace nighat_google 
{ 
public partial class Form1 : Form 
{     
public Form1()     
{        
 InitializeComponent();    
 }     
 private void Form1_Paint(object sender, PaintEventArgs e)   
  {         
SolidBrush s=new SolidBrush(Color.Black);  
       e.Graphics.DrawEllipse(Pens.Red, 50, 50, 100, 100);         e.Graphics.DrawEllipse(Pens.Red, 170, 50, 100, 100);   
       e.Graphics.FillEllipse(s, 90, 50, 20, 20 );  
       e.Graphics.FillEllipse(s, 210, 50, 20, 20);    
  } 
} 
} 

I wrote this piece of code , but its not moving the eyes according to the cursor position . Kindly help me out .
Posted
Updated 18-Dec-11 19:22pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Dec-11 1:21am    
What have you done so far? Don't point me to your code, please -- you haven't done anything to move anything. How can it be a cartoon? This is a traditional problem, a pretty easy one.
--SA

1 solution

Just a few hints:

To get a cursor position, use the static method System.Windows.Forms.Cursor.Position, see http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.aspx[^].

Now, the problem is: how to capture the event of cursor motion?

If you want to response to the cursor motion when a mouse cursor stay within a form of your application, this is fairly simple: you need to handle the event MouseMove or override the method OnMouseMove.

If you need to be responsive to any motion of the mouse withing all of the screen, this is more difficult.

First approach is using system global hooks, but for this you will need to setup a hook in a native DLL, so you will need to go outside of .NET and write some native code. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^]. You can setup a hook in C# code, but you need to do it in native code to make the hook global, see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx[^],
http://support.microsoft.com/kb/318804[^],
http://msdn.microsoft.com/en-us/magazine/cc188966.aspx[^].

Another, a trivial but compromised way would be polling (system-wide) mouse position. You can do it in a separate thread or in a timer, in some regular time interval, which means waste of CPU time and limited responsiveness at the same time. In both cases, you will need to notify the UI thread of the move. This is another problem:

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900