difference between PointToScreen and PointToClient

can someone explain the difference between this two  they are method of the control class.

I have a resoultion of 1024x768.
I have a maximized form.
The form has one panel in it

if i have the mouse somewhere inside the panel, and i am intercepting mouseup for example inside the panel, will there be any difference between these:

 - cursor.position
 - me.PointToClient(cursor.position)   (me is the panel)
 - me.PointToScreen(cursor.position)  (me is the panel)

 - me.FindForm.PointToClient(cursor.position)   (me is the panel)
 - me.FindForm.PointToScreen(cursor.position)  (me is the panel)

According to what i have read, PointToClient will return the Point according to the client region, ie an x and y axis of the client area, while the PointToScreen will always return it to an x and y axis of the Screen


Answer this question

difference between PointToScreen and PointToClient

  • Benc45

    PointToScreen and PointToClient are inverses.  Thus, given a Point p, these three will be equal:


    p == myControl.PointToClient(myControl.PointToScreen(p)) == myControl.PointToScreen(myControl.PointToClient(p))


    The client point is indexed from the upper-left corner of the whatever control is calling the PointToX method.  If this is a form, it is indexed from the point immediately below the top bars (the title bar and MainMenu bar, if you have both/either).  The screen point is indexed from the upper-left screen pixel.

    So, if you have a form maximized (programmatically using form.WindowState = FormWindowState.Maximized) and click in the very upperleft corner of the form (for simplicity, let's say you have set the form's FormBorderStyle to none and have no MainMenu), then both your client point and your screen point will be (0,0).

    However, if you have a title bar on the form, then the upper-left-most point you can click that will fire a mouse-down event is client point (0,0) and screen point (0,26) or so (26 accounting for the height of the title bar).

    Alright, so here's the deal with the two methods:

    PointToScreen -- takes a client-indexed point and turns it into a screen-indexed point.  Thus, if your form is about 50 pixels down from the top of the screen, and 50 pixels from the left side of the screen, then form.PointToScreen(new Point(0,0)) will return a point that is approximately (50,50).

    PointToClient -- takes a screen-indexed point and turns it into a client-indexed point.  Thus, taking the same example as above (your form is about 50 pixels down, 50 pixels in), form.PointToClient(new Point(0,0)) will return a point approximately (-50,-50).  This is because you are passing in a screen point of (0,0), which is the very upper-left pixel you can see on your screen.  In terms of your form's coordinates, this has negative indices.

    IMPORTANT: it rarely makes sense to handle an event that immediately uses the PointToClient method.  Points from event arguments are generally client Points--so if your form handles a MouseDown event and you extract the point from the events args, any necessary conversion will be form.PointToScreen().

    Tom

  • Tom_B

    When you get a MouseEventArg - its X & Y are translated to be within the control's local co-ordinate system.  That is - say you handle the MouseMove of a Panel.  The X & Y you get in the event are going to be relative from the upper left hand corner of the Panel.

    When you ask for Cursor.Position - this is relative to the screen location.


    The local coordinate system is called the client area.  PointToClient implies that you have a Point on the overall screen that you want to convert into your local client area.

    panel1.PointToClient(cursor.position) will translate the screen coordinate to local
    panel1.PointToScreen(cursor.position) will give you garbage 

    me.FindForm.PointToClient(cursor.position) returns the point in the Form's client area
    me.FindForm.PointToScreen(cursor.position) will give you garbage

    When you say panel1.PointToScreen(somepoint) basically it says panel1 starts at position X,Y relative to the screen.  Add somepoint.X and somepoint.Y to the location of panel1 and return it.  So it matters which control you call PointToScreen with.  

    As for handling MouseDown/MouseMove  - I would consider using the X & Y provided in the MouseEventArgs (which is relative to the upper left hand corner of the control) rather than using cursor.Position.

    Hope this helps - have fun mousing!

    Jessica

  • difference between PointToScreen and PointToClient