关于silverlight程序中border(可以是其他的)控件缓慢移动的程序编写
我在网上下了一个程序,但是我并不能完全理解,希望高手能帮帮忙,做一下注释,特别是private void Clock_Tick(object sender, EventArgs e)函数,其次是越详细越好,代码如下:
public partial class MainPage : UserControl
{
private Point mouseStart; //鼠标开始移动的位置
private Point mouseNow; //鼠标现在的所在位置
private DispatcherTimer clock; //定义一个计时器
private bool isMouseDown; //是否按下了鼠标
double left = 0.0;
public MainPage()
{
InitializeComponent();
}
//图片开始移动
private void border_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
if (!this.clock.IsEnabled)
{
this.clock.Start(); //计时开始
}
this.mouseNow = e.GetPosition(this); //获取当前位置
}
}
private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;
border.CaptureMouse();
mouseStart = mouseNow = e.GetPosition(this);
if (this.clock == null)
{
this.clock = new DispatcherTimer();
this.clock.Interval = new TimeSpan(0, 0, 0, 0, 10);
this.clock.Tick += new EventHandler(Clock_Tick);
clock.Start();
}
else
{
clock.Stop();
clock.Start();
}
}
private void border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
border.ReleaseMouseCapture(); //移除鼠标扑获
isMouseDown = false;
}
private void Clock_Tick(object sender, EventArgs e)
{
const double friction = 0.15;
left+=((this.mouseNow.X - this.mouseStart.X) * friction) / 2.2;
border.SetValue(Canvas.LeftProperty, left);
this.mouseStart.X += ((this.mouseNow.X - this.mouseStart.X) * friction);
this.mouseStart.Y += ((this.mouseNow.Y - this.mouseStart.Y) * friction);
if ((Math.Abs(this.mouseNow.X - this.mouseStart.X) < 2) && (Math.Abs(this.mouseNow.Y - this.mouseStart.Y) < 2))
{
this.clock.Stop();
}
}
}
[解决办法]
private void Clock_Tick(object sender, EventArgs e) { const double friction = 0.15; //定义常量 left+=((this.mouseNow.X - this.mouseStart.X) * friction) / 2.2; //设置左边的定位位置 border.SetValue(Canvas.LeftProperty, left);//设置 border的属性 第一个参数Canvas.LeftProperty 是Canvas这个空间 LeftProperty 左边的属性 第二参数 是 值 意思是 定位 border在Canvas容器中的位置 this.mouseStart.X += ((this.mouseNow.X - this.mouseStart.X) * friction);//x坐标运算 this.mouseStart.Y += ((this.mouseNow.Y - this.mouseStart.Y) * friction);//y坐标运算 if ((Math.Abs(this.mouseNow.X - this.mouseStart.X) < 2) && (Math.Abs(this.mouseNow.Y - this.mouseStart.Y) < 2))//条件判断 { this.clock.Stop();//计时器停止 } }
[解决办法]
public partial class MainPage : UserControl
{
private Point mouseStart; //鼠标开始移动的位置
private Point mouseNow; //鼠标现在的所在位置
private DispatcherTimer clock; //定义一个计时器
private bool isMouseDown; //是否按下了鼠标
double left = 0.0;
public MainPage()
{
InitializeComponent();
}
//图片开始移动
private void border_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
if (!this.clock.IsEnabled)
{
this.clock.Start(); //计时开始
}
this.mouseNow = e.GetPosition(this); //获取当前位置
}
}
private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;//设置标志,当前鼠标已经按下
border.CaptureMouse();//启用对象的鼠标捕获
mouseStart = mouseNow = e.GetPosition(this);//获取鼠标当前的位置
if (this.clock == null)//如果Timer为空则新建一个Timer,并且为Timer绑定事件
{
this.clock = new DispatcherTimer();
this.clock.Interval = new TimeSpan(0, 0, 0, 0, 10);//10毫秒执行一次
this.clock.Tick += new EventHandler(Clock_Tick);//注册事件
clock.Start();//开始
}
else
{
clock.Stop();//如果检测到不为空则停止,并且进行下一次启动
clock.Start();
}
}
private void border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
border.ReleaseMouseCapture(); //移除鼠标扑获
isMouseDown = false;
}
private void Clock_Tick(object sender, EventArgs e)
{
const double friction = 0.15;//常量
left += ((this.mouseNow.X - this.mouseStart.X) * friction) / 2.2;//获取左侧偏移量
border.SetValue(Canvas.LeftProperty, left);//设置当前偏移量位置,让Border动起来
this.mouseStart.X += ((this.mouseNow.X - this.mouseStart.X) * friction);//不断增加
this.mouseStart.Y += ((this.mouseNow.Y - this.mouseStart.Y) * friction);
if ((Math.Abs(this.mouseNow.X - this.mouseStart.X) < 2) && (Math.Abs(this.mouseNow.Y - this.mouseStart.Y) < 2))
{
this.clock.Stop();//在某一些情况下Timer停止
}
}
}