我有一个所有者绘制的 PictureBox ( picGrid
),我希望允许用户使用鼠标滚轮进行缩放。那部分很容易。但我想自动滚动,使鼠标下的逻辑点在缩放后仍然是鼠标下的逻辑点。
这是我到目前为止所拥有的。它几乎可以工作,但只是不正确。XScroll
并且YScroll
是我的滚动位置,将用于设置滚动条(除了偏移图像)。
/// <summary>
/// Sets the current zoom percent.
/// </summary>
/// <param name="percent">The percent that the zoom should be set (100 == 1:1 scaling).</param>
/// <param name="x">X coordinate of the mouse in pixels.</param>
/// <param name="y">Y coordinate of the mouse in pixels.</param>
public void SetZoom(int percent, int x, int y)
{
// If needed, this method translates mouse coordinate to logical coordinates
// but doesn't appear to be needed here.
//TranslateCoordinates(ref x, ref y);
// Calculate new scale (1.0f == 1:1 scaling)
float newDrawScale = (float)percent / 100;
// Enforce zoom limits
if (newDrawScale < 0.1f)
newDrawScale = 0.1f;
else if (newDrawScale > 1500.0f)
newDrawScale = 1500.0f;
// Set new zoom (if it's changed)
if (newDrawScale != DrawScale)
{
DrawScale = newDrawScale;
// *** Here's the part that isn't right ***
// Scroll to keep same logical point under mouse
float delta = (DrawScale - 1f);
XScroll = (x * delta);
YScroll = (y * delta);
// Reflect change in scrollbars
UpdateScrollbars();
// Redraw content
picGrid.Invalidate();
}
}
如何计算新的滚动位置,以便缩放后相同的逻辑点保留在鼠标指针下方?