Date: 02-16-2022
Return to Index
created by gbSnippets
'Toolbar buttons with the style %TbStyle_Dropdown will display down arrows
'when the toolbar is also given the extended style %TbStyle_EX_Drawddarrows.
'The dropdown portion is a popup menu, called using TrackPopUp menu, which is
'called from the WM_Notify message that is sent when a dropdown item is selected.
'Like any popup, the popup can be sent to any location on the screen. The code
'below gets the position of the toolbar button to use for positioning the popup menu.
'Primary Code:
'set the toolbar extended style
SendMessage hToolbar, %TB_SETEXTENDEDSTYLE, 0, %TBSTYLE_EX_DRAWDDARROWS
'add toolbars with style %tbstyle_dropdown
Toolbar Add Button hDlg, 500, 1, 200, %TbStyle_DropDown, "x"
'create a popup menu
Menu New Popup To hPopup
Menu Add String, hPopup, "Hello!", 300, %MF_Enabled 'note ID =200
'put code in %WM_Notify to watch for notifications, calling TrackPopupMenu as needed
'and using the position of toolbar button as the location at which to display the popup menu
If CB.Msg = %WM_Notify Then
Local nmtb As TBNOTIFY Ptr, rc As RECT
nmtb = CB.lParam
Select Case @nmtb.hdr.Code
Case %TBN_DROPDOWN
Select Case @nmtb.iItem
Case 200
SendMessage(hToolbar, %TB_GETRECT, 200, VarPTR(rc))
MapWindowPoints(hToolbar, %HWND_Desktop, ByVal VarPTR(rc), 2)
TrackPopupMenu (hPopUp, 0, rc.nLeft, rc.nBottom, 0, hDlg, ByVal %NULL)
End Select
End Select
End If
End Function
'Compilable Example: (Jose Includes)
'The following compilable code demonstrates a dialog with a Toolbar control.
'A resource file, imagelist, and toolbar buttons are used.
'The Dialog CallBack response a toolbar button press is demonstrated.
'Controls can also have a Callback function of their own.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
#Resource "gbsnippets.pbr" 'misc icons
Global hDlg As Dword 'main dialog handle
Global hLst As Dword 'imagelist handke
Global hToolbar as Dword
Global hPopUp as Dword 'popup for use with dropdown menu
Function PBMain()
'create dialog
Dialog New Pixels, 0, "Toolbar Test",,, 500,250, %WS_SysMenu, To hDlg
Dialog Set Color hDlg, %White, %White
Dialog Set Icon hDlg, "x"
'add toolbar
Control Add Toolbar, hDlg, 500,"", 0,0,0,0, _
%WS_Child Or %WS_Border Or %WS_Visible Or %CCS_Top Or %TbStyle_Tooltips Or %TB_AUTOSIZE, _
%TbStyle_EX_Drawddarrows
CreateDropDownMenu
' Set the extended class styles for the control
Control Handle hDlg, 500 to hToolbar
SendMessage hToolbar, %TB_SETEXTENDEDSTYLE, 0, %TBSTYLE_EX_DRAWDDARROWS
'create imagelist
ImageList New Icon 16,16,32,3 To hLst
ImageList Add Icon hLst, "x" '1
ImageList Add Icon hLst, "y" '2
ImageList Add Icon hLst, "z" '3
'attach imagelist
Toolbar Set ImageList hDlg, 500, hLst, 0
'create buttons
Toolbar Add Button hDlg, 500, 1, 200, %TbStyle_Button, "x"
Toolbar Add Button hDlg, 500, 2, 201, %TbStyle_Button, "y"
Toolbar Add Button hDlg, 500, 3, 202, %TbStyle_Dropdown, "z"
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command Then
If CB.Ctl = 200 Then MsgBox "Pushed X!"
If CB.Ctl = 201 Then MsgBox "Pushed Y!"
If CB.Ctl = 202 Then MsgBox "Pushed Z!"
If CB.Ctl = 300 Then MsgBox "Pushed First"
If CB.Ctl = 303 Then MsgBox "Pushed Second"
End If
If CB.Msg = %WM_Notify Then
Local nmtb As TBNOTIFY Ptr, rc As RECT
nmtb = CB.lParam
Select Case @nmtb.hdr.Code
Case %TBN_DROPDOWN
Select Case @nmtb.iItem
Case 202
Call SendMessage(@nmtb.hdr.hwndFrom, %TB_GETRECT, @nmtb.iItem, VarPTR(rc))
Call MapWindowPoints(@nmtb.hdr.hwndFrom, %HWND_Desktop, ByVal VarPTR(rc), 2)
Call TrackPopupMenu (hPopUp, 0, rc.nLeft, rc.nBottom, 0, Cbhndl, ByVal %NULL)
End Select
End Select
Function = 0 : Exit Function
End If
End Function
Sub CreateDropDownMenu
Menu New Popup To hPopup
Menu Add String, hPopup, "First", 300, %MF_Enabled
Menu Add String, hPopup, "-", 0, 0
Menu Add String, hPopup, "Second", 303, %MF_Enabled
End Sub
'gbs_00112
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm