901217-如何在Silverlight中平移? [英] 901217 - how to pan in Silverlight?
问题描述
这里是有关如何开始使用此方法的想法< =" 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 theMouseLeftButtonUp
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屋!