WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Dialog Editor version 6.X
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Toolbar UDFs for Drop Down Menus

 Keywords: WIL Dialog Drop down menus 

;Tool Bar Control 1.2
;This udfs create a flat ToolBar with tooltips and use the system-defined image lists
;for the buttons.
;
;1.2 : added dropdown menus, to use them the dialog procedure has to be a subroutine
;
;Ref : http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/toolbar/reflist.asp

;Each toolbar button has a 'dummy' (invisible) standard WIL button, you must create
;a dummy button for each toolbar button. You can handle the toolbar button click with
;the dummy button id in the dialog callback.
;Button separators don't need a dummy button. 

;Guido 03/03

;-----------------------------------------------------------------------------------;
;TBInitConstants : This subroutine initializes the constants used by the toolbar,   ;
;                  you can call it in the init section of the dialog.               ;
;-----------------------------------------------------------------------------------;
#definesubroutine TBInitConstants()
;System-defined button image lists
IDB_STD_SMALL_COLOR     = 0
IDB_STD_LARGE_COLOR     = 1
IDB_VIEW_SMALL_COLOR    = 4
IDB_VIEW_LARGE_COLOR    = 5
IDB_HIST_SMALL_COLOR    = 8
IDB_HIST_LARGE_COLOR    = 9

;Index values for IDB_HIST_LARGE_COLOR and IDB_HIST_SMALL_COLOR:
HIST_BACK           = 0
HIST_FORWARD        = 1
HIST_FAVORITES      = 2
HIST_ADDTOFAVORITES = 3
HIST_VIEWTREE       = 4

;Index values for IDB_STD_LARGE_COLOR and IDB_STD_SMALL_COLOR:
STD_CUT         = 0
STD_COPY        = 1
STD_PASTE       = 2
STD_UNDO        = 3
STD_REDOW       = 4
STD_DELETE      = 5
STD_FILENEW     = 6
STD_FILEOPEN    = 7
STD_FILESAVE    = 8
STD_PRINTPRE    = 9
STD_PROPERTIES  = 10
STD_HELP        = 11
STD_FIND        = 12
STD_REPLACE     = 13
STD_PRINT       = 14

;Index values for IDB_VIEW_LARGE_COLOR and IDB_VIEW_SMALL_COLOR:
VIEW_LARGEICONS     = 0
VIEW_SMALLICONS     = 1
VIEW_LIST           = 2
VIEW_DETAILS        = 3
VIEW_SORTNAME       = 4
VIEW_SORTSIZE       = 5
VIEW_SORTDATE       = 6
VIEW_SORTTYPE       = 7
VIEW_PARENTFOLDER   = 8
VIEW_NETCONNECT     = 9
VIEW_NETDISCONNECT  = 10
VIEW_NEWFOLDER      = 11
VIEW_VIEWMENU       = 12

;Button States
TBSTATE_CHECKED        = 1
TBSTATE_PRESSED        = 2
TBSTATE_ENABLED        = 4
TBSTATE_HIDDEN         = 8
TBSTATE_INDETERMINATE  = 16
TBSTATE_WRAP           = 32
TBSTATE_ELLIPSES       = 64
TBSTATE_MARKED         = 128

;Button Styles
TBSTYLE_BUTTON         = 0
TBSTYLE_SEP            = 1
TBSTYLE_CHECK          = 2
TBSTYLE_GROUP          = 4
TBSTYLE_CHECKGROUP     = TBSTYLE_GROUP | TBSTYLE_CHECK
TBSTYLE_DROPDOWN       = 8
TBSTYLE_AUTOSIZE       = 16
TBSTYLE_NOPREFIX       = 32
BTNS_SHOWTEXT          = 64
BTNS_WHOLEDROPDOWN     = 128 

;dropdown menus
MF_STRING              = 0
MF_SEPARATOR           = 2048
MF_CHECKED             = 8
MF_DISABLED            = 2
MF_ENABLED             = 0
MF_GRAYED              = 1
MF_UNCHECKED           = 0
#endsubroutine

