Creating a List-View

This is a discussion on Creating a List-View within the Windows Programming forums, part of the Platform Specific Boards category; Code: // m_hWnd is the main window handle hListView = CreateWindow (WC_LISTVIEW,"List", WS_VISIBLE | WS_CHILD,0,0, 200,200,m_hWnd,NULL, (HINSTANCE) GetWindowLong(m_hWnd,GWL_HINSTANCE), NULL); SetWindowLong(hListView,GWL_STYLE,LVS_REPORT); ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    69

    Creating a List-View

    Code:
    // m_hWnd is the main window handle
    
     hListView = CreateWindow
                           (WC_LISTVIEW,"List", 
     	        WS_VISIBLE | WS_CHILD,0,0,
    	        200,200,m_hWnd,NULL,	
    	        (HINSTANCE)
                            GetWindowLong(m_hWnd,GWL_HINSTANCE),
                            NULL); 
    
     SetWindowLong(hListView,GWL_STYLE,LVS_REPORT);
    
     LV_COLUMN col;
     col.mask = LVCF_WIDTH;
     col.cx = 50;
     SendMessage(hListView,LVM_INSERTCOLUMN,0,(LPARAM)&col);
     
     // ... insert other columns
    I tried to create a list view which looks like the Outlook Express' message list

    but it seems it doesn't work,the listview isn't visible...what's the error?
    Last edited by Lionel; 04-16-2005 at 03:22 PM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Have you called InitCommonControls or InitCommonControlsEx?

    You can find a list-view sample here and more samples at FoosYerDoos.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    69

    Lightbulb

    Quote Originally Posted by anonytmouse
    Have you called InitCommonControls or InitCommonControlsEx?

    You can find a list-view sample here and more samples at FoosYerDoos.

    thank you very much for the examples,but the error was that SetWindowLong() removes the WS_CHILD and perhaps the WS_VISIBLE styles from the list view,and in fact i removed the setwndlong call and now it works!!!
    i say this for someone interested

    bye

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Sorry for bumping this thread.. I came to this while searching a ListView sample..
    The problem with your code was the way you are calling SetWindowLong.

    Since GWL_STYLE is single flag used to specify WS_CHILD as well as LVS_REPORT, You need to get the windowLong and add the flags you want and pass it back.

    SetWindowLong(hListView,GWL_STYLE,GetWindowLong(hL istView,GWL_STYLE)|LVS_REPORT);

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Okay, now coming back to my issue..

    I have a ListView created using
    Code:
    HWND hWndListView = CreateWindow(WC_LISTVIEW, 
                                    L"", 
                                    WS_CHILD |WS_VISIBLE| LVS_REPORT, 
                                    rcl.left, 
                                    rcl.top, 
                                    rcl.right - rcl.left, 
                                    rcl.bottom - rcl.top, 
                                    hwndParent,
                                    (HMENU)NULL,
                                    hinst, 
                                    NULL);
    After this I am adding few columns using
    Code:
    listColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
    listColumn.iSubItem=0;
    listColumn.pszText=wszColumnName;
    listColumn.cx=100;
    listColumn.fmt = LVCFMT_LEFT;
    ListView_InsertColumn(hwndListView, 0, &listColumn);
    And few Items using
    Code:
    LVITEM listItem;
    listItem.mask = LVIF_TEXT;
    listItem.pszText =const_cast<LPWSTR>(wszItemText);
    listItem.iItem = 0;
    listItem.iSubItem =0;
    ListView_InsertItem(m_hwndListView,&listItem);
    I would asume this would allow me to select items in the ListView. But currently I am not able to select any items. Even if set LVS_EX_FULLROWSELECT I am not able to select any items.

    What am I missing here?

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    I think I got answer to my question..Turns out in my custom window proc I was not handling LVN_ITEMACTIVATE (nor passing to DefWindowProc)

    Looking at LVN_ITEMACTIVATE Notification Code (Windows)
    "The application receiving this notification code must return zero. "

    I was thiking return value matters only if I handle the message.. Looks like even if I don't handle the message I should return proper values or pass the message to DefWindowProc

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mvadu View Post
    I think I got answer to my question..Turns out in my custom window proc I was not handling LVN_ITEMACTIVATE (nor passing to DefWindowProc)

    Looking at LVN_ITEMACTIVATE Notification Code (Windows)
    "The application receiving this notification code must return zero. "

    I was thiking return value matters only if I handle the message.. Looks like even if I don't handle the message I should return proper values or pass the message to DefWindowProc
    Any message you are not handling should not be listed in the WinProc's switch statments. Just let it fall through to the default: tag and pass it to defwindow proc. (which mostly just discards the messages for you).

    To catch item activation you need to handle WM_NOTIFY and within that LVN_ITEMACTIVATE...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21