zoukankan      html  css  js  c++  java
  • WPF获取和设置鼠标位置与progressbar的使用方法

    一、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++;
            }
    复制代码
  • 相关阅读:
    矩阵Frobenius范数、核范数与奇异值的关系
    范数与正则化
    对偶上升法,增光拉格朗日乘子法,交替方向乘子法
    拉格朗日函数,拉格朗日对偶函数,KKT条件
    relint, 相对内点集的理解
    转:Mac 弹出App can’t be opened because Apple cannot check it for malicious software的解决方法
    数组分块1
    fzu 2275 Game [第八届福建省大学生程序设计竞赛 Problem D]
    fzu 2275 Game [第八届福建省大学生程序设计竞赛 Problem D]
    Plug-in CodeForces
  • 原文地址:https://www.cnblogs.com/jameslif/p/3209961.html
Copyright © 2011-2022 走看看