;-----------------------------------------------------------------------------------;
;TBCreate : Creates a flat tool bar at the top of the dialog.                       ;
;-----------------------------------------------------------------------------------;
;hdlg    : dialog handle                                                            ;
;-----------------------------------------------------------------------------------;
;Returns : toolbar handle                                                           ;
;-----------------------------------------------------------------------------------;
#definefunction TBCreate(hdlg)
comctl32=strcat(dirwindows(1),"comctl32.dll")
user32=strcat(dirwindows(1),"user32.dll")
WS_CHILD=1073741824
WS_VISIBLE=268435456
TBSTYLE_TOOLTIPS=256
TBSTYLE_FLAT=2048
dllcall(comctl32,long:"InitCommonControls") ;make sure common lib is loaded
return dllcall(user32,long:"CreateWindowExA",long:0,lpstr:"ToolbarWindow32",lpstr:"",long:WS_VISIBLE|WS_CHILD|TBSTYLE_FLAT|TBSTYLE_TOOLTIPS,long:0,long:0,long:0,long:0,long:hdlg,long:0,long:dllhinst(""),long:0)
#endfunction

;---------------------------------------------------------------------------------------;
;TBLoadImages : Loads system-defined button images into a toolbar control's image list. ;
;---------------------------------------------------------------------------------------;
;htb      : toolbar handle                                                              ;
;BitmapID : Identifier of a system-defined button image list, see TBInitConstants()     ;
;           This parameter can be set to one of the following values:                   ;
;           IDB_HIST_LARGE_COLOR                                                        ;
;             Microsoft® Windows® Explorer bitmaps in large size.                       ;
;           IDB_HIST_SMALL_COLOR                                                        ;
;             Microsoft® Windows® Explorer bitmaps in small size.                       ;
;           IDB_STD_LARGE_COLOR                                                         ;
;             Standard bitmaps in large size.                                           ;
;           IDB_STD_SMALL_COLOR                                                         ;
;             Standard bitmaps in small size.                                           ;
;           IDB_VIEW_LARGE_COLOR                                                        ;
;             View bitmaps in large size.                                               ;
;           IDB_VIEW_SMALL_COLOR                                                        ;
;             View bitmaps in small size                                                ;
;---------------------------------------------------------------------------------------;
;Returns  : The count of images in the image list, not including the one just added.    ;
;           Returns zero if the toolbar has no image list or if the existing image list ;
;           is empty.                                                                   ;
;---------------------------------------------------------------------------------------;
#definefunction TBLoadImages(htb,BitmapID)
user32=strcat(dirwindows(1),"user32.dll")
TB_LOADIMAGES=1024+50
HINST_COMMCTRL=-1
return dllcall(user32,long:"SendMessageA",long:htb,long:TB_LOADIMAGES,long:BitmapID,long:HINST_COMMCTRL)
#endfunction    

;----------------------------------------------------------------------------------;
;TBAddButtons : Adds buttons to a toolbar.                                         ;
;----------------------------------------------------------------------------------;
;htb  : toolbar handle                                                             ;
;barr : 2 dimensional array that contains the data for each button.                ;
;       Array structure:                                                           ;
;       The 1st element (0,0) contains the number of buttons                       ;
;                                                                                  ;
;       Following are the elements that define each button:                        ;
;       n=1 : increment this value by one for each button                          ;
;                                                                                  ;
;       (n,1) button index   : the button image, see TBInitConstants() for values, ;
;                              if you use a button separator this is the width of  ;
;                              the separator in pixels.                            ;
;       (n,2) button id      : real dummy button id obtained using roboscripter    ;
;                              or : 100 is the id of the 1st control that appears  ;
;                                   in the dialog template, and it is incremented  ;
;                                   by one for each new control.                   ;
;       (n,3) button state   : see TBInitConstants() for values                    ;
;       (n,4) button style   : see TBInitConstants() for values                    ;
;       (n,5) button tooltip : the max len is set to 255 chars                     ;
;                                                                                  ;
;       We need 5 parameters for each button, plus the (0,0) element, so the final ;
;       array definition will always be: arrdimension(nbuttons+1,6)                ;
;                                                                                  ;
;       See test example.                                                          ;
;----------------------------------------------------------------------------------;
#definefunction TBAddButtons(htb,barr)
user32=strcat(dirwindows(1),"user32.dll")
kernel32=strcat(dirwindows(1),"kernel32.dll")
WM_USER=1024
TB_ADDBUTTONS=WM_USER+20
TB_BUTTONSTRUCTSIZE=WM_USER+30
TB_GETTOOLTIPS=WM_USER+35
TB_GETITEMRECT=WM_USER+29
TBSTYLE_SEP=1

