RectangleToScreen question.

  • Thread starter Thread starter Peter Rilling
  • Start date Start date
P

Peter Rilling

Okay, this method seems simple, but it eludes nonetheless.

I have a control that is nested in other controls. Essentially the control
is nested several contains from the main form. All I want is to determine
the screen coordinates of the control, but if I call RectangleToScreen on
either the Form or the parent container results in incorrect bound when
passed the bounds of the control.

How might I determine the screen coordinates of the control when it is
inside other containers?
 
Peter,

What's wrong with this?...

Cursor.Position.X
Cursor.Position.Y

Best Regards
Johann Blake
 
Peter said:
Okay, this method seems simple, but it eludes nonetheless.

I have a control that is nested in other controls. Essentially the control
is nested several contains from the main form. All I want is to determine
the screen coordinates of the control, but if I call RectangleToScreen on
either the Form or the parent container results in incorrect bound when
passed the bounds of the control.

How might I determine the screen coordinates of the control when it is
inside other containers?

You don't mention what the nature of the incorrect result is. Generally
you are doing this correctly: in a simple case, say you have a form and
a button, say at (100, 100) on the form. Now if you pass in a rect like
this to the form's RectangleToScreen method: (x: 100, y: 100, w: 75, h:
23), you should get the screen rectangle of the button in return. Works
for me :-) What are you doing (or seeing) differently?

For purposes of comparison: you should also get to the same top left
coordinate of the inner element by calling PointToScreen(new Point(0,
0)) on the element itself.



Oliver Sturm
 
Not sure what the cursor has to do with translating the control's local
coordinates to screen coordinates.
 
Let's see if I can explain. I am probably not using the method correctly.

Suppose I have the following controls layered such that each is a child of
the outer control. The coordinates in parenthesis is the top-left of the
control within the parent container. So panel2 should be about 30 px from
the edge of the screen (give or take some point for the form edge).

form1 (0, 0)
panel1 (10, 10)
panel2 (20, 20)
panel3 (30, 30)
button1 (40, 40)

What I would want to do is to get the screen position of button1. That
would be about 40+30+20+10 = 100 px, right (again, give or take).

For me to get this value, what is the proper way of using RectangleToScreen?
Would I go form1.RectangleToScreen(button1.Bounds) or
panel3.RectangleToScreen(button1.bounds) or something else? When I
calculate the value, it always is less then what is should be, almost like
it is not taking into account the ancestor containers.
 
Peter said:
Let's see if I can explain. I am probably not using the method correctly.

Suppose I have the following controls layered such that each is a child of
the outer control. The coordinates in parenthesis is the top-left of the
control within the parent container. So panel2 should be about 30 px from
the edge of the screen (give or take some point for the form edge).

form1 (0, 0)
panel1 (10, 10)
panel2 (20, 20)
panel3 (30, 30)
button1 (40, 40)

What I would want to do is to get the screen position of button1. That
would be about 40+30+20+10 = 100 px, right (again, give or take).

No. The screen position is the position on screen, as the name tries to
indicate :-) Your calculation is only correct if your form is actually
at a (0, 0) screen coordinate. But maybe you meant it that way?
For me to get this value, what is the proper way of using RectangleToScreen?
Would I go form1.RectangleToScreen(button1.Bounds) or
panel3.RectangleToScreen(button1.bounds) or something else? When I
calculate the value, it always is less then what is should be, almost like
it is not taking into account the ancestor containers.

Okay, it should work by calling
panel3.RectangleToScreen(button1.Bounds). Or more generically,
button1.Parent.RectangleToScreen(button1.Bounds). If you have a complex
hierarchy of controls, it might be a good idea to just use the control
itself: button1.RectangleToScreen(new Point(0, 0), button1.Size) should
give the same result. I tried these in a test program right now and they
worked fine for me.


Oliver Sturm
 
Thanks. Everything I was doing should have worked but the numbers were not
what I expected.

I discovered that the calculations were incorrect because I was not showing
the form. Although I would not have expected this to be a problem, it was.
I would say this is a bug in the framework. So, if you ever want to use
RectangleToScreen, make sure the form (or maybe control as well) is visible.
 
Peter said:
Thanks. Everything I was doing should have worked but the numbers were not
what I expected.

I discovered that the calculations were incorrect because I was not showing
the form. Although I would not have expected this to be a problem, it was.
I would say this is a bug in the framework. So, if you ever want to use
RectangleToScreen, make sure the form (or maybe control as well) is visible.


Glad to hear you found the problem.


Oliver Sturm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top