Page 1 of 1

[Windows Vista/7] Split buttons with drop down arrow

Posted: Sun Apr 04, 2010 5:38 pm
by Arctic Fox
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 :)

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

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Sun Apr 04, 2010 5:46 pm
by KJ67
Impressive :D

Just for clarity, maybe also update the shown text on the button as the user switches alternative?

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Sun Apr 04, 2010 5:52 pm
by Arctic Fox
Thanks! :D
KJ67 wrote:Just for clarity, maybe also update the shown text on the button as the user switches alternative?
You mean something like this? (see the code my first post)

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Sun Apr 04, 2010 5:55 pm
by KJ67
Nice, now I know what I press.... :wink:

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Sun Apr 04, 2010 6:29 pm
by RASHAD
Hi Fox
Very nice and very new to PB
But add compiler check for windows xp themed enabled

Thanks for sharing

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Sun Apr 04, 2010 8:03 pm
by netmaestro
Nice work and thanks for sharing. I would have grumbled about no code indentation but with 4.50 it's one press of the mouse, hehe.

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Sun Apr 04, 2010 11:36 pm
by Arctic Fox
netmaestro wrote:I would have grumbled about no code indentation but with 4.50 it's one press of the mouse, hehe.
:lol:
One of my bad habits :wink:

Edit
Indentation added 8)

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Mon Apr 05, 2010 3:08 pm
by +18
is there any chance for run it in WinXP? :?:

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Mon Apr 05, 2010 4:25 pm
by Arctic Fox
+18 wrote:is there any chance for run it in WinXP? :?:
Perhaps it is possible - but I think ComCtl32 version 6 has to be present in the computer.

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Re: [Windows Vista/7] Split buttons with drop down arrow

Posted: Sat Sep 07, 2013 8:25 pm
by nalor
I stumbled across this thread and started thinking about the WinXP support of the SplitButton and to make it short: it's not supported in WinXP.

MS clearly says (http://msdn.microsoft.com/en-us/library ... PLITBUTTON ):

Windows Vista and Version 6.00

So even with Common Controls Version 6.00 on XP no SplitButton is possible :(