程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF學習備忘(2)WPF獲取和設置鼠標位置與progressbar的使用方法

WPF學習備忘(2)WPF獲取和設置鼠標位置與progressbar的使用方法

編輯:關於.NET

一、WPF 中獲取和設置鼠標位置

方法一:WPF方法

Point p = Mouse.GetPosition

(e.Source as FrameworkElement);
     
  Point p = (e.Source as FrameworkElement).PointToScreen(pp);

方法二: API方法

/// <summary>   
        /// 設置鼠標的坐標   
        /// </summary>   
        /// <param name="x">橫坐標</param>   
        /// <param name="y">縱坐標</param>          
     
        [DllImport("User32")]
     
        public extern static void SetCursorPos(int x, int y);
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
     
        }
     
        /// <summary>   
        /// 獲取鼠標的坐標   
        /// </summary>   
        /// <param name="lpPoint">傳址參數,坐標point類型</param>   
        /// <returns>獲取成功返回真</returns>   
     
     
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);
     
     
        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            POINT p = new POINT();
            if (GetCursorPos(out p))//API方法
            {
                txtStat.Text = string.Format("X:{0}   Y:{1}", p.X, p.Y);
            }
        }

二、 WPF中實現實時更新progressbar

實現實時更新ProgressBar貌似有很多方法 ,我搜索的很多資料都要用線程,覺得還是有點兒麻煩,最後在國外的技術論壇上看到

一個用代 理解決的方法,下面就是我的調試過程:

private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);
     
     
        private void Process()
        {
            //Configure the ProgressBar
            ProgressBar1.Minimum = 0;
            ProgressBar1.Maximum = short.MaxValue;
            ProgressBar1.Value = 0;
     
 //Stores the value of the ProgressBar
            double value = 0;
     
     
            UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar1.SetValue);
     
 //Tight Loop: Loop until the ProgressBar.Value reaches the max
            do
            {
                value += 1;
     
     
                Dispatcher.Invoke(updatePbDelegate, 
                    System.Windows.Threading.DispatcherPriority.Background, 
                    new object[] { ProgressBar.ValueProperty, value });
     
 }
            while (ProgressBar1.Value != ProgressBar1.Maximum);
     
 }

前台:

<ProgressBar Grid.Row="1" Height="20" Width="200" Margin="0,4,0,0"  

 Name="ProgressBar1" HorizontalAlignment="Center"  VerticalAlignment="top"  />

效果:

方法二:使用定時器

public Window1()
        {
            InitializeComponent();
     
            DispatcherTimer _mainTimer = new DispatcherTimer();
            _mainTimer.Interval = TimeSpan.FromSeconds(1);
            _mainTimer.Tick += new EventHandler(_mainTimer_Tick);
            _mainTimer.IsEnabled = true;
     
        }
void _mainTimer_Tick(object sender, EventArgs e)
        {
            if (progressBar2.Value == progressBar1.Maximum)
                progressBar2.Value = 0;
     
            progressBar2.Value++;
        }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved