901217-如何在Silverlight中平移? [英] 901217 - how to pan in Silverlight?

查看:60
本文介绍了901217-如何在Silverlight中平移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我在UserControl中有一个图像,其中的源是BitmapImage.我需要平移图像控件中显示的内容.我怎样才能做到这一点?换句话说,如何创建另一个BitmapImage源,其内容是原始BitmapImage的偏移,或者如何在Image中绘制当前BitmapImage的偏移版本?

解决方案

这里是有关如何开始使用此方法的想法

 <     ="   ImageCanvas"  MouseMove   ="     ="   Canvas_MouseDown"  MouseLeftButtonUp   ="  > 
  <  图片    ="   moveImage"   . ..." / <  /画布 >  

在这里,我们创建了一个Canvas,我们将使用它来平移图像.接下来,我们需要在后面的代码中连接一些代码.如果这似乎不是MVVM,请不要担心-MVVM的意义不在于从后面的代码中抽象显示代码:

 Point mousePos =  Point();

 double  moveAmountX =  1 ;
 double  moveAmountY =  1 ;

 bool  isMouseDown =  false ;
私有 无效 Canvas_MouseMove(对象发​​件人,MouseEventArgs e)
{
  如果(!isMouseDown)返回;
  currentPos = e.GetPosition(ImageCanvas);
  如果(mousePos.X >  currentPos.X)
  {
    moveAmountX--;
  }
  其他 如果(mousePos.X <  currentPos.X)
  {
    moveAmountX ++;
  }
  如果(mousePos.Y >  currentPos.Y)
  {
    moveAmountY--;
  }
  其他 如果(mousePos.Y <  currentPos.Y)
  {
    moveAmountY ++;
  }
  moveImage.SetValue(Canvas.LeftProperty,moveAmountX);
  moveImage.SetValue(Canvas.TopProperty,moveAmountY);
  mousePos = currentPos;
}

私有 无效 Canvas_MouseDown(对象发​​件人,MouseEventArgs e)
{
  mousePos = e.GetPosition(ImageCanvas);
  isMouseDown =  true ;
}

私有 无效 Canvas_MouseUp(对象发​​件人,MouseEventArgs e)
{
  isMouseDown =  false ;
} 

在此,我们在MouseLeftButtonUp事件中捕获了鼠标,并在MouseLeftButtonUp事件中释放了鼠标.最后,我们比较鼠标移动中的鼠标位置并相应地调整图像.我刚刚在Code Project的编辑器中将其组合在一起,因此您可能需要调整测试的方向,但这应该可以帮助您开始.


hi i''ve an Image in the UserControl which Source is a BitmapImage. i need to pan what is shown in the Image control. how can i do that? in another words, how can i create another BitmapImage source which content be a shift of the original BitmapImage or how can i draw in the Image a shifted version of what is currently in BitmapImage?

解决方案

Here''s an idea of how to get started with this

<Canvas x:Name="ImageCanvas" MouseMove="Canvas_MouseMove" MouseLeftButtonDown="Canvas_MouseDown" MouseLeftButtonUp="Canvas_MouseUp">
  <Image x:Name="moveImage" Source="...." />
</Canvas>

Here we''ve created a Canvas that we are going to use to pan the image around. Next we need to hook up to some code in the code behind. Don''t worry if this doesn''t seem to be MVVM - the point of MVVM is not to abstract display code from the code behind:

Point mousePos = new Point();
Point currentPos = new Point();

double moveAmountX = 1;
double moveAmountY = 1;

bool isMouseDown = false;
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
  if (!isMouseDown) return;
  currentPos = e.GetPosition(ImageCanvas);
  if (mousePos.X > currentPos.X)
  {
    moveAmountX--;
  }
  else if (mousePos.X < currentPos.X)
  {
    moveAmountX++;
  }
  if (mousePos.Y > currentPos.Y)
  {
    moveAmountY--;
  }
  else if (mousePos.Y < currentPos.Y)
  {
    moveAmountY++;
  }
  moveImage.SetValue(Canvas.LeftProperty, moveAmountX);
  moveImage.SetValue(Canvas.TopProperty, moveAmountY);
  mousePos = currentPos;
}

private void Canvas_MouseDown(object sender, MouseEventArgs e)
{
  mousePos = e.GetPosition(ImageCanvas);
  isMouseDown = true; 
}

private void Canvas_MouseUp(object sender, MouseEventArgs e)
{
  isMouseDown = false;
}

In this, we capture the mouse in the MouseLeftButtonUp event and release it in the MouseLeftButtonUp event. Finally, we compare mouse positions in the mouse move and adjust the image accordingly. I''ve just knocked this together in the editor here on Code Project, so you may need to adjust the direction of the tests, but this should get you started.


这篇关于901217-如何在Silverlight中平移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