Getting the absolute position of a control without knowing its parent
Is there anyway to get the screen coords of a control that doesn't have an hWnd and if you don't know its parent control? I am trying to work on a class and I need to know the top and left of a control relative to a screen (not the window) without passing an hWnd.
Re: Getting the absolute position of a control without knowing its parent
Do you mean that a control will be passed as a parameter to a sub/function?
If so, something like this should work:
VB Code:
Dim lLeft As Long
Dim lTop As Long
Dim oObject As Object
Set oObject = [i]<parameter name>[/i]
lLeft = oObject.Left
lTop = oObject.Top
Do While Not (TypeOf oObject Is Form)
Set oObject = oObject.Container
lLeft = lLeft + oObject.Left
lTop = lTop + oObject.Top
Loop
MsgBox lLeft & vbCr & lTop
Re: Getting the absolute position of a control without knowing its parent
Don't all controls have a Parent property, which is the Form it is sited upon.
Re: Getting the absolute position of a control without knowing its parent
Well I am trying to make a class that works with any control that is alignable on an MDI form. Some custom controls might not have a .Parent property so I was wondering if there was an API way to do it without knowing the form or the .Parent.
I guess I could have the user pass the form as an argument when intializing the control, but I was trying to avoid that. If there is no API way to do it then I will just have them pass the form (or the form's hWnd I guess) as an argument.
Thanks guys.
Re: Getting the absolute position of a control without knowing its parent
Quote:
Originally Posted by brucevde
Don't all controls have a Parent property, which is the Form it is sited upon.
I think so, not 100% sure tho. The downside of using Parent is that it does not take into account any frames or other container controls, hence the use of Container in my code (which may or may not be valid for all controls).
Quote:
I guess I could have the user pass the form as an argument when intializing the control, but I was trying to avoid that. If there is no API way to do it then I will just have them pass the form (or the form's hWnd I guess) as an argument.
You would also need to specify any container controls such as frames that the control is on, as these alter the actual position of the control. Bear in mind that you can have frames within frames, and pictureboxes on those frames that contain controls. :sick:
Re: Getting the absolute position of a control without knowing its parent
Thanks SI.
Since the control that my class is targeted for will be alginable on an MDI form, there isn't really much of a problem of multiple frames, etc (as far as I can see) because it will only function the way it is suppose to if the control is placed directly on the MDI form.
If I have the user pass the parent form as an agurment then I can use the .Top and .Left of the control to know if the control is layered with other alignable controls AND I can use the GetWindowRect to get position of the form on the screen.
The reason I need the absolute value of the window is because I am extending a class JA made that draws ontop of all windows. Here is the the thread.
Thanks again SI. :)
Re: Getting the absolute position of a control without knowing its parent
Quote:
Originally Posted by
si_the_geek
Do you mean that a control will be passed as a parameter to a sub/function?
If so, something like this should work:
VB Code:
Dim lLeft As Long
Dim lTop As Long
Dim oObject As Object
Set oObject = [i]<parameter name>[/i]
lLeft = oObject.Left
lTop = oObject.Top
Do While Not (TypeOf oObject Is Form)
Set oObject = oObject.Container
lLeft = lLeft + oObject.Left
lTop = lTop + oObject.Top
Loop
MsgBox lLeft & vbCr & lTop
I had to make a couple of mods to make it work:
Dim tLeft As Long
Dim tTop As Long
Dim tObject As Object
tObject = uDialogue.Parent
tLeft = tObject.Left
tTop = tObject.Top
Do While Not (TypeOf tObject Is Form)
Try
' tObject = tObject.Container
tObject = tObject.parent
Catch ex As Exception
Exit Do
End Try
If tObject IsNot Nothing Then
tLeft = tLeft + tObject.Left
tTop = tTop + tObject.Top
End If
Loop
Re: Getting the absolute position of a control without knowing its parent
getwindowrect api for parent control with hwnd.
like :
picture1> frame1 > label1