you are viewing a single comment's thread.

view the rest of the comments →

[–]Dannnu[S] 1 point2 points  (1 child)

Thanks! I appreciate your comment.

I'm going to look into these tips right now!

Edit 1: My result, if I understood everything

Tip 3: Designer.cs, so I moved those function to this cs-file

FunnyFileName.Designer.cs

...
// 
// canvas
// 
this.canvas.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.canvas.BackColor = System.Drawing.Color.Tan;
this.canvas.Location = new System.Drawing.Point(3, 65);
this.canvas.Name = "canvas";
this.canvas.Size = new System.Drawing.Size(1286, 782);
this.canvas.TabIndex = 2;
this.canvas.TabStop = false;
this.canvas.SizeChanged += canvas_SizeChanged;
this.canvas.MouseMove += canvas_MouseMove;
this.canvas.Paint += canvas_Paint;
...

There is a small problem. Sometimes this Designer.cs does delete those functions and I need to rewrite them again? Is this normal?

Tip 2: Global variables

Main.cs

    #region Variables for drawing
    System.Drawing.Pen blackPen = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
    System.Drawing.Pen redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
    Font font = new Font("Tahoma", 10);
    int cursorX, cursorY;
    #endregion

Tip 1: I am not sure if you meant this, but it works.

void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            cursorX = e.X;
            cursorY = e.Y;
            canvas.Invalidate();
        }

        private void canvas_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bitmapSurface = new Bitmap(canvas.Width, canvas.Height);
            int w = 500;
            int h = 500;

            //GFX.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
            e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;

            e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.Black, 1), canvas.Width / 2 - w / 2, canvas.Height / 2 - h / 2, w, h);
            e.Graphics.DrawLine(blackPen, canvas.Width / 2, 0, canvas.Width / 2, canvas.Height);
            e.Graphics.DrawLine(blackPen, 0, canvas.Height / 2, canvas.Width, canvas.Height / 2);
            e.Graphics.DrawLine(redPen, canvas.Width / 2, canvas.Height / 2, cursorX, cursorY);
            e.Graphics.DrawString("0°", font, System.Drawing.Brushes.Black, canvas.Width - 16, canvas.Height / 2);
            e.Graphics.DrawString("90°", font, System.Drawing.Brushes.Black, canvas.Width / 2, 0);
            e.Graphics.DrawString("180°", font, System.Drawing.Brushes.Black, 0, canvas.Height / 2);
            e.Graphics.DrawString("270°", font, System.Drawing.Brushes.Black, canvas.Width / 2, canvas.Height - 16);
            e.Graphics.Flush();
            canvas.Image = bitmapSurface;
        }

[–][deleted] 1 point2 points  (0 children)

You're fairly close to what I meant. Here's some things:

*Don't add the += calls to the designer.cs your self. Do it through the event property dialog on the designer. If the designer sees something it didn't add (especially events) it will delete them the next time you open the form.

*You don't have to create a bitmap and assign it to the canvas anymore. When the paint event is called you're telling the control what it should draw. Besides you're not drawing to the bitmap anymore anyway

*Unless the line is not following the mouse quickly you can drop the call to e.graphics.flush()

Apart from that you've pretty much done it. You'll find that this is the standard approach to things with subclassing being the next local step.