Screen position of commandbar button in Excel 2007 ?
For me .ActionControl.Left/Top fail in both 2003 & 2007 with the clicked
button button in a popup bar. However
..ActionControl.Parent.Left/Top seems OK. So it means you'll need to work out
the offset of your known button from the top/left of the bar and size,
should be relatively straightforward. Maybe something like this (only
lightly tested) -
Private Declare Function GetAsyncKeyState Lib "user32.dll" ( _
ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib "user32.dll" ( _
ByRef lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Function GetRelVertical() As Currency
Dim idx As Long
Dim pCur As POINTAPI
Dim pBtnSize As POINTAPI, pBtnTL As POINTAPI
Dim RelHoriz As Currency, RelVert As Currency
If GetAsyncKeyState(vbKeyReturn) < 0 Then
GetRelVertical = 12345 ' Enter pressed
Exit Function
End If
GetCursorPos pCur
With Application.CommandBars.ActionControl
idx = .Index
pBtnSize.x = .Width
pBtnSize.y = .Height
pBtnTL.x = .Parent.Left + 2
pBtnTL.y = .Parent.Top + 2 + (idx - 1) * (pBtnSize.y + 1)
End With
RelHoriz = (pCur.x - pBtnTL.x) / pBtnSize.x
RelVert = (pCur.y - pBtnTL.y) / pBtnSize.y
GetRelVertical = RelVert ' should be a decimal 0-1
' Debug.Print RelHoriz, RelVert
End Function
Return GetRelVertical as the first line in the macro called from the popup.
Might need to experient a bit but this seemed pretty accurate for me in both
2003/2007
Not sure if you want the relative horizontal or vertical clicked position,
both in the demo.
I assume if user selects a popup button with the keyboard and presses Enter
you need to know about it (just added that in at the last moment)
Regards,
Peter T
"minimaster" wrote in message
...
I have some add-ins with custom commandbars that have buttons on them
that show a popup menu below the commandbar button when clicked . That
worked fine so far up to Excel 2003.
However in Excel 2007
Application.CommandBars.ActionControl.Top
and
Application.CommandBars.ActionControl.Left
does not deliver anymore the information about the screen position of
the clicked button, so as a consequence my popup menu does get
displayed at the wrong position in Excel 2007. Is there any
workaround, maybe with an Windows API call to get the position of the
commandbar button? I'm explicity interested in the position of the
commandbar button, not the position of the mouse click, because aI
have another button on the commandbar simulates a spinbutton control,
and for that I need to determine whether the mouse click was in the
upper or lower half of the "spin button" on the commandbar.
|