我正在尝试创建一个画布来绘制不同的对象。我已经使用graphics.scaletransform和graphics.translatetransform创建了Zoom和Pan函数,但是我不是帆布背景(网格)(网格)一直填写整个窗口,但是使用以下代码:

编辑:我已经尝试在转换的图形对象中使用坐标,但是似乎不接受负数?!?

编辑:这张照片解释了我的问题:

alt text

 public partial class Form1 : Form
        {

            PointF mouseDown;

            float newX;
            float newY;
            float zoomFactor = 1F;

            Region _rgn;
            Graphics _dc;
            PointF zoomPoint = new PointF(150, 150);

            public Form1()
            {
                InitializeComponent();

                mouseDown = new PointF(0F, 0F);

                this.panel1.Paint += new PaintEventHandler(panel1_Paint);
                this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(panel1_MouseDown);
                this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);

            }



            private void panel1_Paint(object sender, PaintEventArgs e)
            {

                base.OnPaint(e);

                //Graphics bg = 

                Graphics dc = e.Graphics;
                _dc = dc;

                dc.SmoothingMode = SmoothingMode.AntiAlias;

Color gridColor = Color.FromArgb(230, 230, 230);
            Pen gridPen = new Pen(gridColor, 1);

            for (float i = 0; i < this.Height * (zoomFactor); i = i + 30*zoomFactor)
            {
                dc.DrawLine(gridPen, 0, i, this.Width * (zoomFactor), i);
            }
            for (float i = 0; i < this.Width * (zoomFactor); i = i + 30*zoomFactor)
            {
                dc.DrawLine(gridPen, i, 0, i, this.Height * (zoomFactor));
            }

                dc.TranslateTransform(newX, newY);
                dc.ScaleTransform(zoomFactor, zoomFactor, MatrixOrder.Prepend);



                float XPosition = 10;
                float YPosition = 10;
                float CornerRadius = 5;
                float Width = 50;
                float Height = 50;

                Color BoxColor = Color.FromArgb(0, 0, 0);
                Pen BoxPen = new Pen(BoxColor, 2);

                GraphicsPath Path = new GraphicsPath();

                Path.AddLine(XPosition + CornerRadius, YPosition, XPosition + Width - (CornerRadius * 2), YPosition);
                Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition, CornerRadius * 2, CornerRadius * 2, 270, 90);
                Path.AddLine(XPosition + Width, YPosition + CornerRadius, XPosition + Width, YPosition + Height - (CornerRadius * 2));
                Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90);
                Path.AddLine(XPosition + Width - (CornerRadius * 2), YPosition + Height, XPosition + CornerRadius, YPosition + Height);
                Path.AddArc(XPosition, YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90);
                Path.AddLine(XPosition, YPosition + Height - (CornerRadius * 2), XPosition, YPosition + CornerRadius);
                Path.AddArc(XPosition, YPosition, CornerRadius * 2, CornerRadius * 2, 180, 90);

                Path.CloseFigure();



                LinearGradientBrush lgb = new LinearGradientBrush(new PointF(XPosition+(Width/2),YPosition), new PointF(XPosition+(Width/2),YPosition + Height), Color.RosyBrown, Color.Red);

                dc.FillPath(lgb, Path);


                dc.DrawPath(BoxPen, Path);

                Matrix transformMatrix = new Matrix();
                transformMatrix.Translate(newX, newY);
                transformMatrix.Scale(zoomFactor, zoomFactor);

                _rgn = new Region(Path);

                _rgn.Transform(transformMatrix);

            }

            private void panel1_MouseDown(object sender, EventArgs e)
            {
                MouseEventArgs mouse = e as MouseEventArgs;

                if (mouse.Button == MouseButtons.Right)
                {

                    mouseDown = mouse.Location;

                    mouseDown.X = mouseDown.X - newX;
                    mouseDown.Y = mouseDown.Y - newY;

                }

                else if (mouse.Button == MouseButtons.Left)
                {

                    if (_rgn.IsVisible(mouse.Location, _dc))
                    {
                        MessageBox.Show("tada");
                    }


                }

            }

            private void panel1_MouseMove(object sender, EventArgs e)
            {
                MouseEventArgs mouse = e as MouseEventArgs;

                if (mouse.Button == MouseButtons.Right)
                {
                    PointF mousePosNow = mouse.Location;

                    float deltaX = mousePosNow.X - mouseDown.X;
                    float deltaY = mousePosNow.Y - mouseDown.Y;

                    newX = deltaX;
                    newY = deltaY;

                    panel1.Invalidate();

                }


            }

            protected override void OnMouseWheel(MouseEventArgs e)
            {

                MouseEventArgs mouse = e as MouseEventArgs;

                PointF mP = mouse.Location;

                if (e.Delta > 0)
                {
                    if (zoomFactor >= 1 && zoomFactor <= 10)
                    {
                        zoomFactor += 1F;

                        newX = newX - ((mP.X - newX) / (zoomFactor - 1));
                        newY = newY - ((mP.Y - newY) / (zoomFactor - 1));
                    }
                    else if (zoomFactor == 0.5)
                    {
                        zoomFactor = zoomFactor * 2;
                        newX = 2 * newX - mP.X ;
                        newY = 2 * newY - mP.Y ;
                    }
                    else if (zoomFactor < 0.5)
                    {
                        zoomFactor = zoomFactor * 2;
                        newX = 2 * newX - mP.X;
                        newY = 2 * newY - mP.Y;
                    }
                }

                else if (e.Delta < 0)
                {
                    if (zoomFactor >2)
                    {
                        zoomFactor -= 1F;
                        newX = newX + (((mP.X - newX)) / (zoomFactor+1 ));
                        newY = newY + (((mP.Y - newY)) / (zoomFactor+1));
                    }
                    else if (zoomFactor == 2) {
                        zoomFactor -= 1F;

                        newX = newX + ((mP.X - newX)/2);
                        newY = newY + ((mP.Y - newY)/2);
                    }else if(zoomFactor <= 1 && zoomFactor > 0.2)
                    {
                        zoomFactor = zoomFactor / 2;

                        newX = newX + ((mP.X - newX) / 2);
                        newY = newY + ((mP.Y - newY) / 2);

                    }


                }

                panel1.Invalidate();

            }
        }
有帮助吗?

解决方案

在转换坐标系之前绘制网格。

如果不能,请使用 getTransform()resettransform(), ,然后绘制网格,然后向后setTransform(第一步又拿到了)。

恕我直言:如果您“手动”缩放图像而不是使用坐标转换,那就更好了。您将以这种方式拥有更大的控制权,并且不会用任何东西撞到墙壁。

编辑:

另外:您的网格图绘图例程中有一个错误:

for (float i = 0; i < this.Height * (zoomFactor); i = i + 30*zoomFactor)

尝试替换

for (float i = 0; i < this.Height; i = i + 30*zoomFactor)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top