Link to home
Start Free TrialLog in
Avatar of alexo
alexoFlag for Antarctica

asked on

virtual ListView in a dialog

I'm trying to write a small program consisting of a dialog with a virtual listview.
Problem is, the listview always seems to be empty.

I'm using MSVC 4.2 plus the latest comctl32 (headers, lib and DLL).

WinMain() inits the common controls and calls DialogBox().

The dialog template is taken from an .RC file and looks like:

    IDD_GROUPS DIALOG DISCARDABLE  0, 0, 259, 206
    STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION |
        WS_SYSMENU
    CAPTION "List"
    FONT 8, "MS Sans Serif"
    BEGIN
        CONTROL         "List1",IDC_LIST,"SysListView32",LVS_REPORT |
                        LVS_NOCOLUMNHEADER | LVS_NOSORTHEADER | WS_BORDER |
                        WS_TABSTOP | 0x1000,7,49,171,150
        DEFPUSHBUTTON   "Exit",IDCANCEL,186,185,66,14
    END

(I'm using the constant 0x1000 because the resource editor of MSVC 4.2 does not recognize the symbollic LVS_OWNERDATA).

The Dialog procedure looks like:

    BOOL CALLBACK DlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static HWND hListBox = NULL;

        switch (msg)
        {
            case WM_INITDIALOG:
                hListBox = GetDlgItem(hwndDlg, IDC_LIST);
                ListView_SetItemCount(10);
                break;

            case WM_NOTIFY:
                switch (reinterpret_cast<NMHDR*>(lParam)->code)
                {
                    case LVN_GETDISPINFO:
                        NMLVDISPINFO* pInfo = reinterpret_cast<NMLVDISPINFO*>(lParam);

                        if (pInfo->item.mask & LVIF_TEXT)
                            lstrcpyn(pInfo->item.pszText, "this is a line", pInfo->item.cchTextMax);

                        break;
                }
                break;

            case WM_COMMAND:
                switch (LOWORD(wParam))
                {
                    case IDCANCEL:
                        EndDialog(hwndDlg, wParam);
                        return TRUE;
                }
   
        } // end of message switch

        return FALSE;   // Default dialog procedure will do further processing
    }

If I put a breakpoint on the line with lstrcpyn(), I see the correct data being copied.  However, the listview seems empty :-(

What am I doing wrong?
Avatar of chensu
chensu
Flag of Canada image

If the list view control was created with the LVS_OWNERDATA style (a virtual list view), the ListView_SetItemCountEx macro should be used instead of ListView_SetItemCount.
Avatar of alexo

ASKER

I'll try it.
Avatar of alexo

ASKER

This is totally wrong.
The ListView_SetItemCount() and ListView_SetItemCountEx() macros are defined as:

    #if (_WIN32_IE >= 0x0300)
    // these flags only apply to LVS_OWNERDATA listviews in report or list mode
    #define LVSICF_NOINVALIDATEALL  0x00000001
    #define LVSICF_NOSCROLL         0x00000002
    #endif

    #define LVM_SETITEMCOUNT        (LVM_FIRST + 47)
    #define ListView_SetItemCount(hwndLV, cItems) \
      SNDMSG((hwndLV), LVM_SETITEMCOUNT, (WPARAM)cItems, 0)

    #if (_WIN32_IE >= 0x0300)
    #define ListView_SetItemCountEx(hwndLV, cItems, dwFlags) \
      SNDMSG((hwndLV), LVM_SETITEMCOUNT, (WPARAM)cItems, (LPARAM)dwFlags)
    #endif

The only difference is the dwFlags argument to the LVM_SETITEMCOUNT message.
Since I *want* to invalidate all items and change scroll position when the number of items changes, the dwFlags argument should be set to 0 and the ListView_SetItemCount() macro is perfectly adequate.
Just to be on the safe side, I tested ListView_SetItemCountEx().  No change.
Avatar of alexo

ASKER

The code in WM_INITDIALOG seems to be OK.  Planted some GetLastError() funcs and they all indicate success.
Also it doesn't make a difference if I return TRUE or FALSE in response to LVN_GETDISPINFO.
Avatar of alexo

ASKER

InitCommonControlsEx() with ICC_LISTVIEW_CLASSES succeeds.
ASKER CERTIFIED SOLUTION
Avatar of linda101698
linda101698

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alexo

ASKER

Linda, that is not the solution that worked.
I added a comment for that question saying what was the procedure.  However, it seems to be gone now.  What happened?  Was I censored?
If you can tell me what happened to my comment I'll give you an "A" :-)

For the record:
A listview in "report" mode needs to have at least one column.  The colums must be added explicitly because the listview defaults to 0 columns.
Avatar of linda101698
linda101698

Thanks for posting the correct solution :-)

I don't know what happened to your post.  I thought the message I copied was what HAD worked for you.  I'm glad you came in and posted the correct solution.  The other could have messed someone up.

Linda