TTF_SUBCLASS=16
TTM_ADDTOOL=WM_USER+4

MAXTTLEN=256

nbuttons=barr[0,0]
stsize=nbuttons*18
aTBBUTTON=binaryalloc(stsize)

;add buttons
bos=0
for nb=1 to nbuttons
  binarypoke4(aTBBUTTON,bos,barr[nb,1]) ;iBitmap
  bos=bos+4
  binarypoke4(aTBBUTTON,bos,barr[nb,2]) ;idCommand
  bos=bos+4
  binarypoke(aTBBUTTON,bos,barr[nb,3])  ;fsState
  bos=bos+1
  binarypoke(aTBBUTTON,bos,barr[nb,4])  ;fsStyle
  bos=bos+9
next

dllcall(user32,long:"SendMessageA",long:htb,long:TB_BUTTONSTRUCTSIZE,long:18,long:0)
dllcall(user32,long:"SendMessageA",long:htb,long:TB_ADDBUTTONS,long:nbuttons,lpbinary:aTBBUTTON)

;add tooltips
htt=dllcall(user32,long:"SendMessageA",long:htb,long:TB_GETTOOLTIPS,long:0,long:0)
RECT=binaryalloc(16)
TOOLINFO=binaryalloc(44)
tooltxt=binaryalloc(MAXTTLEN)
tooltxtad=intcontrol(42,tooltxt,0,0,0)
binarypoke4(TOOLINFO,0,44) ;cbSize
binarypoke4(TOOLINFO,4,TTF_SUBCLASS) ;uFlags
binarypoke4(TOOLINFO,8,htb) ;hwnd
;uId
;rect
binarypoke4(TOOLINFO,32,dllhinst("")) ;hinst
for nb=1 to nbuttons
  if barr[nb,4]==TBSTYLE_SEP || barr[nb,5]=="" then continue ;no tooltips for separators 
  
  dllcall(user32,long:"SendMessageA",long:htb,long:TB_GETITEMRECT,long:nb-1,lpbinary:RECT)

  binarypoke4(TOOLINFO,16,binarypeek4(RECT,0)) ;left
  binarypoke4(TOOLINFO,20,binarypeek4(RECT,4)) ;top
  binarypoke4(TOOLINFO,24,binarypeek4(RECT,8)) ;right
  binarypoke4(TOOLINFO,28,binarypeek4(RECT,12));bottom
  
  binarypokestr(tooltxt,0,barr[nb,5]) ;tooltxt
  binarypoke4(TOOLINFO,36,tooltxtad)  ;lpszText
  
  dllcall(user32,long:"SendMessageA",long:htt,long:TTM_ADDTOOL,long:0,lpbinary:TOOLINFO)

  dllcall(kernel32,long:"RtlZeroMemory",long:tooltxtad,long:MAXTTLEN) ;reset txt buffer
next
binaryfree(aTBBUTTON)
binaryfree(TOOLINFO)
binaryfree(RECT)
binaryfree(tooltxt)
#endfunction

;-------------------------------------------------------------------------;
;TBCheckButton : Checks or unchecks a given button in a toolbar.          ;
;-------------------------------------------------------------------------;
;htb   : toolbar handle                                                   ;
;idBut : Command identifier of the button to check                        ;
;chk   : Check flag. If this parameter is TRUE, the check is added.       ;
;        If it is FALSE, the check is removed.                            ;
;-------------------------------------------------------------------------;
;Returns : TRUE if successful, or FALSE otherwise                         ;
;-------------------------------------------------------------------------;
#definefunction TBCheckButton(htb,idBut,chk)
  user32=strcat(dirwindows(1),"user32.dll")
  TB_CHECKBUTTON=1024+2
  return dllcall(user32,long:"SendMessageA",long:htb,long:TB_CHECKBUTTON,long:idbut,long:chk)
#endfunction

