Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Split Button In ListView Header

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin Split Button In ListView Header

    Couldn't find any example of doing this, so here it goes..

    Click image for larger version

Name:	LvDropDown.jpg
Views:	72
Size:	42.6 KB
ID:	783772
    Code:
    '--------------------------------------------------------------------
    ' HeaderDropDown.bas by Borje Hagsten, Aug 10, 2019
    ' Public Domain example for listview control using manifest
    ' to enable features in COMCTL32.DLL version 6 and later.
    ' This one adds a split button to each column in a header control
    ' and how to use that for a column-related menu to show up. It
    ' also shows a neat way to set fixed column width. Not sure why we
    ' need a split button in a ListView Header, but to show it can be done.
    ' Maybe can enable filtered contents or search depending on selection
    ' in the popup menu, or whatever..
    ' Included in HeaderDropDown.zip is this code, resource file with
    ' version info and XPTheme.xml plus compiled exe. Yeah, I like to
    ' use .RC file compiled to .PBR resource best, but you can of course
    ' just as well include version info and manifest directly in PBWin10.
    '--------------------------------------------------------------------
    #COMPILE EXE
    #DIM ALL
    #RESOURCE "LvThemed.PBR"  ' resource file, compiled from LvThemed.rc
    
    %UNICODE = 1
    #INCLUDE "WIN32API.INC"
    #INCLUDE "COMMCTRL.INC"
    
    #IF NOT %DEF(%HDF_FIXEDWIDTH)  ' To enable compile with PBWin9 include files.
      %HDF_FIXEDWIDTH  = &H0100    ' Can't resize the column; same as LVCFMT_FIXED_WIDTH
      %HDF_SPLITBUTTON = &H1000000 ' Column is a split button; same as LVCFMT_SPLITBUTTON
      %HDN_DROPDOWN    = (%HDN_FIRST-18)  ' Split button notification
    #ENDIF
    
    %IDC_LABEL    = 101
    %IDC_LISTVIEW = 221
    %IDC_HEADER   = 321
    %IDM_POPUP1   = %WM_USER + 500
    %IDM_POPUP2   = %WM_USER + 501
    
    '#RESOURCE MANIFEST, 1, "XPTheme.xml" ' if you like this way better than .PBR resource
    
    '====================================================================
    FUNCTION PBMAIN
       LOCAL c, hDlg AS LONG
    
       DIALOG NEW 0, "DDT - Header DropDown Example",,, 345, 152, _
                  %WS_MINIMIZEBOX OR %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
    
      CONTROL ADD LABEL, hDlg, %IDC_LABEL, "", 5, 121, 334, 26, %SS_SUNKEN
      CONTROL SET COLOR hDlg, %IDC_LABEL, %BLUE, -1
    
      InitCommonControls
      '------------------------------------------------------------------
      CONTROL ADD LISTVIEW, hDlg, %IDC_LISTVIEW,"", 5, 5, 334, 110, _
                  %WS_CHILD OR %WS_TABSTOP OR %LVS_REPORT OR _
                  %LVS_SHOWSELALWAYS OR %LVS_SINGLESEL, %WS_EX_CLIENTEDGE
      LISTVIEW SET STYLEXX hDlg, %IDC_LISTVIEW, %LVS_EX_GRIDLINES OR %LVS_EX_FULLROWSELECT
    
      '------------------------------------------------------------------
      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 1, "Column 1", 80,0
      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 2, "Column 2", 80,0
      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 3, "Column 3", 80,0
      LISTVIEW INSERT COLUMN hDlg, %IDC_LISTVIEW, 4, "Column 4", 80,0
    
      FOR c = 1 TO 20
          LISTVIEW INSERT ITEM hDlg, %IDC_LISTVIEW, c, 0, "Line " + FORMAT$(c)
          LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW,    c, 2, "Item " + FORMAT$(c)
          LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW,    c, 3, "Item " + FORMAT$(c)
          LISTVIEW SET TEXT hDlg, %IDC_LISTVIEW,    c, 4, "Item " + FORMAT$(c)
      NEXT
    
      '------------------------------------------------------------------
      DIALOG SHOW MODAL hDlg CALL DlgProc
    
    END FUNCTION
    
    '====================================================================
    CALLBACK FUNCTION DlgProc
      LOCAL c, hPopup AS LONG, sBuf AS STRING, rc AS RECT
      LOCAL hdrItem   AS HD_ITEM
      LOCAL pHdn      AS HD_NOTIFY PTR
      LOCAL LpLvNm    AS NM_LISTVIEW PTR
      STATIC hHeader  AS DWORD, stCol AS LONG
    
      SELECT CASE CBMSG
      CASE %WM_INITDIALOG  'Add a spilt button to each header column and tell it we want fixed width
          CONTROL SEND CB.HNDL, %IDC_LISTVIEW, %LVM_GETHEADER, 0, 0 TO hHeader ' static header handle
          FOR c = 0 TO Header_GetItemCount(hHeader) - 1
              hdrItem.mask = %HDI_FORMAT
              Header_GetItem hHeader, c, BYVAL VARPTR(hdrItem)
              hdrItem.fmt = hdrItem.fmt OR %HDF_SPLITBUTTON OR %HDF_FIXEDWIDTH
              Header_SetItem hHeader, c, BYVAL VARPTR(hdrItem)
          NEXT
    
      CASE %WM_COMMAND
         SELECT CASE CBCTL
         CASE %IDM_POPUP1 : ? "Choice 1 for column" + STR$(stCol) + " selected"
         CASE %IDM_POPUP2 : ? "Choice 2 for column" + STR$(stCol) + " selected"
         END SELECT
    
      CASE %WM_NOTIFY
          pHdn = CB.LPARAM
          IF @pHdn.hdr.hWndFrom = hHeader THEN ' @pHdn.iItem is 0-based column
              SELECT CASE @pHdn.hdr.code
              CASE %HDN_ITEMCLICK : sBuf    = " Message: %HDN_ITEMCLICK"    + $CRLF + " Column:" + STR$(@pHdn.iItem+1) ' clicked header button
              CASE %HDN_ITEMDBLCLICK : sBuf = " Message: %HDN_ITEMDBLCLICK" + $CRLF + " Column:" + STR$(@pHdn.iItem+1) ' clicked header button
              CASE %HDN_DROPDOWN  : sBuf    = " Message: %HDN_DROPDOWN"     + $CRLF + " Column:" + STR$(@pHdn.iItem+1) ' clicked split button
                  stCol = @pHdn.iItem+1  ' use static stCol to track selected column after menu item was selected
                  CONTROL SET TEXT CB.HNDL, %IDC_LABEL, sBuf : sBuf = " "
                  MENU NEW POPUP TO hPopup
                  MENU ADD STRING, hPopup, "Choice 1 for column" + STR$(stCol), %IDM_POPUP1, %MF_ENABLED  ' trapped under %WM_COMMAND
                  MENU ADD STRING, hPopup, "Choice 2 for column" + STR$(stCol), %IDM_POPUP2, %MF_ENABLED  ' trapped under %WM_COMMAND
                  Header_GetItemRect(hHeader, @pHdn.iItem, BYVAL VARPTR(rc))
                  MapWindowPoints(hHeader, 0, BYVAL VARPTR(rc), 2)
                  TrackPopupMenu hPopup, %TPM_LEFTALIGN, rc.nLeft, rc.nBottom, 0, CB.HNDL, BYVAL 0
              END SELECT
              IF LEN(sBuf) THEN CONTROL SET TEXT CB.HNDL, %IDC_LABEL, sBuf ' show what notification we got
    
          ELSEIF @pHdn.hdr.idFrom = %IDC_LISTVIEW THEN
              LpLvNm = CB.LPARAM
              SELECT CASE @LpLvNm.hdr.code ' iSubItem = 0-based column, iItem = 0-based line
              CASE %LVN_ITEMCHANGED : sBuf = " Message: %LVN_ITEMCHANGED" + $CRLF + " Column:" + STR$(@LpLvNm.iSubItem+1) + $CRLF + " Line:" + STR$(@LpLvNm.iItem+1)
              CASE %NM_CLICK        : sBuf = " Message: %NM_CLICK"        + $CRLF + " Column:" + STR$(@LpLvNm.iSubItem+1) + $CRLF + " Line:" + STR$(@LpLvNm.iItem+1)
              CASE %NM_DBLCLK       : sBuf = " Message: %NM_DBLCLK"       + $CRLF + " Column:" + STR$(@LpLvNm.iSubItem+1) + $CRLF + " Line:" + STR$(@LpLvNm.iItem+1)
              END SELECT
              IF LEN(sBuf) THEN CONTROL SET TEXT CB.HNDL, %IDC_LABEL, sBuf ' show what notification we got
          END IF
    
      END SELECT
    END FUNCTION
    Attached Files

  • #2
    Typical, just realized I forgot to include XPTheme.xml i zip file. The one I have used looks like this:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity
        name="Microsoft.Windows.Shell.shell32"
        processorArchitecture="x86"
        version="5.1.0.0"
        type="win32"/>
    <description>Windows Shell</description>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity
                type="win32"
                name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0"
                processorArchitecture="x86"
                publicKeyToken="6595b64144ccf1df"
                language="*"
            />
        </dependentAssembly>
    </dependency>
    </assembly>

    Comment

    Working...
    X