Windows Phone 7中实现图片的动态加载和移动
时间:2012-11-03 16:50来源:百度空间 作者:lhaix 点击:334次
public partial class slide:PhoneApplicationPage { Imageimg; PointimgLocate; Pointprevious= new Point(-100,-100); public slide() { InitializeComponent(); init(); } private void button1_Click( object sender,RoutedEventArgse) { NavigationService.GoBack(); }
public partial class slide : PhoneApplicationPage
{
Image img;
Point imgLocate;
Point previous = new Point(-100,-100);
public slide()
{
InitializeComponent();
init();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
}
private void init(){
img = new Image();
img.Source = new BitmapImage(new Uri("../img/1.gif", UriKind.Relative));
Canvas.SetLeft(img, 100);
Canvas.SetTop(img, 100);
this.canvasObj.Children.Add(img);
imgLocate = new Point(100,100);
}
private void canvasObj_MouseMove(object sender, MouseEventArgs e)
{
//当拖动鼠标时,首先获取当前坐标值。
double X = e.GetPosition(canvasObj).X;
double Y = e.GetPosition(canvasObj).Y;
//计算当前坐标与鼠标上一次的位置之间的差距。
double deltaX = X - previous.X;
double deltaY = Y - previous.Y;
//将当前鼠标位置保存。
previous = new Point(X, Y);
imgLocate.X += deltaX;
imgLocate.Y += deltaY;
img.SetValue(Canvas.LeftProperty, imgLocate.X);
img.SetValue(Canvas.TopProperty, imgLocate.Y);
}
private void canvasObj_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
previous.X = e.GetPosition(canvasObj).X;
previous.Y = e.GetPosition(canvasObj).Y;
}
}
{
Image img;
Point imgLocate;
Point previous = new Point(-100,-100);
public slide()
{
InitializeComponent();
init();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
}
private void init(){
img = new Image();
img.Source = new BitmapImage(new Uri("../img/1.gif", UriKind.Relative));
Canvas.SetLeft(img, 100);
Canvas.SetTop(img, 100);
this.canvasObj.Children.Add(img);
imgLocate = new Point(100,100);
}
private void canvasObj_MouseMove(object sender, MouseEventArgs e)
{
//当拖动鼠标时,首先获取当前坐标值。
double X = e.GetPosition(canvasObj).X;
double Y = e.GetPosition(canvasObj).Y;
//计算当前坐标与鼠标上一次的位置之间的差距。
double deltaX = X - previous.X;
double deltaY = Y - previous.Y;
//将当前鼠标位置保存。
previous = new Point(X, Y);
imgLocate.X += deltaX;
imgLocate.Y += deltaY;
img.SetValue(Canvas.LeftProperty, imgLocate.X);
img.SetValue(Canvas.TopProperty, imgLocate.Y);
}
private void canvasObj_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
previous.X = e.GetPosition(canvasObj).X;
previous.Y = e.GetPosition(canvasObj).Y;
}
}
本文来自lhaix的博客,原文地址:http://hi.baidu.com/lhaix/blog/item/bf8ce5dffd9f390d49540316.html