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

Share your advanced PureBasic knowledge/code with the community.
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

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

Post 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
Last edited by Arctic Fox on Mon Apr 05, 2010 12:40 pm, edited 3 times in total.
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

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

Post by KJ67 »

Impressive :D

Just for clarity, maybe also update the shown text on the button as the user switches alternative?
The best preparation for tomorrow is doing your best today.
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

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

Post 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)
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

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

Post by KJ67 »

Nice, now I know what I press.... :wink:
The best preparation for tomorrow is doing your best today.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Hi Fox
Very nice and very new to PB
But add compiler check for windows xp themed enabled

Thanks for sharing
Egypt my love
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

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

Post 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)
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

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

Post by +18 »

is there any chance for run it in WinXP? :?:
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

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

Post 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
nalor
Enthusiast
Enthusiast
Posts: 121
Joined: Thu Apr 02, 2009 9:48 pm

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

Post 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 :(
Post Reply