0

enter image description hereI'm new to c#, coming from a python / wxpython background. I'm still very inexperienced.

I'm trying to create a user workspace that allows objects that have a representative shape (box or circle) to be placed. I'd like the user to be able to pan and zoom within the workspace. Eventually I want to have the items have attributes and be able to be connected together with lines.

I created a simple c# app that allows me to paint squares over a tiled .png grid background, but I can't figure out to set up a real workspace to pan or zoom.

At this stage some high level suggestions would be great.

Here is my code just FYI (be kind)

        public partial class MainForm : Form
    {
        bool global_draw_redbox = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void redbox(Point mousepos)
        {
            System.Drawing.Graphics graphicsObj;

            graphicsObj = CreateGraphics();

            Pen myPen = new Pen(System.Drawing.Color.Red, 3);

            Rectangle myRectangle = new Rectangle(mousepos.X - 125, mousepos.Y - 100, 250, 200);

            graphicsObj.DrawRectangle(myPen, myRectangle);

        }
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            var relativePoint = this.PointToClient(Cursor.Position);

            if (global_draw_redbox)
            {
                //Draw the object based on the mouse position
                redbox(relativePoint);
                //Reset the flag so we draw just one
                global_draw_redbox = false;
            }
            else
            {
                MessageBox.Show("No object selected");
            }
        }

        private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show("Test Mouse Double Click");
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            var relativePoint = this.PointToClient(Cursor.Position);

            XY_Mouse_Position.Text = relativePoint.ToString();
        }

        private void boxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            global_draw_redbox = true;
        }
    }
  • 1
    Did you already see [this answer](https://stackoverflow.com/questions/32204274/panel-drawing-zoom-in-c-sharp) ? – ywwy Sep 25 '19 at 15:00
  • 1
    That link is not bad but quite limited wrt this question. What you need to do is define a shape/drawaction class. [See here](https://stackoverflow.com/questions/28714411/update-a-drawing-without-deleting-the-previous-one/28716887#28716887). The basic rule for drawing is that it __all__ must happen in/with the Paint parameter e.Graphics. So all shapes must be in a List and all be drawn each time! - Best don't draw onto the Form but onto a PictureBox (recommended), a Label or a DoubleBuffered Panel, btw.. – TaW Sep 25 '19 at 15:22
  • 1
    Oh, and __never__ do this: `graphicsObj = CreateGraphics();` - Winforms graphics basic rule #1 : Never use `control.CreateGraphics`! Never try to cache a `Graphics` object! Either draw into a `Bitmap bmp` using a `Graphics g = Graphics.FromImage(bmp)` or in the `Paint` event of a control, using the `e.Graphics` parameter.. You can test the persistance of your graphics by doing a Minimize/Maximize sequence.. – TaW Sep 25 '19 at 15:53

0 Answers0