[Windows Vista/7] Split buttons with drop down arrow
Posted: Sun Apr 04, 2010 5:38 pm
This example demonstrates the use of split buttons - a button style from Windows Vista.
It must be compiled with XP skin enabled. (thanks RASHAD for pointing this out)
Please let me know if anything can be improved
It must be compiled with XP skin enabled. (thanks RASHAD for pointing this out)
Please let me know if anything can be improved

Code: Select all
EnableExplicit
; CONSTANTS
; http://msdn.microsoft.com/en-us/library/bb775951%28VS.85%29.aspx
#BS_SPLITBUTTON = 12
#BCN_FIRST = -1250
#BCN_DROPDOWN = #BCN_FIRST + 2
#BCM_SETSPLITINFO = 5639
#BCSIF_GLYPH = 1
#BCSIF_IMAGE = 2
#BCSIF_STYLE = 4
#BCSIF_SIZE = 8
#BCSS_NOSPLIT = 1
#BCSS_STRETCH = 2
#BCSS_ALIGNLEFT = 4
#BCSS_IMAGE = 8
; STRUCTURES
; http://msdn.microsoft.com/en-us/library/bb775957%28VS.85%29.aspx
Structure NMBCDROPDOWN
hdr.NMHDR
rcButton.RECT
EndStructure
; http://msdn.microsoft.com/en-us/library/bb775955%28VS.85%29.aspx
Structure BUTTON_SPLITINFO
mask.i
himlGlyph.i
uSplitStyle.i
size.SIZE
EndStructure
Define Event, SplitInfo.BUTTON_SPLITINFO
; Callback procedure.
Procedure WndProc(hWnd, uMsg, wParam, lParam)
Protected *pDropDown.NMBCDROPDOWN = lParam, pt.POINT
; http://msdn.microsoft.com/en-us/library/bb775983%28VS.85%29.aspx
; http://msdn.microsoft.com/en-us/library/bb775949%28VS.85%29.aspx#using_splits
; #BCN_DROPDOWN is sent 'along with' #WM_NOTIFY. Read the notification code from an NMHDR structure (included in an
; NMBCDROPDOWN structure whose pointer is retrieved by lParam).
If uMsg = #WM_NOTIFY And *pDropDown\hdr\code = #BCN_DROPDOWN
; Retrieves the co-ordinates of the button gadget.
pt\x = *pDropDown\rcButton\left
pt\y = *pDropDown\rcButton\bottom
; Converts the co-ordinates from client co-ordinates to screen co-ordinates.
; You can also use *pDropDown\hdr\idFrom instead of wParam.
ClientToScreen_(GadgetID(wParam), @pt)
; Checks gadget number and displays a popup menu at the retrieved co-ordinates.
Select wParam
Case 0
Debug "Drop down: Split button 1"
DisplayPopupMenu(0, hWnd, pt\x, pt\y)
Case 1
Debug "Drop down: Split button 2"
DisplayPopupMenu(1, hWnd, pt\x, pt\y)
EndSelect
EndIf
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
OpenWindow(0, 100, 100, 300, 35, "Split buttons", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
ButtonGadget(0, 5, 5, 125, 25, "Split button 1", #BS_SPLITBUTTON)
ButtonGadget(1, 170, 5, 125, 25, "Split button 2", #BS_SPLITBUTTON)
CreatePopupMenu(0)
MenuItem(0, "Item 1.1")
MenuItem(1, "Item 1.2")
MenuItem(2, "Item 1.3")
CreatePopupMenu(1)
MenuItem(3, "Item 2.1")
MenuItem(4, "Item 2.2")
MenuItem(5, "Item 2.3")
SetWindowCallback(@WndProc(), 0)
; Sets information for split button 2. Positions the drop down arrow on the left side.
; http://msdn.microsoft.com/en-us/library/bb775981%28VS.85%29.aspx
SplitInfo\mask = #BCSIF_STYLE
SplitInfo\uSplitStyle = #BCSS_ALIGNLEFT
SendMessage_(GadgetID(1), #BCM_SETSPLITINFO, 0, @SplitInfo)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 0
Debug "Click: Split button 1"
Case 1
Debug "Click: Split button 2"
EndSelect
Case #PB_Event_Menu
Select EventMenu()
Case 0
Debug "Popup menu: Item 1.1"
SetGadgetText(0, "Split button 1 (1.1)")
Case 1
Debug "Popup menu: Item 1.2"
SetGadgetText(0, "Split button 1 (1.2)")
Case 2
Debug "Popup menu: Item 1.3"
SetGadgetText(0, "Split button 1 (1.3)")
Case 3
Debug "Popup menu: Item 2.1"
SetGadgetText(1, "Split button 2 (2.1)")
Case 4
Debug "Popup menu: Item 2.2"
SetGadgetText(1, "Split button 2 (2.2)")
Case 5
Debug "Popup menu: Item 2.3"
SetGadgetText(1, "Split button 2 (2.3)")
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
End