;--------------------------------------------------------------------------;
;TBShowPopup: Displays a dropdown menu associated with a toolbar button and;
;             waits until a selection is done.                             ;
;             This function creates one level menus only.                  ;
;--------------------------------------------------------------------------;
;TB_MENUA : 2 dimensional array that defines all the menus                 ;
;mindex   : 1 based index of the menu to display in the array              ;
;--------------------------------------------------------------------------;
;Returns  : Menu item id that has been clicked or 0 if no selection.       ;
;--------------------------------------------------------------------------;
;           TB_MENUA array structure:                                      ;
;           the first 2 members are general info:                          ;
;           (0,0) dialog handle                                            ;
;           (0,1) toolbar handle                                           ;
;                                                                          ;
;           following are the members that define each dropdown menu:      ;
;           n=1   : increment by one for each new dropdown menu            ;
;                                                                          ;
;           dropdown menu info                                             ;
;           (n,0) number of menu items in this dropdown menu               ;
;           (n,1) toolbar button id associated with this menu              ;
;                                                                          ;
;           menu item info                                                 ;
;           (n,2) menu item type (MF_STRING , MF_SEPARATOR)                ;
;           (n,3) menu item id , the value this function will return       ;
;           (n,4) menu item text                                           ;
;                                                                          ;
;           The definition for the array will be always:                   ;
;           ndrop      number of dropdown menus                            ;
;           nitemsmax  max number of menu items a dropdown can have        ;
;           TB_MENUA=arrdimension(ndrop+1,(nitemsmax*3)+2)                 ;
;                                                                          ;
;           See example.                                                   ;
;--------------------------------------------------------------------------;
#definefunction TBShowPopup(TB_MENUA,mindex)
  user32=strcat(dirwindows(1),"user32.dll")
  WM_USER=1024
  TB_GETRECT=WM_USER+51
  HWND_DESKTOP=0
  TPM_LEFTALIGN=0
  TPM_LEFTBUTTON=0
  TPM_VERTICAL=64
  TPM_RETURNCMD=256

  RECT=binaryalloc(16)

  ;general info
  hdlg=TB_MENUA[0,0]
  htb=TB_MENUA[0,1]
  
  ;drop down info
  nitems=TB_MENUA[mindex,0]
  butid=TB_MENUA[mindex,1] 
 
  dllcall(user32,long:"SendMessageA",long:htb,long:TB_GETRECT,long:butid,lpbinary:RECT)                       
  dllcall(user32,long:"MapWindowPoints",long:htb,long:HWND_DESKTOP,lpbinary:RECT,long:2)
        
  hpopup=dllcall(user32,long:"CreatePopupMenu")
  
  for x=2 to (nitems*3)+1 by 3
    style=TB_MENUA[mindex,x]
    mid=TB_MENUA[mindex,x+1]
    text=TB_MENUA[mindex,x+2]
    dllcall(user32,long:"AppendMenuA",long:hpopup,long:style,long:mid,lpstr:text)
  next
  
  r=dllcall(user32,long:"TrackPopupMenuEx",long:hpopup,long:TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL|TPM_RETURNCMD,long:binarypeek4(RECT,0),long:binarypeek4(RECT,12),long:hdlg,lpnull)

  dllcall(user32,long:"DestroyMenu",long:hpopup)
  binaryfree(RECT)
  return r
#endfunction
   

;-----------------------------------------------------------------------------------
;TEST
;-----------------------------------------------------------------------------------

;DIALOG CALLBACK
#Definesubroutine dlgproc(hdlg,msg,id,p4,p5)
Switch msg
  Case 0 ;Init
    DialogProcOptions(hdlg,2,1) ;Push buttons
    ;real WIL buttons ids
    ID_BACKBUT=100
    ID_TREEBUT=101
    ID_FAVBUT=102
        
    ;set up toolbar
    TBInitConstants()
    
    ;make button array
    nbuttons=4
    barr=arrdimension(nbuttons+1,6) ;always the same
    barr[0,0]=nbuttons

    ;but 1
    barr[1,1]=HIST_BACK       ;iBitmap
    barr[1,2]=ID_BACKBUT      ;idCommand
    barr[1,3]=TBSTATE_ENABLED ;fsState
    barr[1,4]=TBSTYLE_CHECK   ;fsStyle
    barr[1,5]=""              ;tooltip

    ;but 2
    barr[2,1]=HIST_VIEWTREE    
    barr[2,2]=ID_TREEBUT             
    barr[2,3]=TBSTATE_ENABLED 
    barr[2,4]=TBSTYLE_BUTTON
    barr[2,5]="Tree"

    ;but 3 Separator
    barr[3,1]=0    
    barr[3,2]=0             
    barr[3,3]=TBSTATE_ENABLED 
    barr[3,4]=TBSTYLE_SEP
    barr[3,5]=""

    ;but 4
    barr[4,1]=HIST_ADDTOFAVORITES    
    barr[4,2]=ID_FAVBUT             
    barr[4,3]=TBSTATE_ENABLED 
    barr[4,4]=TBSTYLE_BUTTON
    barr[4,5]=""
    
    ;create toolbar
    htb=TBCreate(hdlg)
    TBLoadImages(htb,IDB_HIST_SMALL_COLOR)
    TBAddButtons(htb,barr)

    
    ;MENUS
    ndrop=2      ;number of dropdown menus
    nitemsmax=3  ;max number of menu items a dropdown can have
    
    TB_MENUA=arrdimension(ndrop+1,(nitemsmax*3)+2) ;always the same

    ;general data
    TB_MENUA[0,0]=hdlg      ;dialog handle
    TB_MENUA[0,1]=htb       ;toolbar handle
    
    ;1st drop down
    nitems=3
    TB_MENUA[1,0]=nitems     ;menu items in this dropdown
    TB_MENUA[1,1]=ID_BACKBUT ;toolbar button id (real wil button id)
    
    ;1st item
    TB_MENUA[1,2]=MF_STRING ;menu item type
    TB_MENUA[1,3]=1         ;menu item id
    TB_MENUA[1,4]="hello"   ;menu item text
    ;2nd item
    TB_MENUA[1,5]=MF_SEPARATOR 
    TB_MENUA[1,6]=-1         
    TB_MENUA[1,7]=""   
    ;3rd item
    TB_MENUA[1,8]=MF_STRING 
    TB_MENUA[1,9]=2          
    TB_MENUA[1,10]="world"   

    
    ;2nd drop down
    nitems=1
    TB_MENUA[2,0]=nitems         
    TB_MENUA[2,1]=ID_FAVBUT    
    
    ;1st item
    TB_MENUA[2,2]=MF_STRING 
    TB_MENUA[2,3]=1         
    TB_MENUA[2,4]="Foo"    
  
    drop(barr) ;free button array
    break           

  Case 2 ;Button pushed
    Switch id      
      Case 1 ;Back 
        select TBShowPopup(TB_MENUA,1)
          case 1
            message("","hello")
            break

          case 2
            message("","world")
            break
        endselect
        
        ;uncheck button
        TBCheckButton(htb,ID_BACKBUT,0)
      
        Return -2
        Break

      case 2 ;Tree
        message("","Tree")
        Return -2
        Break

      Case 3 ;Favorites
        select TBShowPopup(TB_MENUA,2)
          case 1
            message("","foo")
            break

        endselect
        Return -2
        Break

  EndSwitch
EndSwitch
Return -1
#Endsubroutine

IntControl (49, 1, 0, 0, 0)
;DIALOG TEMPLATE
MyDialogFormat=`WWWDLGED,6.1`

MyDialogCaption=`ToolBar Control`
MyDialogX=-1
MyDialogY=-1
MyDialogWidth=318
MyDialogHeight=174
MyDialogNumControls=003
MyDialogProcedure=`dlgproc`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=4513660

MyDialog001=`185,151,036,012,PUSHBUTTON,DEFAULT,"Back",1,1,33,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`225,151,036,012,PUSHBUTTON,DEFAULT,"Tree",0,2,1,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`265,151,036,012,PUSHBUTTON,DEFAULT,"Favorites",2,3,1,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("MyDialog",1)

drop(TB_MENUA)
 

Article ID:   W15480
File Created: 2003:05:13:11:28:06
Last Updated: 2003:05:13:11:28:06