Thread: ~~multi-column list~~

  1. #1
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149

    ~~multi-column list~~

    i need to list some information of books in my program.
    because each information of book has several parts, (like isbn,title,press ....) i need to set the listbox to multi-column.
    but i don't how to do this
    can anyone help me? it would be great if someone can show me a sample to let me konw how to create and control a multi-column list.

    blow me ... ...

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Compile and run this, see if it answers your question.
    Code:
    //
    // Multi Column ListBox Tutorial
    // c. adrianxw
    //
    
    #include <windows.h>
    #include <stdio.h>
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hInstance;
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                       LPSTR Args, int WinMode)
    {
        HWND hWnd;
        MSG Message;
        WNDCLASSEX WinClass;
    
        hInstance = hThisInst;
        
        WinClass.cbSize = sizeof(WNDCLASSEX);   
        WinClass.hInstance = hThisInst;
        WinClass.lpszClassName = "Window";
        WinClass.lpfnWndProc = WindowFunc; 
        WinClass.style = 0;     
        WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
        WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        WinClass.lpszMenuName = NULL; 
        WinClass.cbClsExtra = 0; 
        WinClass.cbWndExtra = 0;
        WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH); 
        
        if(!RegisterClassEx(&WinClass)) return 0;
        
        hWnd = CreateWindow("Window", 
                            "Window",
                            WS_OVERLAPPEDWINDOW, 
                            CW_USEDEFAULT,
                            CW_USEDEFAULT, 
                            CW_USEDEFAULT, 
                            CW_USEDEFAULT,
                            HWND_DESKTOP,
                            NULL,
                            hThisInst,
                            NULL);
        
        ShowWindow(hWnd, 
                   WinMode);
        UpdateWindow(hWnd);
    
        while(GetMessage(&Message,
                         NULL,
                         0,
                         0))
        {
            TranslateMessage(&Message); 
            DispatchMessage(&Message);
        }
        return Message.wParam;
    }
    
    LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
        HWND hListBox;
        int i;
        char Output[12];
    
        switch(Message)
        {
        case WM_CREATE:
            hListBox = CreateWindow("LISTBOX",
                                    NULL,
                                    WS_CHILD | WS_VISIBLE | LBS_MULTICOLUMN,
                                    10,
                                    10,
                                    500,
                                    500,
                                    hWnd,
                                    (HMENU) 500,
                                    hInstance,
                                    NULL);
    
            SendMessage(hListBox,
                        LB_SETCOLUMNWIDTH,
                        (WPARAM) 100,
                        (LPARAM) 0);
    
            for (i=0; i<100; i++)
            {
                sprintf(Output, "String %d", i);
                SendMessage(hListBox,
                            LB_ADDSTRING,
                            (WPARAM) 0,
                            (LPARAM) Output);
            }
                        
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd,
                                 Message,
                                 wParam,
                                 lParam);
        }
        return 0;
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    thanks a lot,adrianxw.



    but there are still some problem:
    SendMessage(hListBox,
    LB_ADDSTRING,
    (WPARAM) 0,
    (LPARAM) Output);
    this code add all strings in one column, i wonder how can i add five strings in fine columns one row?
    and how to add column title???

    blow me ... ...

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The list-view control can be customised more but is also more complex. You can check out the FoosYerDoos common controls site or you can try this modified version of the scratch program.
    Code:
    #define _WIN32_IE 0x300
    #include <windows.h>
    #include <commctrl.h>
    #include <stdio.h>
    #pragma comment(lib, "gdi32.lib")
    #pragma comment(lib, "comctl32.lib")
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hInstance;
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                       LPSTR Args, int WinMode)
    {
        HWND hWnd;
        MSG Message;
        WNDCLASSEX WinClass      = { 0 };
        INITCOMMONCONTROLSEX icc = { 0 };
    
        hInstance = hThisInst;
    
        icc.dwSize = sizeof(icc);
        icc.dwICC = ICC_LISTVIEW_CLASSES;
        InitCommonControlsEx(&icc);
        
        WinClass.cbSize        = sizeof(WNDCLASSEX);   
        WinClass.hInstance     = hThisInst;
        WinClass.lpszClassName = TEXT("ListViewSample");
        WinClass.lpfnWndProc   = WindowFunc; 
        WinClass.style         = 0;     
        WinClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hIconSm       = LoadIcon(NULL, IDI_WINLOGO);
        WinClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WinClass.lpszMenuName  = NULL; 
        WinClass.cbClsExtra    = 0; 
        WinClass.cbWndExtra    = 0;
        WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH); 
        
        if(!RegisterClassEx(&WinClass)) return 0;
        
        hWnd = CreateWindow(TEXT("ListViewSample"), 
                            TEXT("List View Sample"),
                            WS_OVERLAPPEDWINDOW, 
                            CW_USEDEFAULT,
                            CW_USEDEFAULT, 
                            CW_USEDEFAULT, 
                            CW_USEDEFAULT,
                            NULL,
                            NULL,
                            hThisInst,
                            NULL);
        
        ShowWindow(hWnd, WinMode);
        UpdateWindow(hWnd);
    
        while(GetMessage(&Message,
                         NULL,
                         0,
                         0))
        {
            TranslateMessage(&Message); 
            DispatchMessage(&Message);
        }
        return Message.wParam;
    }
    
    LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
        HWND hListView;
    
        switch(Message)
        {
        case WM_CREATE:
        {
            LVCOLUMN lvc = { 0 };
            LVITEM   lv  = { 0 };
    
            hListView = CreateWindow(WC_LISTVIEW,
                                     NULL,
                                     WS_CHILD | WS_VISIBLE | LVS_REPORT,
                                     10,
                                     10,
                                     500,
                                     500,
                                     hWnd,
                                     (HMENU) 500,
                                     hInstance,
                                     NULL);
    
    	ListView_SetExtendedListViewStyle(hListView, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
    
            lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH  | LVCF_FMT;
            lvc.fmt  = LVCFMT_LEFT;
    
            /* Add four columns to the list-view (first column contains check box). */
            lvc.iSubItem = 0;
            lvc.cx       = 50;
            lvc.pszText  = TEXT("Record");
            ListView_InsertColumn(hListView, 0, &lvc);
    
            lvc.iSubItem = 1;
            lvc.cx       = 100;
            lvc.pszText  = TEXT("Name");
            ListView_InsertColumn(hListView, 1, &lvc);
    
            lvc.iSubItem = 2;
            lvc.cx       = 70;
            lvc.pszText  = TEXT("Episodes");
            ListView_InsertColumn(hListView, 2, &lvc);
    
            lvc.iSubItem = 3;
            lvc.cx       = 150;
            lvc.pszText  = TEXT("Any Good?");
            ListView_InsertColumn(hListView, 3, &lvc);
    
            /* Add some rows. */
            lv.iItem = 0;
            ListView_InsertItem(hListView, &lv);
            ListView_SetItemText(hListView, 0, 1, TEXT("Friends"));
            ListView_SetItemText(hListView, 0, 2, TEXT("500"));
            ListView_SetItemText(hListView, 0, 3, TEXT("Alright"));
            ListView_SetCheckState(hListView, 0, TRUE);
    
            lv.iItem = 1;
            ListView_InsertItem(hListView, &lv);
            ListView_SetItemText(hListView, 1, 1, TEXT("Survivor"));
            ListView_SetItemText(hListView, 1, 2, TEXT("970,000"));
            ListView_SetItemText(hListView, 1, 3, TEXT("Please, not again"));
            ListView_SetCheckState(hListView, 1, FALSE);
                        
            break;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd,
                                 Message,
                                 wParam,
                                 lParam);
        }
        return 0;
    }
    Last edited by anonytmouse; 12-18-2004 at 06:35 AM.

  5. #5
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    thanks , i will try this

    blow me ... ...

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I like to use the LVS_EX_GRIDLINES style as well
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    I know this is an old post, but hey it beats starting a new one.

    I am just wondering what _WIN32_IE is in the posted code? :

    Code:
    #define _WIN32_IE 0x300
    thanks in advance!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So does reading the forum rules
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. BorlandC proj
    By mary18 in forum C Programming
    Replies: 68
    Last Post: 02-20-2008, 11:22 AM
  3. Retrieving Multiple inputs with fgets?
    By Axel in forum C Programming
    Replies: 25
    Last Post: 09-13-2005, 04:04 PM
  4. Bitwise OR
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2005, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM