如果不想使用DeepZoom,也可以使用
ViewBox
下面是我在Silverlight应用程序中使用的一些代码,通过一些工作可以将其更改为对捏和触摸手势的反应,而不是鼠标滚轮+单击/拖动事件。也可以根据捏手势的“力度”改变缩放量。
<Border Name="viewboxBackground" Background="Black">
<Viewbox Name="viewboxMain">
<!--your content here -->
</Viewbox>
</Border>
代码隐藏:
#region Pan and Zoom Events and Handlers
void MouseClickHandler(object sender, MouseButtonEventArgs e)
{
_mouseClickPos = e.GetPosition(viewboxBackground);
bMoving = true;
}
void MouseMoveHandler(object sender, MouseEventArgs e)
{
if (bMoving)
{
//get current transform
CompositeTransform transform = viewboxMain.RenderTransform as CompositeTransform;
Point currentPos = e.GetPosition(viewboxBackground);
transform.TranslateX += (currentPos.X - _mouseClickPos.X);
transform.TranslateY += (currentPos.Y - _mouseClickPos.Y);
viewboxMain.RenderTransform = transform;
_mouseClickPos = currentPos;
}
}
void MouseReleaseHandler(object sender, MouseButtonEventArgs e)
{
bMoving = false;
}
void MouseWheelZoom(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0)
{
_zoomMultiplier += _zoomRate;
ApplyZoomTransform(viewboxMain, _zoomMultiplier, new Point(viewboxMain.ActualWidth / 2, viewboxMain.ActualHeight / 2));
}
else if (e.Delta < 0 && _zoomMultiplier > 1)
{
_zoomMultiplier -= _zoomRate;
ApplyZoomTransform(viewboxMain, _zoomMultiplier, new Point(viewboxMain.ActualWidth / 2, viewboxMain.ActualHeight / 2));
}
}
/// <summary>
///
/// </summary>
/// <param name="element"></param>
/// <param name="iZoomFactor"></param>
/// <param name="zoomCenter">If provided, the zoom will be centered around the given position.</param>
void ApplyZoomTransform(UIElement element, double iZoomFactor, Point? zoomCenter)
{
//get current transform
CompositeTransform transform = viewboxMain.RenderTransform as CompositeTransform;
if (zoomCenter != null)
{
transform.CenterX = zoomCenter.GetValueOrDefault().X;
transform.CenterY = zoomCenter.GetValueOrDefault().Y;
}
transform.ScaleX = iZoomFactor;
transform.ScaleY = iZoomFactor;
element.RenderTransform = transform;
}
#endregion