c# – place panel in rectangle with screen coordinates – Code Utility

[

How do I get a panel to use the same coordinates of the points of a rectangle I plot?
For example, say I plot the points of a rectangle with

SetCursorPos(200, 200); // top left
SetCursorPos(200, 400); // top right
SetCursorPos(400, 400); // bottom right
SetCursorPos(200, 400); // bottom left
SetCursorPos(300, 300); // center

Then I create a panel, and give it the same dimensions

panel.Size = new Size(200, 200);

Now I want to position the panel exactly in the location of the rectangle.

This is where I am having problems.
I have been trying unsuccessfully to accomplish this.
I tried…

Control control = panel;
Point point control.PointToClient(new Point(200 + 8, 200 + 32)); // the additions make up for the fact that PointToClient and PointToScreen are out by those pixels

This shows the correct coordinates for the panel when I position it.

panel.Location = new Point(point.X, point.Y);

Before : Location point 0,0

After :

(0) top left – 200,200

(1) center – 300,300

Location point 200,200

Rect: (200, 200)-(400, 400) Size: {Width=200, Height=200}

The panel however, is always offset, regardless.

enter image description here

I tried moving the anchor point, but this does not help.

panel.Anchor = (AnchorStyles.Left | AnchorStyles.Top);

…and I also tried

ClientToScreen(panel.Handle, ref point);

…to no avail.

Could the problem be associated with the parent’s (Form) relative position? What do I need to do to simply reposition the panel so that it aligns with my rectangle?

,

I figured a way that gets what I want.
I use the parent’s top left, and subtract it from the child’s top left I want to place. Then use the resulting value for the position.

panel.Location = new Point(panel.Left - parent.Left, panel.Top - parent.Top);

This way, I always know the objects position, relative to its parent.

Results

enter image description here

Edited :
I give Jimi’s suggestion another try, and it does work. (The problem was with my coordinates.)

Control control = form; // not panel
Point point control.PointToClient(new Point(200, 200)); 
panel.Location = point;

Thanks again Jimi.

]