在C#Winforms

时间:2015-08-27 17:52:31

标签: c# winforms zoom panel

我有一个可以放大和缩小的应用程序,只有我想放大和缩小的问题但是聚焦到面板位置的当前中间位置,而不是使用鼠标位置

任何想法?

private void panel1_Paint(object sender, PaintEventArgs e)
{
    SolidBrush brushs = new SolidBrush(Color.White);
    e.Graphics.Clip = new Region(new Rectangle(0, 0, Viewer.Width, Viewer.Height));
    e.Graphics.FillRegion(brushs, e.Graphics.Clip);

    Graphics g = e.Graphics;
    g.TranslateTransform(_ImgX, _ImgY);
    g.ScaleTransform(_Zoom, _Zoom);
    g.SmoothingMode = SmoothingMode.AntiAlias;
    SolidBrush myBrush = new SolidBrush(Color.Black);
    Pen p = new Pen(Color.Red);
    foreach (CircuitData.ResistorRow resistorRow in ResistorData.Resistor)
    {
        RectangleF rec = new RectangleF((float)(resistorRow.CenterX  - resistorRow.Length/ 2), (float)(resistorRow.CenterY - resistorRow.Width/ 2), (float)resistorRow.Length, (float)resistorRow.Width);
        float orientation = 360 - (float)resistorRow.Orientation;
        PointF center = new PointF((float)resistorRow.CenterX, (float)resistorRow.CenterY);
        PointF[] points = CreatePolygon(rec, center, orientation);
        if (!Double.IsNaN(resistorRow.HiX) && !Double.IsNaN(resistorRow.HiY))
        {
            g.FillEllipse(myBrush, (float)resistorRow.HiX  - 2 , (float)resistorRow.HiY - 2, 4, 4);
            g.DrawLine(p, new PointF((float)resistorRow.HiX , (float)resistorRow.HiY ), center);
    }

    g.FillPolygon(myBrush, points);
}

}

更新 缩放功能,我知道这是错误的,但如果有人可以帮我修复逻辑或更好的想法。

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        float oldZoom = _Zoom;
        _Zoom = zoomTrackPad.Value / 10f;

        int x = Math.Abs(Viewer.Width / 2 );
        int y = Math.Abs(Viewer.Height / 2 );

        int oldImageX = (int)(x / oldZoom);
        int oldImageY = (int)(y / oldZoom);

        int newImageX = (int)(x / _Zoom);
        int newImageY = (int)(y / _Zoom);

        _ImgX = newImageX - oldImageX + _ImgX;
        _ImgY = newImageY - oldImageY + _ImgY;

        Viewer.Invalidate();
    }

谢谢

1 个答案:

答案 0 :(得分:3)

正如你所说,你的缩放逻辑是错误的。由于转换矩阵的工作方式,这是错误的。

转换矩阵是一个接一个地附加的,在这种情况下,你正在翻译图像就好像已经缩放的那样,然后你缩放它,这将改变缩放原点并且不会居中。

我会在这里假设一些事情,如果pelase评论有什么不妥。

假设您的可见区域(面板大小)为100 * 100像素,并且您希望“虚拟”内容大小为200 * 200像素。我认为你有两个滚动条可以取代面板内的内容,如果你只是使用面板尺寸而没有滚动条,那么你应该调整计算位移的方式。

因此,您的滚动条的范围从0到(面板。(带/高度) - 内容。(宽度/高度)),在这种情况下,范围从0到100。

让我们首先将我们的内容集中在一开始。为了使它居中,我们将TranslateTransform应用于Graphics对象,其可见区域的一半大小减去虚拟区域大小的一半:

e.Graphics.TranslateTransform(panel.With / 2 - content.Width / 2, panel.Height / 2 - content.Height / 2);

这会使绘图居中,但是,嘿!我们没有使用定位滚动条!

Okokoko,让我们应用那些滚动条偏移量。首先,当我们以内容为中心时,将这些滚动条的值设置为其最大值的一半,在我们的示例中,我们应将它们设置为50。

现在,正如我们所说,转换是附加的,所以,没有什么能阻止你添加新的TranslateTransform。你只能在一次通话中做到这一点,但为了简单起见,我们在两次通话中这样做(同样,以后应用变焦会更好)。

e.Graphics.TranslateTransform(panel.With / 2 - content.Width / 2, panel.Height / 2 - content.Height / 2);
e.Graphics.TranslateTransform(hScrollBar1.Value - (hScrollbar1.Maximum / 2), vScrollBar1.Value - (vScrollbar1.Maximum / 2));

如您所见,我们正在减去其最大值的一半,我们已经将内容置于中心位置,因此当滚动条位于中心时,我们需要应用0的平移,我们的范围从-Maximum / 2到Maximum / 2 ,在这个例子中从-50到50。

好吧,我们现在有一个虚拟内容区域位于屏幕中心,我们可以替换它,现在我们需要应用缩放。

我看到你有一个滚动条,所以让我们按照我们的例子使用它。

滚动条的范围是你的选择,我看你正在使用值/ 10f,所以如果你的滚动条范围从1到100,你将有0.1到10的缩放,这是一个很好的选择。< / p>

现在,让我们将此滚动条设置为10,以初始进行单一缩放。

现在有了一个有趣的部分,转换是按照你设置它们的顺序应用的,所以在翻译之前应用缩放而不是在之后之后应用

在我们的例子中,我们首先想要将内容居中,转换到我们想要看到的位置(滚动条值)然后缩放。

e.Graphics.TranslateTransform(panel.With / 2 - content.Width / 2, panel.Height / 2 - content.Height / 2);
e.Graphics.TranslateTransform(hScrollBar1.Value - (hScrollbar1.Maximum / 2), vScrollBar1.Value - (vScrollbar1.Maximum / 2));
float scale = zScrollBar.Value / 10f;
e.Graphics.ScaleTransform(scale, scale);

好的,听起来不错,但等一下!它会悲惨地失败。为什么?因为当我们缩放时,我们的虚拟大小也会相对于视图区域(面板大小)进行放大,因此我们需要将此比例应用于内容大小(因此,应用于滚动条值):

float scale = zScrollBar.Value / 10f;
e.Graphics.TranslateTransform(panel.With / 2 - ((content.Width / 2) * scale), panel.Height / 2 - ((content.Height / 2) * scale));
e.Graphics.TranslateTransform((hScrollBar1.Value - (hScrollbar1.Maximum / 2)) * scale, (vScrollBar1.Value - (vScrollbar1.Maximum / 2)) * scale);
e.Graphics.ScaleTransform(scale, scale);

最后,我们有一个完全正常工作的虚拟区域,您可以通过它来缩放和移动它。

我对图形有点生疏,所以也许有些计算是错误的,我希望不会,在任何情况下,这都会给你关于如何做到这一点的一般想法。

干杯!

PD:如果您不想替换虚拟区域并且只是放大面板中心,请跳过第二个平移变换,如果您想放大虚拟区域的当前可见中心使用置换(不缩放它,变换已经具有此逻辑)而不是滚动条值。