none
Move mouse programmatically

    Question

  • Is there a way to changing the mouse position programmatically?

    The reason why I need this: I change the cursor to Cursors.Wait during a long operation.  However the change does not show if I do not move the mouse.  Or perhaps there is a way of immediately showing the change without moving the mouse?

    Monday, August 08, 2011 2:25 PM

Answers

  • Hi,

    The long operation I refer to is a RIA Services call to retrieve database data to populate my DataGrid.

    According to your description ,I have created a sample project as below:

    1.Create a Domain Service Class file(which is named as POCOService) in .Web project:

    namespace SilverlightAppSampleC.Web
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.ServiceModel.DomainServices.Hosting;
        using System.ServiceModel.DomainServices.Server;
    
    
        // TODO: Create methods containing your application logic.
        [EnableClientAccess()]
        public class POCOService : DomainService
        {
            // You can get the data from your database
            // I get the data like this in order to run the sample conveniently.
            public POCOEntity GetStandardEntity()
            {
                POCOEntity entity = new POCOEntity();
                entity.ID = Guid.NewGuid();
                entity.Prop1 = 1;
                entity.Prop2 = 1;
                entity.AssociatedEntities = new AssociatedEntity[]{
                new AssociatedEntity {
                    ID = Guid.NewGuid(),
                    Prop1 = entity.Prop1,
                    Prop2 = entity.Prop2,
                    Name = "Ming1"
                },
                new AssociatedEntity {
                    ID = Guid.NewGuid(),
                    Prop1 = entity.Prop1,
                    Prop2 = entity.Prop2,
                    Name = "Ming2"
                }
    
                };
                return entity;
            }
        }
        public class POCOEntity
        {
            [Key]
            public Guid ID { get; set; }
            public int Prop1 { get; set; }
            public int Prop2 { get; set; }
    
            [Include]
            [Association("POCOEntity_AssociatedEntity", "Prop1,Prop2", "Prop1,Prop2")]
            public ICollection<AssociatedEntity> AssociatedEntities { get; set; }
        }
        public class AssociatedEntity
        {
            [Key]
            public Guid ID { get; set; }
            public int Prop1 { get; set; }
            public int Prop2 { get; set; }
            public string Name { get; set; }
        }
    }

    2.Code in a silverlight page(.xaml file):

    <Grid x:Name="LayoutRoot">
            <StackPanel>
                <Button Name="btn" Content="Load data to DataGrid" Margin="20" Width="150" Height="30" Click="btn_Click" />
                <sdk:DataGrid Name="datagrid1" Width="400" Height="300" />
            </StackPanel>
        </Grid>

    3.Code in the silverlight page(.xaml.cs file):

     private LoadOperation<POCOEntity> loadOp;
            private void btn_Click(object sender, RoutedEventArgs e)
            {
                this.Cursor = Cursors.Wait;
                POCOContext con = new POCOContext();
                loadOp = con.Load(con.GetStandardEntityQuery());
                loadOp.Completed += new EventHandler(loadOp_Completed);
            }
            void loadOp_Completed(object sender, EventArgs e)
            {
                IEnumerable<POCOEntity> data = loadOp.Entities;
                ObservableCollection<POCOEntity> list = new ObservableCollection<POCOEntity>(data);

                // Here I use a datagrid control to display the value of POCOEntity.AssociatedEntities property.
                this.datagrid1.ItemsSource = list[0].AssociatedEntities;
                this.Cursor = Cursors.Arrow;
            }

    The sample project works fine at my side ,thus please check the sample project above and see whether it helps.

    In addition ,I would recommend that you can also refer the link below for an article which explains how to change the mouse cursor:

    http://blogs.silverlight.net/blogs/msnow/archive/2008/08/14/tip-of-the-day-27-how-to-change-the-mouse-cursor.aspx

    Hope it can help you.

    Sunday, August 14, 2011 10:29 PM
  • Hi Ming,

    Thanks for the sample code.  I was familiar with that way of changing the cursor.  I was having a problem because I was initiating the long operation with a button that belongs to another user control on the main page, and I was changing the cursor in another usercontrol where the DataGrid is hosted.  So what changes is not the Button.Cursor but the DataGrid.Cursor, and does not become apparent until I move the mouse to the DataGrid, leading me to believe that I had to move the cursor to show the change which was not correct.  The code change is from

    this.Cursor = Cursors.Wait

    to

    this.GetParent(typeof(MainPage).Cursor = Cursors.Wait.

    which changes, in effect, the cursor of the whole main page.  So the cursor change is visible whereever the mouse is on the page.

     

    Thanks to everyone for your answers.

    Wednesday, August 17, 2011 1:30 PM

All replies

  • unless you're running in OOB+FullTrust and you have access to COM, then no there is no way.

    could you not just pop up a message that says something like "operation complete"?  so then the user would move their mouse to hit the 'OK' button...

    Monday, August 08, 2011 3:56 PM
  • Actualy what I need is for the cursor to display as Cursors.Wait when the long operation starts and not when the long operation ends.

    Monday, August 08, 2011 5:35 PM
  • Hi,

    The reason why I need this: I change the cursor to Cursors.Wait during a long operation.  However the change does not show if I do not move the mouse. 

    From my experience ,you can use a new Thread to do the long operation ,set Cursor as Cursors.Wait before the new Thread being started, then use the Dispatcher.BeginInvoke method to set Cursor as Cursors.Arrow when the operation is completed.

    Actualy what I need is for the cursor to display as Cursors.Wait when the long operation starts and not when the long operation ends.

    Here is the complete sample ,please check it and see whether it helps.

    1.Code in a silverlight page(.xaml file):

    <Grid x:Name="LayoutRoot">
            <StackPanel>
                <Button Content="Click to start the long operation" Name="btn" Width="200" Height="50" Click="btn_Click" Margin="20" />
            </StackPanel>
        </Grid>

    2.Code in the silverlight page(.xaml.cs file):

     private void btn_Click(object sender, RoutedEventArgs e)
            {
                this.Cursor = Cursors.Wait;
                this.btn.Content = "Wating";
                new Thread(() =>
                    {
                        // Long operation.
                        Thread.Sleep(10000);
                        this.Dispatcher.BeginInvoke(new Action(() => 
                        {
                            this.Cursor = Cursors.Arrow;
                            this.btn.Content = "Click to start the long operation";
                        }));
                    }).Start();
            }

    Hope it can help you.

    Friday, August 12, 2011 5:32 AM
  • Hi Ming,

    The long operation I refer to is a RIA Services call to retrieve database data to populate my DataGrid.  I'm not on my development computer right now so I cannot copy the exact source code, but this is how it looks like:

     

    Cursor origCursor;

    void btnGetData_Click(...)

    {

       origCursor = Cursors.Current;

       Cursors.Current = Cursors.Wait;

       LoadOperation lo = ...

       lo.OnCompleted += loOnCompleted;

    }

    void loOnCompleted(...)

    {

      Cursors.Current = origCursor;

    }

    As I said in my original post, the cursor does not change to Cursors.Wait unti I move my mouse to the datagrid.

    Saturday, August 13, 2011 8:22 AM
  • Hi,

    The long operation I refer to is a RIA Services call to retrieve database data to populate my DataGrid.

    According to your description ,I have created a sample project as below:

    1.Create a Domain Service Class file(which is named as POCOService) in .Web project:

    namespace SilverlightAppSampleC.Web
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.ServiceModel.DomainServices.Hosting;
        using System.ServiceModel.DomainServices.Server;
    
    
        // TODO: Create methods containing your application logic.
        [EnableClientAccess()]
        public class POCOService : DomainService
        {
            // You can get the data from your database
            // I get the data like this in order to run the sample conveniently.
            public POCOEntity GetStandardEntity()
            {
                POCOEntity entity = new POCOEntity();
                entity.ID = Guid.NewGuid();
                entity.Prop1 = 1;
                entity.Prop2 = 1;
                entity.AssociatedEntities = new AssociatedEntity[]{
                new AssociatedEntity {
                    ID = Guid.NewGuid(),
                    Prop1 = entity.Prop1,
                    Prop2 = entity.Prop2,
                    Name = "Ming1"
                },
                new AssociatedEntity {
                    ID = Guid.NewGuid(),
                    Prop1 = entity.Prop1,
                    Prop2 = entity.Prop2,
                    Name = "Ming2"
                }
    
                };
                return entity;
            }
        }
        public class POCOEntity
        {
            [Key]
            public Guid ID { get; set; }
            public int Prop1 { get; set; }
            public int Prop2 { get; set; }
    
            [Include]
            [Association("POCOEntity_AssociatedEntity", "Prop1,Prop2", "Prop1,Prop2")]
            public ICollection<AssociatedEntity> AssociatedEntities { get; set; }
        }
        public class AssociatedEntity
        {
            [Key]
            public Guid ID { get; set; }
            public int Prop1 { get; set; }
            public int Prop2 { get; set; }
            public string Name { get; set; }
        }
    }

    2.Code in a silverlight page(.xaml file):

    <Grid x:Name="LayoutRoot">
            <StackPanel>
                <Button Name="btn" Content="Load data to DataGrid" Margin="20" Width="150" Height="30" Click="btn_Click" />
                <sdk:DataGrid Name="datagrid1" Width="400" Height="300" />
            </StackPanel>
        </Grid>

    3.Code in the silverlight page(.xaml.cs file):

     private LoadOperation<POCOEntity> loadOp;
            private void btn_Click(object sender, RoutedEventArgs e)
            {
                this.Cursor = Cursors.Wait;
                POCOContext con = new POCOContext();
                loadOp = con.Load(con.GetStandardEntityQuery());
                loadOp.Completed += new EventHandler(loadOp_Completed);
            }
            void loadOp_Completed(object sender, EventArgs e)
            {
                IEnumerable<POCOEntity> data = loadOp.Entities;
                ObservableCollection<POCOEntity> list = new ObservableCollection<POCOEntity>(data);

                // Here I use a datagrid control to display the value of POCOEntity.AssociatedEntities property.
                this.datagrid1.ItemsSource = list[0].AssociatedEntities;
                this.Cursor = Cursors.Arrow;
            }

    The sample project works fine at my side ,thus please check the sample project above and see whether it helps.

    In addition ,I would recommend that you can also refer the link below for an article which explains how to change the mouse cursor:

    http://blogs.silverlight.net/blogs/msnow/archive/2008/08/14/tip-of-the-day-27-how-to-change-the-mouse-cursor.aspx

    Hope it can help you.

    Sunday, August 14, 2011 10:29 PM
  • Hi Ming,

    Thanks for the sample code.  I was familiar with that way of changing the cursor.  I was having a problem because I was initiating the long operation with a button that belongs to another user control on the main page, and I was changing the cursor in another usercontrol where the DataGrid is hosted.  So what changes is not the Button.Cursor but the DataGrid.Cursor, and does not become apparent until I move the mouse to the DataGrid, leading me to believe that I had to move the cursor to show the change which was not correct.  The code change is from

    this.Cursor = Cursors.Wait

    to

    this.GetParent(typeof(MainPage).Cursor = Cursors.Wait.

    which changes, in effect, the cursor of the whole main page.  So the cursor change is visible whereever the mouse is on the page.

     

    Thanks to everyone for your answers.

    Wednesday, August 17, 2011 1:30 PM