Announcement

Collapse
No announcement yet.

ListView Control

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

  • ListView Control

    I'm needing some help with a ListView.

    I'm using SDK programming and all my Dialogs and Menus are
    stored in a resource file created with VS6.0

    Code:
    - I'm lost on how to add contents to the ListView
    - How do I have the Listview show an icon that is present in
      the Resource File?
    Any helps would be appreciated.

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    From the WinAPI help file:

    The ListView_InsertItem macro inserts a new item in a list view control. You can use this macro or explicitly send the LVM_INSERTITEM message.

    int ListView_InsertItem(

    HWND hwnd,
    const LV_ITEM FAR *pitem
    );


    Parameters

    hwnd

    Handle to the list view control.

    pitem

    Pointer to an LV_ITEM structure that specifies the attributes of the list view item. The iItem member specifies the index of the new item.
    You cannot use ListView_InsertItem or LVM_INSERTITEM to insert subitems; the iSubItem member of the LV_ITEM structure must be zero.



    Return Values

    Returns the index of the new item if successful or -1 otherwise.
    Your best bet is to download the WinAPI help file from the support section of this site and examine the section on List View Messages carefully. It might take a while to puzzle out (at least, it did for me!) but the end result is worth it!

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


    [This message has been edited by Frank Kelley (edited April 21, 2001).]

    Comment


    • #3
      I'll toss in some misc code I have for ya:
      This was not done with DDT however, the old fashioned way worked best at the time...
      Code:
      'In Dialog procedure, great resizing tool:
      
          Case %WM_SIZE
              Local Xsize           As Long
              Local Ysize           As Long
              GetClientRect g_hWndMain, WndRect
              Xsize = WndRect.nRight - WndRect.nLeft
              Ysize = WndRect.nBottom - WndRect.nTop
              If IsWindowVisible(g_hStatus) Then
                  SendMessage g_hStatus, %WM_SIZE, wParam, lParam
                  ShowWindow g_hStatus, %SW_NORMAL
                  If IsWindowVisible(g_hListView) Then SetWindowPos g_hListView, 0, 0, 0, XSize, YSize-20, %SWP_NOMOVE Or %SWP_NOZORDER Or %SWP_DRAWFRAME
              Else
                  If IsWindowVisible(g_hListView) Then SetWindowPos g_hListView, 0, 0, 0, XSize, YSize, %SWP_NOMOVE Or %SWP_NOZORDER Or %SWP_DRAWFRAME
              End If
              Function = 0
              Exit Function
      
      
      '
      '
      For clearing all items:
                      SendMessage g_hListView, %LVM_DELETEALLITEMS,0,0
      '
      '
      '
      '
      '
      For adding items:
              g_sRec(0) = Ltrim$(Str$(lLoop)) 'Count of how many Records
              g_sRec(1) = Parse$(LVArray(lLoop) , 1) 'Friday
              g_sRec(2) = Parse$(LVArray(lLoop),2) + ", " + Parse$(LVArray(lLoop),3) '1999
              g_sRec(3) = Ltrim$(Remove$(Parse$(LVArray(lLoop) , 4),"At ")) 'Time     'This and next line are used for print/clipboard
              g_sRec(4) = Ltrim$(Remove$(Parse$(LVArray(lLoop) , 5),"by ")) 'User Name
              g_sRec(4) = Trim$(Parse$(g_sRec(4)," on ",1))
              g_sRec(5) = Trim$(Remove$(Parse$(LVArray(lLoop),6)," on "))
              g_sRec(5) = Parse$(LVArray(lLoop)," on ",2)
      
              AppendListView g_hListView 'Append to the ListView
      
      '
      '
      '
      '
      And the rest of the gang, I have not used most of these, mainly the append function:
      
      Function CreateListView (ByVal hWnd As Long, ByVal ID_LIST As Integer) As Long
          Dim listb   As Long
          Dim rect    As rect
          Dim hInst   As Word
          Local Y     As Long
      
          GetClientRect hWnd, rect
          If g_lStatusBarFlag = 1 Then Y = rect.nBottom - 20 Else Y = rect.nBottom
      
          listb = CreateWindow ( "SysListView32","", %WS_border Or %WS_Child Or %WS_visible Or _
                             %LVS_REPORT Or %LVS_SINGLESEL Or %LVS_SHOWSELALWAYS,0, 0, _
                             rect.nRight - 1, Y, hWnd, ID_LIST, hInst, %Null )
          Function = listb
      
      End Function
      
      '------------------------------------------------------------------------------
      
      Sub SetListView (hlist As Dword, dList As ListData)
          Dim lvc     As LV_COLUMN
          Dim iStatus As Long
          Dim szStr   As Asciiz * 32
          Local dummy As Long
      
          lvc.mask = %LVCF_FMT Or %LVCF_Width Or %LVCF_Text Or %LVCF_SUBITEM
          lvc.fmt = %LVCFMT_LEFT
          lvc.PszText = VarPtr(szStr)
          szStr = dList.Fields(0).fName
          lvc.cx = dList.Fields(0).Size
          lvc.iSubItem = 0
          ''' Test
      '    iStatus = listView_InsertColumn (hList, ByVal dummy, ByVal lvc)
          iStatus = SendMessage(hList, %LVM_INSERTCOLUMN, 0, VarPtr(lvc))
          SetCol hList, dList
      End Sub
      
      '------------------------------------------------------------------------------
      
      Sub SetCol(hlist As Dword, dList As ListData)
          Dim lvc     As LV_COLUMN
          Dim iStatus As Integer
          Dim szStr   As Asciiz * 32
          Dim lLoop   As Long
      
          lvc.mask = %LVCF_FMT Or %LVCF_Width Or %LVCF_Text Or %LVCF_SUBITEM
          lvc.fmt = %LVCFMT_LEFT
          lvc.PszText = VarPtr(szStr)
      
          For lLoop = 1 To dList.Columns
              szStr = dList.Fields(lLoop).fName
              lvc.cx = dList.Fields(lLoop).Size
              If lvc.cx > 200 Then lvc.cx = 200
              lvc.iSubItem = lLoop
              lvc.fmt = dList.Fields(lLoop).Alignment
              iStatus = ListView_InsertColumn (hList, lLoop, ByVal VarPtr(lvc))
      
          Next
      
      End Sub
      
      '------------------------------------------------------------------------------
      
      Sub AppendListView (hlist As Dword)
          Local z       As Integer
          Local iStatus As Integer
          Local szStr   As Asciiz * 300
          Local lvi     As LV_ITEM
          'LOCAL x     AS LONG
      
          'this will be the next record
      
          lvi.iItem = ListView_GetItemCount(hList) '+ 1
          lvi.mask = %LVIF_TEXT
          lvi.stateMask = %LVIS_FOCUSED '%LVIS_SELECTED
          lvi.pszText = VarPtr(szStr)
      
      
          For z = 0 To UBound(g_sRec)
              szStr = g_sRec(z)
              lvi.iSubItem = z
              lvi.lParam = lvi.iItem
              If z = 0 Then
                  lvi.mask = %LVIF_TEXT Or %LVIF_PARAM Or %LVIF_STATE
                  iStatus = ListView_InsertItem (hList, lvi)
              Else
                  lvi.mask = %LVIF_TEXT
                  iStatus = ListView_SetItem (hList, lvi)
              End If
          Next
      End Sub
      
      '------------------------------------------------------------------------------
      
      Sub Find(hlist As Dword, Value As String)
          Dim lvf As LV_FINDINFO
          Dim lvi As LV_ITEM
          Dim szStr As Asciiz * 300
          Dim iStatus As Integer
      
          lvf.flags = %LVFI_STRING Or %LVFI_Partial
          szStr = Value
      
          lvf.psz = VarPtr(szStr)
          lvi.iItem = ListView_FindItem(hList, -1, lvf)
          If lvi.iItem > 0 Then
              istatus = ListView_EnsureVisible(hList, lvi.iItem, %true)
          End If
      End Sub
      
      '------------------------------------------------------------------------------
      
      Function RetrieveLVData(hlist As Dword, Recno As Long, Fieldno As Long) As String
          Local lvi       As LV_ITEM
          Local value     As Asciiz * 300
          Local lReturn   As Long
      
          %cchTextMax = 300
      
          lvi.cchTextMax = %cchTextMax
          lvi.iItem = Recno
          lvi.pszText = VarPtr(value)
          lvi.iSubItem = Fieldno
          lvi.Mask = %LVIF_TEXT
          lReturn = ListView_GetItem (hList, lvi)
          Function = value
      End Function
      
      '------------------------------------------------------------------------------
      
      Function HitColumn (hlist As Dword, xdim As Long) As Long
          Local lCounter  As Long
          Local lWidth    As Long
          Local lSumWidth As Long
      
          'sumx = 0
          'i = 0
          Do
              lWidth = ListView_GetColumnWidth(hList, lCounter)
              lSumWidth = lSumWidth + lWidth
              If xdim < lSumWidth Then
                  Function = lCounter
                  Exit Do
              End If
              Incr lCounter
          Loop
      
      End Function
      
      '------------------------------------------------------------------------------
      
      Sub ResetLParam(hlist As Dword)
          Local lLoop     As Long
          Local lRecs      As Long
          Local lvi       As LV_ITEM
          Local lReturn   As Long
      
          lvi.mask = %LVIF_PARAM
          lvi.iSubItem = 0
          lRecs = ListView_GetItemCount(hList)
          For lLoop = 0 To lRecs - 1
              lvi.iItem = lLoop
              lReturn = ListView_GetItem (hList, lvi)
              lvi.lParam = lvi.iItem
              lReturn = ListView_SetItem (hList, lvi)
          Next
      End Sub
      
      '------------------------------------------------------------------------------------------
      ------------------
      Scott
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment


      • #4
        Scott,

        Thats for the code I actually figured it out myself.

        I do have a question about the Common Controls. If I'm designing
        my dialogs with a resource file do I still need to initialize
        the common controls before I use them?

        Thanks

        ------------------
        -Greg
        -Greg
        [email protected]
        MCP,MCSA,MCSE,MCSD

        Comment


        • #5
          If you are asking if you still have to call InitCommonControls/Ex() before loading the dialogs or using the controls, then, the answer is YES - the Common Controls DLL still needs to get loaded and initialized before it is used.

          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #6
            Ok that answers my question. Thanks

            ------------------
            -Greg
            -Greg
            [email protected]
            MCP,MCSA,MCSE,MCSD

            Comment

            Working...
            X