WPF备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法
2013-07-23 09:15
691 查看
一、WPF中获取和设置鼠标位置
方法一:WPF方法
方法二:API方法
[b]二、WPF中实现实时更新progressbar[/b]
实现实时更新ProgressBar貌似有很多方法,我搜索的很多资料都要用线程,觉得还是有点儿麻烦,最后在国外的技术论坛上看到
一个用代理解决的方法,下面就是我的调试过程:
privatedelegatevoidUpdateProgressBarDelegate(System.Windows.DependencyPropertydp,
Objectvalue);
privatevoidProcess()
{
//ConfiguretheProgressBar
ProgressBar1.Minimum=0;
ProgressBar1.Maximum=short.MaxValue;
ProgressBar1.Value=0;
//StoresthevalueoftheProgressBar
doublevalue=0;
UpdateProgressBarDelegateupdatePbDelegate=newUpdateProgressBarDelegate(ProgressBar1.SetValue);
//TightLoop:LoopuntiltheProgressBar.Valuereachesthemax
do
{
value+=1;
Dispatcher.Invoke(updatePbDelegate,
System.Windows.Threading.DispatcherPriority.Background,
newobject[]{ProgressBar.ValueProperty,value});
}
while(ProgressBar1.Value!=ProgressBar1.Maximum);
}
前台:
效果:
方法二:使用定时器
方法一:WPF方法
Pointp=Mouse.GetPosition(e.SourceasFrameworkElement); Pointp=(e.SourceasFrameworkElement).PointToScreen(pp);
方法二:API方法
///<summary> ///设置鼠标的坐标 ///</summary> ///<paramname="x">横坐标</param> ///<paramname="y">纵坐标</param> [DllImport("User32")] publicexternstaticvoidSetCursorPos(intx,inty); publicstructPOINT { publicintX; publicintY; publicPOINT(intx,inty) { this.X=x; this.Y=y; } } ///<summary> ///获取鼠标的坐标 ///</summary> ///<paramname="lpPoint">传址参数,坐标point类型</param> ///<returns>获取成功返回真</returns> [DllImport("user32.dll",CharSet=CharSet.Auto)] publicstaticexternboolGetCursorPos(outPOINTpt); privatevoidWindow_MouseMove(objectsender,MouseEventArgse) { POINTp=newPOINT(); if(GetCursorPos(outp))//API方法 { txtStat.Text=string.Format("X:{0}Y:{1}",p.X,p.Y); } }
[b]二、WPF中实现实时更新progressbar[/b]
实现实时更新ProgressBar貌似有很多方法,我搜索的很多资料都要用线程,觉得还是有点儿麻烦,最后在国外的技术论坛上看到
一个用代理解决的方法,下面就是我的调试过程:
privatedelegatevoidUpdateProgressBarDelegate(System.Windows.DependencyPropertydp,
Objectvalue);
privatevoidProcess()
{
//ConfiguretheProgressBar
ProgressBar1.Minimum=0;
ProgressBar1.Maximum=short.MaxValue;
ProgressBar1.Value=0;
//StoresthevalueoftheProgressBar
doublevalue=0;
UpdateProgressBarDelegateupdatePbDelegate=newUpdateProgressBarDelegate(ProgressBar1.SetValue);
//TightLoop:LoopuntiltheProgressBar.Valuereachesthemax
do
{
value+=1;
Dispatcher.Invoke(updatePbDelegate,
System.Windows.Threading.DispatcherPriority.Background,
newobject[]{ProgressBar.ValueProperty,value});
}
while(ProgressBar1.Value!=ProgressBar1.Maximum);
}
前台:
<ProgressBarGrid.Row="1"Height="20"Width="200"Margin="0,4,0,0"Name="ProgressBar1"HorizontalAlignment="Center"VerticalAlignment="top"/>
效果:
方法二:使用定时器
publicWindow1()
{
InitializeComponent();
DispatcherTimer_mainTimer=newDispatcherTimer();
_mainTimer.Interval=TimeSpan.FromSeconds(1);
_mainTimer.Tick+=newEventHandler(_mainTimer_Tick);
_mainTimer.IsEnabled=true;
}
void_mainTimer_Tick(objectsender,EventArgse)
{
if(progressBar2.Value==progressBar1.Maximum)
progressBar2.Value=0;
progressBar2.Value++;
}
相关文章推荐
- WPF获取和设置鼠标位置与progressbar的使用方法
- WPF获取鼠标点击位置和API使用
- WPF 中获取和设置鼠标位置
- 使用JS获取当前地理位置方法汇总(如用谷歌接口,会出再以上报错,必须申请密钥并设置接受服务器IP!!!)
- WPF设置窗体可以使用鼠标拖动大小的方法
- 使用 window.getSelection() 方法获取鼠标划取部分的起始位置和结束位置的问题
- 在不使用JFrame的情况下,获取鼠标桌面位置的最(zui)小(er)方法
- Android获取当前位置的三种方式及其使用方法
- 用 javascript 获取当页面上鼠标(光标)位置 和 触发事件的对象 的方法
- wpf 处理获取鼠标点击方法
- 使用getBoundingClientRect()方法获取元素距离浏览器周边的位置
- 百度地图中使用mouseover事件获取经纬度时无法拿到鼠标所在位置的经纬度。
- WPF获取鼠标的位置
- jQ获取目标div中鼠标的位置信息的俩种方法
- WPF获取控件位置的方法
- 获取鼠标图片和鼠标位置的方法
- GridView的getChildAt(postion)获取不到指定位置的item的解决方法(动态设置指定item为选中状态)
- WPF中获取鼠标相对于屏幕的位置
- 获取并设置鼠标位置 C语言
- Android使用动画设置ProgressBar进度的方法