0

I am a novice in programming and i need Your advice, please. I was tasked with writing a programm that will be counting number of items needed according to length of a track. This track we need to draw by ourselves, but I don't want to make this application full screen, so I need a Zooming function and for a life of me I can't get how should i do it. Please help!

And here's the code.

namespace RFIDGovno

{

    public partial class Form1 : Form
    {
        List<Line> lines = new List<Line>();
        List<Dot> dots = new List<Dot>();
        bool isVert;
        Point sPlace;
        Point ePlace;
        bool downed=false;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            Point temp = e.Location;
            if (downed)
            {
                if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
                {
                    temp.X = sPlace.X;
                }
                else
                {
                    temp.Y = sPlace.Y;
                }

                downed = false;
                ePlace = temp;
                dots.Add(new Dot(ePlace));
                lines.Add(new Line(sPlace,ePlace,isVert, isVert ? Math.Abs(sPlace.Y-ePlace.Y) : Math.Abs(sPlace.X - ePlace.X)));

            }else
            {
                if (!dots.Any<Dot>())
                {
                    downed = true;
                    sPlace = e.Location;
                    dots.Add(new Dot(sPlace));
                }
                else if(dots.Where(t => t.fuse).Any<Dot>())
                {
                    downed = true;
                    sPlace = dots.Where(t => t.fuse).First().place;
                }
            }
            drawing(null);
        }

        private void Zooming()
        {
            
        }

        private void drawing(Point? mouse)
        {
            Graphics g = pictureBox1.CreateGraphics();
            Bitmap im = new Bitmap(pictureBox1.Width-1, pictureBox1.Height-1);
            Graphics q = Graphics.FromImage(im);
            q.Clear(Color.White);

            lines.ForEach(line =>
            {
                q.DrawLine(new Pen(Brushes.Black, 2), line.pStart, line.pEnd);
                q.DrawString(line.length.ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, line.isVert ? line.pStart.X : line.pStart.X + ((line.pEnd.X - line.pStart.X) / 2) - 5, line.isVert ? line.pStart.Y + ((line.pEnd.Y - line.pStart.Y) / 2) - 5 : line.pStart.Y);

            });

            if (downed)
            {
                q.DrawLine(new Pen(Brushes.Black, 2), sPlace, mouse ?? sPlace);
                if ((isVert)&&(mouse.HasValue))
                {
                    q.DrawString(Math.Abs(sPlace.Y-mouse.Value.Y).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
                }
                else if ((isVert==false) && (mouse.HasValue))
                {
                    q.DrawString(Math.Abs(sPlace.X - mouse.Value.X).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
                }
            }

            dots.ForEach(dot =>
            {   
                q.DrawEllipse(new Pen(Brushes.Black, 2),dot.place.X-1, dot.place.Y - 1, 2, 2);
                if (dot.fuse)
                {
                    q.DrawEllipse(new Pen(Brushes.Red, 2), dot.place.X - 2, dot.place.Y - 2, 4, 4);
                }
            });
            g.DrawImage(im, 1, 1);
            q.Dispose();
            im.Dispose();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Point temp = e.Location;
            if (downed)
            {
                if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
                {
                    temp.X = sPlace.X;
                    isVert = true;
                }
                else
                {
                    temp.Y = sPlace.Y;
                    isVert = false;
                }
            }
            else
            {
                dots.ForEach(t => t.fuse = false);
                List<Dot> tDots = dots.Where(dt => Math.Abs(e.Location.X - dt.place.X) < 20 && Math.Abs(e.Location.Y - dt.place.Y) < 20).ToList();
                if (tDots.Any<Dot>())
                {
                    double min = 0;
                    min = tDots.Min(t => distance(t.place, e.Location));
                    tDots.Where(t => distance(t.place, e.Location) == min).First().fuse = true;
                }
            }
            drawing(temp);
        }
        private double distance(Point p1, Point p2)
        {
            double dist=0;
            dist = (Math.Sqrt(Math.Abs(p1.X-p2.X)+Math.Abs(p1.Y-p2.Y)));
            return dist;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int between = 0;
            string curr = comboBox1.SelectedItem.ToString();
            if (curr == "Short")
            {
                between = 1250;
            }
            else if (curr == "Medium")
            {
                between = 1875;
            }
            else
            {
                between = 3750;
            }
            double wut = (double.Parse(textBox1.Text) / between * 300 + double.Parse(textBox3.Text) + double.Parse(textBox2.Text) * 2);
            string answer = Convert.ToString(Math.Ceiling(wut));
            label4.Text = answer;
        }
    }

    public class Line
    {
        public Point pEnd;
        public Point pStart;
        public bool isVert;
        public int length;
        public Line(Point pStart, Point pEnd, bool isVert,int length)
        {
            this.pStart = pStart;
            this.pEnd = pEnd;
            this.isVert = isVert;
            this.length = length;
        }
    }

    public class Dot
    {
        public bool fuse;
        public Point place;
        public Dot(Point place)
        {
            this.place = place;
        }
    }
}
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
Zekewitch
  • 1
  • 1
  • To scale/zoom stuff you draw use graphics.ScaleTransform! – TaW Oct 13 '20 at 09:39
  • [Zoom and translate an Image from the mouse location](https://stackoverflow.com/a/61964222/7444103). MouseWheel events are used to scale the Rectangle that describes the Image object. Other *effects* (rotate, pan etc.) are accumulated using Matrix objects and methods (`Matrix.RotateAt()`). Note that the zoom effect is applied to the Rectangle that describes the Image bounds. Only after it's applied to the Image. The same for the other effects. This reduces the code needed to apply and draw all effects to ~6 lines. – Jimi Oct 13 '20 at 11:09
  • BTW, this is wrong: `Graphics g = pictureBox1.CreateGraphics();`. Use the Paint event of your *canvas* to draw your Images/Shapes. – Jimi Oct 13 '20 at 11:12
  • @Jimi Thank you for your insight! But I still have some question: 1. Why is **Graphics g = pictureBox1.CreateGraphics();** wrong? 2. Will it work with an image that I had drawn? I would attach a screenshot but I don't have enough points for that. – Zekewitch Oct 13 '20 at 13:34
  • [Getting Started with Graphics Programming](https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/getting-started-with-graphics-programming), [How to: Create Graphics Objects for Drawing](https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-create-graphics-objects-for-drawing). The object generated by `Control.CreateGraphics()` is not persistent and it's invalidated each time a Control is redrawn (often). You use this method, mostly, when you need to measure a string rendered in a device context. Use the Graphics object provided by PaintEventArgs for drawing. – Jimi Oct 13 '20 at 13:42
  • You can find this information all over SO and MSDN. You actually don't even need `Graphics.FromImage()` here. See the `Graphics.DrawImage` overloads that allow to specify a source image and also source and destination rectangles. – Jimi Oct 13 '20 at 13:51

0 Answers0