1

I have a picturebox with an image. This image is a map with different locations of cities.

The task: When I hover over a city name, a window should open and show some information about the city.

I already programmed an invisible rectangle which covers the city name on the picture and when I click in the area of the rectangle, actions can be performed.

My problem now is, that my form starts maximized but still should be resized like the user wants it to. The program also should be able to run on different resolutions and that is where the problem starts.

My picturebox sizeMode is on Zoom and dock is set to Fill. Obviously the rectangle stays fixed on the form and when I resize the window, it won't cover the city name anymore. I already implemented an scaling for my rectangle which I think does his job but there is an another problem, that because of my pictureBox settings on "Zoom" and "Fill" I'll get white empty areas in my form when resizing so that my rectangles again won't cover the city name.

In addition to that, I also have to implement that the rectangle scales his size when the city name gets smaller or bigger on the picture so that the rectangle always covers the area above the city name and not getting too small or too big.

public partial class Form1 : Form
    {
        private MyRectangle rect = new MyRectangle("Kiel", 885, 124, 51, 18);
        private float xScale, yScale;

        public Form1()
        {
            InitializeComponent();

            xScale = rect.X / (float)pictureBox1.Width;
            yScale = rect.Y / (float)pictureBox1.Height;
        }

        private void pictureBox1_Resize(object sender, EventArgs e)
        {
            rect.X = (int)(pictureBox1.Width * xScale);
            rect.Y = (int)(pictureBox1.Height * yScale);
        }
    }

class MyRectangle
    {
        private int x, y, height, width;
        private string name;

        public MyRectangle(string name, int x, int y, int width, int height)
        {
            this.name = name;
            this.x = x;
            this.y = y;
            this.height = height;
            this.width = width;
        }

        public MyRectangle(int x, int y, int width, int height)
        {
            this.x = x;
            this.y = y;
            this.height = height;
            this.width = width;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        public int Y
        {
            get { return y; }
            set { y = value; }
        }

        public int X
        {
            get { return x; }
            set { x = value; }
        }

        public bool Contains(int x, int y)
        {
            if ((this.x <= x && x <= (this.x + width)) && (this.y <= y && y <= (this.y + height)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
iRecordS
  • 11
  • 1
  • Take some ideas from [this](https://stackoverflow.com/a/50326689/14171304), [this](https://stackoverflow.com/a/39305038/14171304), and [this](https://stackoverflow.com/a/30437608/14171304). – dr.null Sep 21 '21 at 12:45
  • One more to study is [here](https://stackoverflow.com/a/61964222/14171304). – dr.null Sep 21 '21 at 12:54
  • 2
    Thanks, I haven't found those posts when I did some researches. I will look if I can implement one of those ideas. – iRecordS Sep 21 '21 at 13:13

0 Answers0