wpf利用线程制作初始界面和关闭窗体特效

1.首先定义初始窗体,和主窗体。

  初始窗体(StartWindow) 主窗体(MainWindow):

2.在主窗体界面中,加载初始窗体。注意在线程中操作UI元素需要使用BeginInvoke或者Invoke方法。

  

        StartWindow start;

        public MainWindow()

        {

            InitializeComponent();

            Thread thread = new Thread(LoadResource);

            thread.Start();

            this.Visibility = Visibility.Hidden;

            start = new StartWindow();

            start.closeTheWindows += start_closeTheWindows;

            start.Show();

        }



        /// <summary>

        /// 窗体start关闭之后执行

        /// </summary>

        private void start_closeTheWindows()

        {

            this.Visibility = Visibility.Visible;

        }



        /// <summary>

        /// 执行加载资源等操作

        /// </summary>

        /// <param name="obj"></param>

        private void LoadResource(object obj)

        {

            Thread.Sleep(3000);

            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate()

            {

                start.Close();

            });

        }
MainWindow.xaml.cs

 

3.在startWindow中用到窗体关闭特效,定义了委托,在关闭之后显示主窗体。

 

        /// <summary>

        /// 重写OnCloseIng,关闭特效

        /// </summary>

        bool _closinganimation = true;

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)

        {

            e.Cancel = _closinganimation;

            _closinganimation = false;

            base.OnClosing(e);



            System.Windows.Media.Animation.Storyboard sb = new System.Windows.Media.Animation.Storyboard();

            System.Windows.Media.Animation.DoubleAnimation dh = new System.Windows.Media.Animation.DoubleAnimation();

            System.Windows.Media.Animation.DoubleAnimation dw = new System.Windows.Media.Animation.DoubleAnimation();

            System.Windows.Media.Animation.DoubleAnimation dop = new System.Windows.Media.Animation.DoubleAnimation();

            dop.Duration = dh.Duration = dw.Duration = sb.Duration = new Duration(new TimeSpan(0, 0, 2));

            dop.To = dh.To = dw.To = 0;

            System.Windows.Media.Animation.Storyboard.SetTarget(dop, this);

            System.Windows.Media.Animation.Storyboard.SetTarget(dh, this);

            System.Windows.Media.Animation.Storyboard.SetTarget(dw, this);

            System.Windows.Media.Animation.Storyboard.SetTargetProperty(dop, new PropertyPath("Opacity", new object[] { }));

            System.Windows.Media.Animation.Storyboard.SetTargetProperty(dh, new PropertyPath("Height", new object[] { }));

            System.Windows.Media.Animation.Storyboard.SetTargetProperty(dw, new PropertyPath("Width", new object[] { }));

            sb.Children.Add(dh);

            sb.Children.Add(dw);

            sb.Children.Add(dop);



            sb.Completed += (a, b) => { this.Close(); };

            sb.Begin();

        }
StartWindow关闭特效代码

 

        /// <summary>

        /// 关闭子窗体委托

        /// </summary>

        public delegate void CloseThis();

        public StartWindow()

        {

            InitializeComponent();

        }



        public event CloseThis closeTheWindows;

        private void Window_Closed(object sender, EventArgs e)

        {

            closeTheWindows();

        }
定义委托代码

 

关闭代码下载与CSDN资源:http://download.csdn.net/detail/daixin1031017817/5501187

 

你可能感兴趣的:(WPF)