Mega Code Archive

 
Categories / C# / WPF
 

Make an object follow the mouse pointer as it moves on the screen

<Window x:Class="WpfApplication1.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="mouseMoveWithPointer" Height="400" Width="500">   <Canvas MouseMove="MouseMoveHandler" Background="Red">     <Ellipse Name="ellipse" Fill="LightBlue" Width="100" Height="100"/>   </Canvas> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Shapes; using System.Windows.Input; namespace WpfApplication1 {     public partial class Window1 : Window     {         public Window1()         {             InitializeComponent();         }         private void MouseMoveHandler(object sender, MouseEventArgs e)         {             // Get the x and y coordinates of the mouse pointer.             Point position = e.GetPosition(this);             double pX = position.X;             double pY = position.Y;             ellipse.Width = pX;             ellipse.Height = pY;         }     } }