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获取和设置鼠标位置与progressbar的使用方法的更多相关文章
- WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法
一.WPF 中获取和设置鼠标位置 方法一:WPF方法 Point p = Mouse.GetPosition(e.Source as FrameworkElement); Point p = (e.S ...
- 在WPF里面实现以鼠标位置为中心缩放移动图片
原文:在WPF里面实现以鼠标位置为中心缩放移动图片 在以前的文章使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果里面,介绍了如何在WPF里面移动和放大缩小图片, ...
- javascript获取以及设置光标位置
一. 获取光标位置: // 获取光标位置 function getCursortPosition (textDom) { var cursorPos = 0; if (document.selecti ...
- WPF获取和设置应用程序范围的资源
设置资源: Application.Current.Resources["ApplicationScopeResource"] = Brushes.White; 使用代码获取资源: ...
- OpenCV获取与设置像素点的值的几个方法
Title: OpenCV OpenCV像素值的获取与设置 Fn 1 : 使用 Mat 中对矩阵元素的地址定位的知识 (参考博文:OpenCV中对Mat里面depth,dims,channels,st ...
- unity获取ugui上鼠标位置
public class GetMousePos : MonoBehaviour { public Canvas canvas;//画布 private RectTransform rectTrans ...
- $(window).scrollTop() 获取当前的鼠标位置 offset.left()指定标签在html中的坐标 offset.top() 指定标签在html中的坐标position() 指定标签相对父(relative)标签的坐标
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里?
原文:WPF 程序鼠标在窗口之外的时候,控件拿到的鼠标位置在哪里? 在 WPF 程序中,我们有 Mouse.GetPosition(IInputElement relativeTo) 方法可以拿到鼠标 ...
- 使用C#模拟键盘输入、鼠标移动和点击、设置光标位置及控制应用程序的显示
1.模拟键盘输入(SendKeys) 功能:将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样. 语法:SendKeys.Send(string keys);SendKeys.SendWa ...
随机推荐
- remote debug
https://docs.microsoft.com/en-us/visualstudio/debugger/remote-debugging https://docs.microsoft.com/e ...
- nyoj--16--矩形嵌套(动态规划)
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a< ...
- 前后端分离之接口登陆权限token
随着业务的需求普通的springmvc+jsp已经不能满足我们的系统了,会逐渐把后台和前端展示分离开来,下面我们就来把普通的springmvc+jsp分为 springmvc只提供rest接口,前端用 ...
- Git 学习笔记(三)
我记得最初学习的时候我提到了使用版本控制软件的最大好处是让你可以永远后悔,那么如何吃后悔药呢?在项目过程中我们很有可能因为各种因素对我们的操作进行回滚,对于传统的版本控制系统来说,并不复杂,拿 SVN ...
- 读书笔记第三周 人月神话 刘鼎乾 PB16070837
读书笔记第三周:人月神话 这本书主要讲述了如何管理一个软件开发团队的问题,其中如何提高团队的效率可以说是本书的重点之一了.感觉这本书地中文版翻译得比较晦涩,很多表达比较模糊,看起来有些吃力,因此下 ...
- cache(缓存)的作用
cache的作用: 连接文件.内存与应用,为信息流在三者之间流动提供通道: 存储管理:对外与对内: 存取效率: 多线程: 一次存储:分批存储? 系统的缓存控制机制(虚拟内存)使用分段分页与命中机制. ...
- Grand Central Dispatch-thread pool pattern
Grand Central Dispatch (GCD) is a technology developed by Apple Inc. to optimize application support ...
- Pyhton学习——Day59
参考博客: http://www.cnblogs.com/wupeiqi/articles/6144178.html Form 1. 验证 2. 生成HTML(保留上次输入内容) 3. 初始化默认是 ...
- ./configure --prefix /?/? 解释
-- ./configure :编译源码 --prefix :把所有资源文件放在 之后的路径之后
- SpringBoot下支付宝接口的使用
SpringBoot下支付宝接口的使用 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> < ...