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 ...
随机推荐
- UVA 11542 Square 高斯消元 异或方程组求解
题目链接:点击打开链接 白书的例题练练手. . . P161 #include <cstdio> #include <iostream> #include <algori ...
- asp.net web site中reference的version的autoupdate
https://stackoverflow.com/questions/833924/visual-studio-stop-auto-update-on-references This is vali ...
- Linux就该这么学 20181009(第十二章 SAMBA)
参考链接https://www.linuxprobe.com Samba 跨平台的文件共享 linux-linux linux-windows /etc/samba/smb.conf 里面 []这个名 ...
- java类List及List遍历器的代码
从某个程序中截取的一个示例代码: List<User> users = userDao.selectAll(); //mybatis java orm Iterator<User&g ...
- SQLiteHelp
using System; using System.Collections.Generic; using System.Text; using System.Data.SQLite; using S ...
- java同步锁的正确使用
同步锁分类 对象锁(this) 类锁(类的字节码文件对象即类名.class) 字符串锁(比较特别) 应用场景 在多线程下对共享资源的安全操作. 需求:启动5个线程对共享资源total进行安全操作. 同 ...
- vue路由钩子拦截器beforeEach和afterEach及页面路由变化路由监听
在路由跳转的时候,我们需要一些权限判断或者其他操作.这个时候就需要使用路由的钩子函数. 定义:路由钩子主要是给使用者在路由发生变化时进行一些特殊的处理而定义的函数. 总体来讲vue里面提供了三大类钩子 ...
- IntelliJ Idea使用代码折叠
VS风格: //region xxxxxxxxxxxxxx ............. //endregion 快捷键: Ctrl+Shift+”+/-”,全部展开.折叠
- 苹果操作系统名称演变史 新名称macOS
历史回顾 发布年代 名称 序号 1994-1999 Classic Mac OS 1-9 2001-2011 Mac OS X 10.0-Lion 2012-2015 OS X Mountain Li ...
- 《图解HTTP》摘要
网络基础TCP/IP 使用Cookie进行状态管理 HTTP首部 确保Web安全的HTTPS 1.网络基础TCP/IP 2.使用Cookie进行状态管理:HTTP是无状态协议. 3.HTTP首部 HT ...