The ZAZ Blog: When all you have is a chicken and a rocket launcher, make some really badass scrambled eggs.

Custom Controls: Finding your global screen bounds
« Previous Entry | Home | Next Entry »

I find myself writing a fair amount of custom controls. Frequently, I find myself wanting to find the position of the mouse relative to my control. The MouseDown, MouseDrag, etc. events have this info, but if you need this in the Paint event, you must either save it to a property or get it from the system. We're exploring the latter, as it's not as simple as you might think.

System.MouseX and System.MouseY will tell you where the mouse is. You might think that simply using System.MouseX - Self.Left is sufficient - but it's not. Self.Left and Self.Top only return values relative to the parent window. In today's REALbasic, ContainerControls come into play. In fact, multiple layers of ContainerControls can occur. Then, finding your control's true position on screen can become difficult.

The solution is a method I use call TrueWindow, that extends any Window (and thus ContainerControl) or RectControl. It traverses up the Window.Parent chain until it hits a Window that is not a ContainerControl.

Function TrueWindow(Extends R As RectControl) As Window
return r.window.truewindow
End Function

Function TrueWindow(Extends W As Window) As Window
if w isa containercontrol then
return containercontrol(w).window.truewindow
else
return w
end
End Function

Now technically, this will be enough. But I like to use a couple other tricks too. I usually create a structure, we'll call it ZAZRect, which has four integer properties: Left, Top, Width, and Height. I also create two extender methods for it: Right() and Bottom() which just calculate that for me.

I then use this nice TrueBoundary() method to instantly get the bounding rect of any control relative to the user's screen:

Function TrueBoundary(Extends C As RectControl) As ZAZRect
dim r as zazrect
r.height = c.height
r.width = c.width
r.top = c.top
r.left = c.left

dim w as window = c.window
while w isa containercontrol
r.top = r.top + w.top
r.left = r.left + w.left
w = containercontrol(w).window
wend

r.top = r.top + w.top
r.left = r.left + w.left
return r
End Function

So there's some ground work for some upcoming tutorials I plan to write.

License: Creative Commons (details) | Digg | E-Mail Thom McGrath
View More Entries From The Same: Day | Month | Year

Comments
Comments are being accepted below.

Pings
This entry is accepting pings.

Leave a comment
We utilize unique methods to ensure your email address stays private while remaining useful.
Depending on the authentication scheme used, some or all of the personal data entered below will be ignored in favor of the data stored with your account.


Disclaimer: I am currently an employee of REAL Software. My comments and opinions are mine alone and do not represent those of my employer. My posts are not official REAL Software communications, and may contain information that is incorrect or misleading.