CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004
    Posts
    561

    RectangleToScreen please explain.

    I'm using RectangleToScreen to try to find the exact screen coordinates of a control in a form.

    Code:
     
       Rectangle rectQD = m_qd.Control.FindForm().RectangleToScreen(m_qd.Control.Bounds);
    This piece of code above sometimes works. I've seen this sometimes return the screen location of the parent form not the location of the control residing in it. ???

    Code:
      
       Rectangle rectQD = m_qd.Control.RectangleToScreen(m_qd.Control.FindForm().ClientRectangle);
    This piece of code above always returns the correct screen location of the control. But it doesn't make any sense to me. Can someone explain?

    Thank you,

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: RectangleToScreen please explain.

    control.x and control.y will give you the screen coordinates of a control relative to the parent form.

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: RectangleToScreen please explain.

    Control has a property called Parent. If you drop a control on to a form, its parent will be the form. if you drop a control inside a panel inside the form, then that control's parent will be the container its nested inside. FindForm gets the control that is the containing form for the control (it walks the Parent tree for you).

    like biged said, the control's coords are relative to its container, so if you have a button sitting inside the form at x=7, y=7, and call FindForm().PointToScreen(control.Location), its going to take the offset 7,7 and add the forms location (relative to the screen) and return that location.

    if your control has a panel on it (at 7,7) and a button inside it (at 7,7) and you call its RectangleToScreen, its going to recursively obtain correct offset info back to the screen (control's location of 7,7 + container's location of 7,7, plus form's location of n,n). calling FindForm and passing in a point or rect into it, is going to skip any controls it may be nested inside, and could end up giving you the wrong point as an offset.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured