WPF获取和设置鼠标位置与progressbar的使用方法
2013-07-24 09:51
225 查看
一、WPF中获取和设置鼠标位置
方法一:WPF方法
方法二:API方法
二、WPF中实现实时更新progressbar
实现实时更新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); } }
二、WPF中实现实时更新progressbar
实现实时更新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备忘录(2)WPF获取和设置鼠标位置与progressbar的使用方法
- WPF获取鼠标点击位置和API使用
- 使用 window.getSelection() 方法获取鼠标划取部分的起始位置和结束位置的问题
- WPF设置窗体可以使用鼠标拖动大小的方法
- 在不使用JFrame的情况下,获取鼠标桌面位置的最(zui)小(er)方法
- WPF 中获取和设置鼠标位置
- 使用JS获取当前地理位置方法汇总(如用谷歌接口,会出再以上报错,必须申请密钥并设置接受服务器IP!!!)
- jQ获取目标div中鼠标的位置信息的俩种方法
- WPF中获取鼠标相对于屏幕的位置
- JS获取鼠标相对位置的方法
- WPF获取控件位置的方法
- WPF获得鼠标位置的方法
- 用 javascript 获取当页面上鼠标(光标)位置 和 触发事件的对象 的方法
- javascript 获取鼠标当前位置的通用方法
- Javascript 获取鼠标当前的位置实现方法
- js获取鼠标位置的各种方法
- H5响应式设计中使用JQ的offset,positon方法获取位置不准的解决方法
- WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)
- 使用jQuery的animate()方法让滚动条滚动到指定的位置,同时设置滚动的速度
- 基于JavaScript实现 获取鼠标点击位置坐标的方法