tags:

views:

746

answers:

4

I'm trying to display one form relative to a Button on a control below it.

But Button.top is relative to the titlebar of the bottom form, and the top form will be relative to the screen.

So, to compensate for that I need to now how tall the titlebar is.

I've used Form.height-Form.ScalehHeight but ScaleHeight doesn't include the title bar or the border so Scaleheight is inflated slightly.

Anyone know how to calculate the height of just the title bar?

+1  A: 

You'll probably need to make a Win32 API call to GetSystemMetrics()

ahockley
+3  A: 

You need to use the GetSystemMetrics API call to get the height of the titlebar.

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Const SM_CYCAPTION = 4

Property Get TitleBarHeight() as Long
    TitleBarHeight = GetSystemMetrics(SM_CYCAPTION)
End Property

Note: This will return the height in pixels. If you need twips you will have to convert using a form's ScaleY method like so: Me.ScaleY(TitleBarHeight(), vbPixels, vbTwips)

rpetrich
+3  A: 

Subract it back out:

(Form.height-Form.ScaleHeight) - (Form.Width-Form.ScaleWidth) / 2
recursive
Elegant!FYI, There's a typo, fixed below. (I don't have enough rep to edit)(Form.height-Form.ScaleHeight) - (Form.Width-Form.ScaleWidth)
Clay Nichols
Sneaky. I like it!
rpetrich
We use the same technique. There is still a typo. As written above, you are subtracting twice the border. Should be (Form.height-Form.ScaleHeight) - (Form.Width-Form.ScaleWidth)/2
MarkJ
A: 

"Recursive's" answer above is not quite correct. It subtracts twice the border width - there is a border on the left and one on the right!

We get the best results with this:

(Form.Height-Form.ScaleHeight) - (Form.Width-Form.ScaleWidth)/2
MarkJ