Announcement

Collapse
No announcement yet.

Adding Images to ListView Column Headers

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

  • Adding Images to ListView Column Headers

    With the fantasic aid of POffs, and PBWinSpy I can usually muddle
    through and find a solution to a problem. However, how to add images
    to ListView column headers is evading me. I found a bit in Poffs
    by Jules Marchildon but I must be doing something really stupid which
    is not an abnormal thing for me but I would appreciate help.

    Thanks,

    ~Bob

    Code:
    ' Test Code to add an Image to a ListView column header
       
    #COMPILE EXE
    #DIM ALL
    '--------------------------------------------------------------------------------
    '   ** Includes **
    '-------------------------------------------------------------------------------- 
    ' $IncLean = "Great Stuff by Borje"
     
    #IF NOT %DEF($IncLean)
      #INCLUDE  "WIN32API.INC"
      #INCLUDE "COMMCTRL.INC"
    #ELSE
      #INCLUDE "LV2.inc"
    #ENDIF
    '--------------------------------------------------------------------------------
    '   ** Constants **
    '--------------------------------------------------------------------------------
    %IDD_DIALOG1  = -1
    %ListView     = 1001
    %AddButton    = 1002
    '--------------------------------------------------------------------------------
    '   ** Declarations **
    '--------------------------------------------------------------------------------
    DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
    DECLARE FUNCTION SampleListView(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL _
                                    lColCnt AS LONG, BYVAL lRowCnt AS LONG) AS LONG
    DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
    DECLARE SUB AddImageToListViewColumnHeader(hDlg AS DWORD)
    '--------------------------------------------------------------------------------
    GLOBAL hBmp1 AS DWORD, hListView AS DWORD
    '--------------------------------------------------------------------------------
    FUNCTION PBMAIN()
      '
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ' Initialize ListView control
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      LOCAL iccex AS INIT_COMMON_CONTROLSEX
      iccex.dwSize = SIZEOF(iccex)
      iccex.dwICC = %ICC_LISTVIEW_CLASSES
      CALL InitCommonControlsEx(iccex)    
      '
      ShowDIALOG1 %HWND_DESKTOP
      '
    END FUNCTION
    '--------------------------------------------------------------------------------
    '   ** CallBacks **
    '--------------------------------------------------------------------------------
    CALLBACK FUNCTION ShowDIALOG1Proc()
      '
      SELECT CASE CBMSG
        CASE %WM_INITDIALOG
          ' Load System Up Arrow Bitmap Image
          hBmp1 = LoadBitmap(0, BYVAL %OBM_UPARROWD)
        '  
        CASE %WM_COMMAND
          SELECT CASE CBCTL
            CASE %AddButton
              IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                AddImageToListViewColumnHeader CBHNDL 
              END IF        
          END SELECT   
        '  
        CASE %WM_DESTROY
          IF hBmp1 THEN DeleteObject hBmp1
      END SELECT
      '
    END FUNCTION
    '--------------------------------------------------------------------------------
    '   ** Sample Code **
    '--------------------------------------------------------------------------------
    FUNCTION SampleListView(BYVAL hDlg AS DWORD, BYVAL lID AS LONG, BYVAL lColCnt AS _
      LONG, BYVAL lRowCnt AS LONG) AS LONG
      LOCAL lStyle  AS LONG
      LOCAL tLVC    AS LV_COLUMN
      LOCAL tLVI    AS LV_ITEM
      LOCAL szBuf   AS ASCIIZ * 32
      LOCAL lCol    AS LONG
      LOCAL lRow    AS LONG
      '
      lStyle = ListView_GetExtendedListViewStyle(hListView)
      ListView_SetExtendedListViewStyle hListView, _ 
        lStyle OR %LVS_EX_FULLROWSELECT OR %LVS_EX_GRIDLINES
      '
      'Load column headers.
      tLVC.mask    = %LVCF_FMT OR %LVCF_TEXT OR %LVCF_SUBITEM
      tLVC.fmt     = %LVCFMT_LEFT
      tLVC.pszText = VARPTR(szBuf)
      FOR lCol = 0 TO lColCnt - 1
        szBuf = "Column" + FORMAT$(lCol)
        tLVC.iOrder = lCol
        ListView_InsertColumn hListView, lCol, tLVC
      NEXT lCol
      '
      'Load sample data.
      FOR lRow = 0 TO lRowCnt - 1
        tLVI.stateMask   = %LVIS_FOCUSED
        tLVI.pszText     = VARPTR(szBuf)
        tLVI.iItem       = lRow
        FOR lCol = 0 TO lColCnt - 1
          szBuf           = "Column" + FORMAT$(lCol) + " Row" + FORMAT$(lRow)
          tLVI.iSubItem   = lCol
          tLVI.lParam     = lRow
          IF lCol = 0 THEN
            tLVI.mask = %LVIF_TEXT OR %LVIF_PARAM OR %LVIF_STATE
            ListView_InsertItem hListView, tLVI
          ELSE
            tLVI.mask = %LVIF_TEXT
            ListView_SetItem hListView, tLVI
          END IF
        NEXT lCol
      NEXT i
      '
      'Auto size columns.
      FOR lCol = 0 TO lColCnt - 1
        ListView_SetColumnWidth hListView, lCol, %LVSCW_AUTOSIZE
      NEXT lCol
      '
    END FUNCTION
    '--------------------------------------------------------------------------------
    '   ** Dialogs **
    '--------------------------------------------------------------------------------
    FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
      LOCAL lRslt AS LONG
      LOCAL hDlg AS DWORD
      '
      DIALOG NEW hParent, "Test - Add Image to ListView Header", 69, 76, 272, 106, _
        %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS _
        OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR _
        %DS_SETFONT, %WS_EX_WINDOWEDGE OR %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
        %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
      CONTROL ADD "SysListView32", hDlg, %ListView, "SysListView321", 5, 5, 185, 95, _
        %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %WS_TABSTOP OR %LVS_REPORT OR _
        %LVS_SHOWSELALWAYS OR %LVS_EDITLABELS, %WS_EX_LEFT OR %WS_EX_RIGHTSCROLLBAR
      CONTROL ADD BUTTON, hDlg, %AddButton, "Add  Up Arrow", 205, 35, 60, 15
      '
      CONTROL HANDLE hDlg,%ListView TO hListView
      '
      SampleListView hDlg, %ListView, 3, 30
      '
      DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
      '
      FUNCTION = lRslt
    END FUNCTION
    '--------------------------------------------------------------------------------
    SUB AddImageToListViewColumnHeader(hDlg AS DWORD)
      '
      LOCAL hHeader AS LONG
      LOCAL hdi     AS HD_ITEM
      LOCAL szItem  AS ASCIIZ*32
      LOCAL hBmp    AS LONG
      LOCAL nCol    AS LONG
      '
      'get a handle to the listview header component
      hHeader = SendMessage(hListView, %LVM_GETHEADER, 0, 0)
      '
      'set up the required structure members
      szItem      = "Got it"
      hdi.mask    = %HDI_IMAGE OR %HDI_FORMAT
      hdi.pszText = VARPTR(szItem)
      hdi.fmt     = %HDF_STRING OR %HDF_IMAGE OR %HDF_BITMAP_ON_RIGHT
      hdi.iImage  = hBmp1 
      '
      nCol = 0 ' Test for 1st column only
      SendMessage hHeader, %HDM_SETITEM, nCol, VARPTR(hdi)
      '
    END SUB
    ------------------




    [This message has been edited by Bob Scott (edited May 14, 2003).]

  • #2
    Originally posted by Bob Scott:
    With the fantasic aid of POffs, and PBWinSpy ...
    Sorry Guys, I added a tribute to Borje's IncLean which I use all
    the time but just pasted it into the posted code without checking
    and sure enough I pasted it a few lines off. It's fixed now so if
    anyone would be so kind to give me some feedback I would appreciate
    it.

    Thanks,

    ~Bob




    ------------------

    Comment

    Working...
    X