3

I'm trying to create a canvas for drawing different objects. I've created zoom and pan functions using graphics.scaleTransform and graphics.translateTransform, but i wan't the canvas background (a grid) to allways fill out the entire window, however it does not, using the following code:

EDIT: I've tried using the coordinates in the transformed graphics object, but it seems it won't accept negative numbers?!?

EDIT: This picture explains my problem:

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();

            }
        }
Robaticus
  • 22,857
  • 5
  • 54
  • 63
Bildsoe
  • 1,310
  • 6
  • 31
  • 44

1 Answers1

2

Draw your GRID BEFORE transforming the coordinate system.

If you can't, use GetTransform() and ResetTransform(), then draw grid, then SetTransform back (the one got in the first step).

IMHO: It would be much better if you scale you image 'manually' instead of using coordinate transformation. You'll have much greater control this way, and won't hit the wall with anything.

EDIT:

Also: you have a bug in your grid drawing routine:

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

try replacing with

for (float i = 0; i < this.Height; i = i + 30*zoomFactor)
Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
  • @Daniel - what do you mean by scaling manually? that i scale the individual object on the canvas? – Bildsoe Nov 30 '10 at 12:23
  • @Daniel - Oh and i do draw the grid before scaling, but when i scale it down, it is not big enough, especially when i both zoom and pan. – Bildsoe Nov 30 '10 at 12:24
  • yes, scale and shift them around manually. what do you mean - not big enough? also, from your code it seems that you DON'T draw grid before the transformation... maybe if you can produce a picture of 'THIS IS HOW I WANT IT' and 'THIS IS LIKE IT IS NOW' – Daniel Mošmondor Nov 30 '10 at 12:25
  • @Daniel - Yes I can create some pictures like that. But really what happens now is that the grid stays the size of the original windows. So if I zoom out, only part of the background is covered by the grid. I tried moving the code to before the scale, and just set the translation and scale by appliying the zoomFactor variable to them. But the result is the same. Also can you elaborate on what the problem might become by NOT transforming them manually? – Bildsoe Nov 30 '10 at 12:50
  • @Daniel - I produced the picture as promised :) – Bildsoe Nov 30 '10 at 13:41