PDA

View Full Version : Attach a Control to Menu bar?



garretthylltun
08-06-2014, 02:15
Not sure this is possible and still very very new to thinBasic here. I'm trying to see if I can add a label and a button to the menu bar of my UI app. I tried grabbing the ID of the menu bar to use for the control add, but yeah, not working out as I had hoped.

Is it possible to add any control to the menu bar?

If yes, could someone please give me some hints to look for in the help file?

Thanks in advance and any help is greatly appreciated,
~Garrett

Petr Schreiber
08-06-2014, 08:58
Hi Garret,

there might be some hack to do so, but the menu isn't part of the window client area, which means you are not allowed to enhance it with other controls by default.


Petr

Petr Schreiber
08-06-2014, 11:55
...but there is always a way, right?

If you don't insist on menu bar, you can manage custom menu showing. Checkout this example (thanks Eros for inspiration):


Uses "UI"

Begin ControlID
%closeButton

%menuLabel
%menuItemFirst
%menuItemSecond
End ControlID

Declare Function TrackPopupMenu Lib "USER32.DLL" Alias "TrackPopupMenu" (ByVal hMenu As DWord, ByVal uFlags As DWord, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hWnd As DWord, lprc As RECT) As Long

Function TBMain()
DWord hDlg

Dialog New Pixels, 0, "Custom menu handling", -1, -1, 320, 240, %WS_POPUP | %WS_VISIBLE | %WS_CLIPCHILDREN | %WS_CAPTION | %WS_SYSMENU | %WS_MINIMIZEBOX To hDlg

Control Add Button, hDlg, %closeButton, "Click to kill", 210, 205, 100, 25
Control Add Label, hDlg, %menuLabel, "Click me for menu", 10, 10, 100, 25, %SS_NOTIFY | %SS_CENTER | %SS_CENTERIMAGE | %WS_BORDER

Dialog Show Modal hDlg Call cbDialog

End Function
' -- ---------------------------------------------
'CallBack Function used To Handle Dialog events
' -- ---------------------------------------------
CallBack Function cbDialog() As Long

Select Case Callback_Message

Case %WM_COMMAND

Select Case Callback_Control

Case %closeButton
If Callback_Control_Message = %BN_CLICKED Then
Dialog End Callback_Handle
End If

Case %menuLabel
If Callback_Control_Message = %BN_CLICKED Then

DWord hChildMenu
DWord lResult
Local cr As CHARRANGE
Local position As POINTAPI
DWord width, height

' -- Create menu
MENU New Popup To hChildMenu
MENU Add Popup, 0, "Custom menu", hChildMenu, 4
MENU Add String, hChildMenu, "First", %menuItemFirst, %MF_ENABLED
MENU Add String, hChildMenu, "Second", %menuItemSecond, %MF_ENABLED

' -- Get position of the clicked control
Control Get Loc Callback_Handle, %menuLabel To position.x, position.y
' -- Get the size
Control Get Size Callback_Handle, %menuLabel To width, height

' -- Shift the position below the control
position.y += height

' -- Convert local window position to screen position
Win_ClientToScreen Callback_Handle, position

' -- Show and handle menu.
' -- Important: use %TPM_RETURNCMD otherwise the choosed command will not be returned
lResult = TrackPopupMenu(hChildMenu, %TPM_LEFTALIGN Or %TPM_RIGHTBUTTON Or %TPM_RETURNCMD, position.x, position.y, 0, Callback_Handle, ByVal %NULL)

' -- Handle menu events
Select Case lResult
Case %menuItemFirst
MsgBox Callback_Handle, "First item clicked"

Case %menuItemSecond
MsgBox Callback_Handle, "Second item clicked"

End Select

End If

End Select

End Select
End Function



Petr

garretthylltun
08-06-2014, 18:27
Thank you Petr, this might be a good way around this. :-)

~Garrett