简体   繁体   中英

How to change the window position in wpf on a mouse click

I need to change the position of a window on mouse click. here is the code.

private void Button_Click(object sender, RoutedEventArgs e)
{
    for(int i=0; i<50; i++)
    {
        this.Top -= i; 
        this.Left -= i;
    } 
}

But whenever i run this program only the last position is shown. My intention is to move it continuosly till the end of loop.

Finally i found the answer myself. It s working perfectly as i expected. I used SynchronizationContext which can post Actions to update controls on UI thread.

    public partial class Splash : Window
    {
        SynchronizationContext sc;
        System.Timers.Timer t;
        double i=0;
        double tempTop;
        double angle = 0;
        public Splash()
        {
            InitializeComponent();
            sc=SynchronizationContext.Current;
        }
        private void Move(object sender, MouseEventArgs e)
        {
            DragMove();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnMinim_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

                l1.Content = "Helicopter Moving";
            if(t!=null)
            {
                t.Stop();
                t.Dispose();
            }
                //for (double i = 0; i < 1; i += 0.05)
                //{
                //    this.Top -= i;
                //    this.Left -= i;
                //    Thread.Sleep(100);
                //}
                //l1.Content = "Helicopter Stopped";
                tempTop = this.Top;
                t = new System.Timers.Timer();
                t.Interval = 10;
                t.Enabled = true;
                t.Elapsed += Change;
                t.Start();

        }
        void Change(object sender, EventArgs e)
        {
            if (i <= 3)
            {
                sc.Post(o =>
                {
                    this.Top = tempTop * (Math.Cos(Math.PI * angle / 180));
                    this.Left -= i;
                    angle = (angle >= 360) ? 0 : ++angle;
                    i = i + 0.01;
                }, null);
            }
            else
            {
                t.Stop();
                i = i * -1;
            }

        }
    }
}

Try this should work Thread.Sleep will not work for you as its a UI thread. You need timer to make this work

Timer t; 
private void Button_Click(object sender, RoutedEventArgs e)
{

    i=0;
    if(t!=null)
    { 
       t.Stop();
       t.Dispose();
    }
    t = new Timer();
    t.Interval = 800;
    t.Enabled = true;
    t.Tick += T_Tick;
    t.Start();                                                    

}
int i=0;
private static void T_Tick(object sender, EventArgs e)
{
   if(i<=50)
   {
        this.Top -= i; 
        this.Left -= i;
        i++;
    }
    else
     t.Stop();   
}        

Just start an animation when your click event triggers. You can define how long should the animation last.

Basic benefit of using animations instead of doing calculations manually is animations being run in separated thread, so you don't loose application's responsiveness.
What is more, you can edit your animations in separated tools, such as Blend without the need to verifying animations in runtime.

Few sources: http://www.wpf-tutorial.com/styles/trigger-animations-enteractions-exitactions/ http://dotnetslackers.com/articles/wpf/IntroductionToWPFAnimations.aspx http://www.wpftutorial.net/Animation.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM