Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to find the centre point of a form for positioning purposes

public class MyForm 
{
   public MyForm() 
   {
      InitializeComponent();
      Rectangle formRect = new Rectangle(Location, Size);
      // want to find the centre point of formRect
      // hoped there would be something like this
      // Point centreOfform = formRect.CentrePoint();
   }
}


I'm sure there is an easy way to do this but it is eluding me at the moment, any ideas.

Suppose I could divide the width by 2 and add it to x and the height by 2 and add it to y.
Posted
Updated 15-Oct-10 0:57am
v3
Comments
Henry Minute 15-Oct-10 6:56am    
Your last para is correct. I don't know an easier way to do it.
CPallini 15-Oct-10 6:59am    
"Suppose I could divide the width by 2 and add it to x and the height by 2 and add it to y." Yes, I suppose you could.
ARopo 15-Oct-10 7:09am    
Just realised where I got the notion, in C++ MFG CRect has a CenterPoint function C

1 solution

Respect to Full monitor scan ->
Point p = new Point();
p.X = Location.X + Size.Width / 2;
p.Y = Location.Y + Size.Height / 2;

Only respect to form ->

Point p = new Point();
p.X = Size.Width / 2;
p.Y = Size.Height / 2;

when you change the form position then 1st one will be changed but the 2nd is fixed, it will not be changed.
 
Share this answer
 
Comments
ARopo 15-Oct-10 7:32am    
Thanks this is what I have done, but I was expecting a function to do it, Like in MFC CRect has a CenterPoint() function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900