Question PointToScreen

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
Apparently I don't understand what PointToScreen() is actually returning. I'm thinking it should be absolute screen co-ordinates. In a form, I have some labels in a panel, which itself is in a panel and am trying to derive the screen co-ordinates for the labels in one statement without adding the X and Y co-ordinates of the panels. Why does PointToScreen(Label.Location) return the same value as PointToClient(Label.Location) and the same as Label.Location? Shouldn't PointToScreen() return absolute SCREEN co-odinates?
 
Which controls PointTo are you talking about? All controls have this method, and the result is relative to the control that calls it. Your starting point is the Label, its Location is relative to its parent control, using the parent controls PointToScreen will give you the screen coordinates of this point.
VB.NET:
Dim labelScreenLocation As Point = Label1.Parent.PointToScreen(Label1.Location)
 
Further to what JohnH posted, if you want the Label to tell you its screen coordinates then you want to use the origin, i.e. (0,0) with respect to the Label:
VB.NET:
Dim labelScreenLocation As Point = Label1.PointToScreen(Point.Empty)
 
Great, the less code, but it also helps to understand the relation of the control coordinates. Another interesting point is that toplevel forms PointToScreen always return screen coordinates, even if they are MDI childs.
 
I don't know why this is so confusing to me. I think I've got it and then I don't.
Here's a senario where I've got a base form and on top of it, a mostly transparent form. The transparent form is going to draw a rectangle that encloses a label on the base form. It calls a sub named "DrawBox" that does the dirty work of creating a graphics object, etc. and takes a rectangle as it's argument. This line of code works.
VB.NET:
DrawBox(Me.RectangleToClient(BaseForm.RectangleToScreen(New Rectangle _
    (BaseForm.PointToClient(BaseForm.Label1.PointToScreen(New Point(-8, -8))), New Size(100, 40)))))
How many of the ".toClient" and ".toScreen" function calls above are actually neccessary here?
 
Well, first up you do not create a Graphics object in any old method. You should be drawing ONLY in the Paint event handler of the control you're drawing on. You might like to read this, and make sure you follow the advice in the first post and read post #17 as well.

As for your code, apply some logic. Forget the code and just ask yourself what steps you need to perform. You first need to get the Label's location in screen coordinates. I've already shown you how to do that in post #3. You then need to turn those screen coordinates into client coordinates on the other form. That's one more call.
VB.NET:
thisForm.PointToClient(theOtherForm.theLabel.PointToScreen(Point.Empty))
 
I am also having a problem with PointToScreen but only when the form is not maximized. I have a DataGridView on a tab control. When the user clicks a cell in the grid I want to display different controls just below the current cell. The code below works fine but only if the form the controls are on is maximized. If I initially run the project without the form being maximized the locations I get are incorrect for what I want. If I maximize the form then the code works. Would appreciate any help.

Private Sub CustomGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Me.CellEnter

Dim Ctl As Control = Nothing
Dim CellRect As Rectangle = Nothing

CellRect = Me.GetCellDisplayRectangle (Me.CurrentCell.ColumnIndex,Me.CurrentCell.RowIndex, False)

Ctl = CheckForCustomEditor()
If Not IsNothing(Ctl) Then
Ctl.Width = CellRect.Width
Ctl.Location = New Point(CellRect.X + Me.Left, (CellRect.Y + Me.Top) + CellRect.Height)
End If
End Sub
 
VB.NET:
Ctl.Parent = Me
Now the Ctl coordinates can be set in Dgv coordinates, ie same as reported from GetCellDisplayRectangle (offset to cell height).
 
I am also having a problem with PointToScreen but only when the form is not maximized. I have a DataGridView on a tab control. When the user clicks a cell in the grid I want to display different controls just below the current cell. The code below works fine but only if the form the controls are on is maximized. If I initially run the project without the form being maximized the locations I get are incorrect for what I want. If I maximize the form then the code works. Would appreciate any help.

Private Sub CustomGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Me.CellEnter

Dim Ctl As Control = Nothing
Dim CellRect As Rectangle = Nothing

CellRect = Me.GetCellDisplayRectangle (Me.CurrentCell.ColumnIndex,Me.CurrentCell.RowIndex, False)

Ctl = CheckForCustomEditor()
If Not IsNothing(Ctl) Then
Ctl.Width = CellRect.Width
Ctl.Location = New Point(CellRect.X + Me.Left, (CellRect.Y + Me.Top) + CellRect.Height)
End If
End Sub
You say that you're having trouble with PointToScreen yet I see no usage of PointToScreen in your code.
 
Back
Top