像在 Expression Blend 中一样实现缩放
在 Expression Blend 中,当您滚动鼠标滚轮时,屏幕上的元素会放大/缩小,重点关注鼠标所在的位置。
我正在尝试在自己的项目中实现相同的缩放,但是我遇到了缩放后跳跃、移动鼠标然后再次缩放的问题。
到目前为止,我使用 RenderTransform 和 ScaleTransform,使用鼠标坐标作为 ScaleTransform 的 centerX 和 centerY。
有谁知道他们是如何在 Expression Blend 中做到这一点的?
这是我到目前为止所做的:
private void ScrollCanvasMouseWheel(object sender, MouseWheelEventArgs e)
{
double zoomAmnt = e.Delta > 0 ? 0.2 : -0.2;
_scaleTransformAmount += zoomAmnt;
if (_scaleTransformAmount < 0.5)
_scaleTransformAmount = 0.5;
Point position = e.GetPosition(ScrollCanvas);
var scaleTransform = ScrollCanvas.RenderTransform as ScaleTransform;
if (scaleTransform == null) throw new ArgumentNullException("scaleTransform");
scaleTransform.ScaleX = _scaleTransformAmount;
scaleTransform.ScaleY = _scaleTransformAmount;
scaleTransform.CenterX = position.X;
scaleTransform.CenterY = position.Y;
}
In Expression Blend, when you scroll the mouse wheel, the on-screen elements are zoomed in/out, focused on the position where the mouse is.
I'm trying to implement an identical zoom in my own project, however I'm having problems with it jumping around after zooming, moving the mouse and then zooming again.
So far I'm using a RenderTransform with a ScaleTransform, using the mouse co-ordinates as the centerX and centerY for the ScaleTransform.
Does anyone know how they've done it in Expression Blend?
This is what I've done so far:
private void ScrollCanvasMouseWheel(object sender, MouseWheelEventArgs e)
{
double zoomAmnt = e.Delta > 0 ? 0.2 : -0.2;
_scaleTransformAmount += zoomAmnt;
if (_scaleTransformAmount < 0.5)
_scaleTransformAmount = 0.5;
Point position = e.GetPosition(ScrollCanvas);
var scaleTransform = ScrollCanvas.RenderTransform as ScaleTransform;
if (scaleTransform == null) throw new ArgumentNullException("scaleTransform");
scaleTransform.ScaleX = _scaleTransformAmount;
scaleTransform.ScaleY = _scaleTransformAmount;
scaleTransform.CenterX = position.X;
scaleTransform.CenterY = position.Y;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种缩放很好,所以我尝试实现一次,它的数学有点奇怪,我花了大约两天的时间才弄清楚有效的方法,但这可能是相当难看的代码(带有
_ 的变量
是缩放控件上的字段):当然,变换的顺序很重要:
That kind of zoom is nice so i tried to implement it once, the maths of it are a bit weird, it took me about two days to figure something out that worked, this might be rather ugly code though (variables with
_
are fields on the zoom control):The order of the transforms is important of course: