WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法

简介: 原文:WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法一、WPF 中获取和设置鼠标位置   方法一:WPF方法   Point p = Mouse.GetPosition(e.
原文: WPF备忘录(2)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++;
        }

 

 

 

目录
相关文章
WPF界面异常:未将对象引用设置到对象实例
WPF界面异常:未将对象引用设置到对象实例
WPF TreeView设置所有节点默认展开
WPF TreeView设置所有节点默认展开
WPF中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
WPF中给TextBox/TextBlock设置提示文本
|
前端开发 C# Windows
WPF鼠标、键盘、拖拽事件、用行为封装事件
本文主要介绍了WPF中常用的鼠标事件、键盘事件以及注意事项,同时使用一个案例讲解了拓展事件。除此之外,本文还讲述如何用行为(Behavior)来封装事件。
|
C# 索引 容器
WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同时增加鼠标的悬停效果。
1733 0
|
C# 图形学
在WPF里面实现以鼠标位置为中心缩放移动图片
原文:在WPF里面实现以鼠标位置为中心缩放移动图片 在以前的文章使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果里面,介绍了如何在WPF里面移动和放大缩小图片,程序也支持使用滚轮的方式缩放图片。
1597 0
|
C#
设置WPF输入框焦点
原文:设置WPF输入框焦点 在WPF中设置控件键盘焦点 Keyboard.Focus(/*控件名称*/);
1263 0
|
C# 编译器 数据格式
WPF备忘录(7)WPF图片资源路径介绍
原文:WPF备忘录(7)WPF图片资源路径介绍 在项目中增加两张图片Content.jpg和Resource.jpg,分别将其生成操作属性设置为Content和Resource。     在界面中增加两个Image控件ImgContent和ImgResource,在XAML中分别设置Source路径为Content.jpg和Resource.jpg。
910 0
|
前端开发 C#
WPF备忘录(1)有笑脸,有Popup
原文:WPF备忘录(1)有笑脸,有Popup 1.画个笑脸给大家娱乐一下: 效果如下:   2.
905 0