Please Help me.......

Hi all,
In my win32 application,I want to createa window with 3 column & N rows.By using Listview control (Report style)How can i create this table.Please give me some sample code
Last edited on
Ill assume you want to put this in your main window.
Here is an example: (This is your WndProc for your window)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#define LISTVIEW 101        // Listview resource handle

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_CREATE: {
            hwnd hListView;        // Listview window handle
            LVCOLUMN LvCol;        // List column data structure
            char temp[256];        // Temporary data to store the title in.

            // Create the ListView control
            hListView = CreateWindowEx(WS_EX_CLIENTEDGE,
                                       WS_VISIBLE | WS_CHILD | LVS_REPORT,
                                       0, 0, 100, 100,         // These don't really matter
                                       hwnd,
                                       (HMENU)LISTVIEW,  // The resource value for the handle
                                       GetModuleHandle(NULL),
                                       NULL);
            if (hListView == NULL)        // Couldn't create listview for some reason.
                MessageBoxA(hwnd, "Could not create listview.", "Error", MB_OK | MB_ICONERROR);

            ZeroMemory(&LvCol, sizeof(LVCOLUMN));    // Clear the memory
            LvCol.mask = LCVF_TEXT;            // Only using text for the titles
            LvCol.cx = 0x66;                // Width of the columns

            // Get the column name from a string table
            LoadString(GetModuleHandle(NULL), IDS_COLUMN1, temp, 256);
            LvCol.pszText = temp;

            // Insert the column
            SendMessage(hListView, LVM_INSERTCOLUMN, 0, (LPARAM)&LvCol);

            // Add 2 more columns
            for (int i = 1; i < 3; i++) {
                LoadString(GetModuleHandle(NULL), IDS_COLUMN1 + i, temp, 256);
                LvCol.pszText = temp;
                SendMessage(hListView, LVM_INSERTCOLUMN, i, (LPARAM)&LvCol);
            }
        } break;

        case WM_SIZE: {            // When the window is resized
            HWND hListView;        // Handle to the listview
            RECT rcClient;            // Size of the client window
            int nListHeight;        // Stores the height of the listview.

            GetClientRect(hwnd, &rcClient);        // Get the height of the window
            nListHeight = rcClient.bottom - rcClient.top;    // Fit the ListHeight inbetween.

            hListView = GetDlgItem(hwnd, LISTVIEW);        // Get the handle for the listview
            // Set the listview size to fill the screen completely.
            SetWindowPos(hListView, NULL, 0, 0, rcClient.right, nListHeight, SWP_NOZORDER);
        } break;

        // (... Other things go here ...)

        default:        // Let windows process unhandled messages
            return DefWindowProc(hwnd, msg,wParam, lParam);
    }

    // Let windows know we handled the message ourselves.
    return 0;
}
I tryed this one.But,not Displaying any box.
@NT3

Your CreateWindowEx call is missing the class name and window name??

Anyway, the code below runs.
- You have to call InitCommonControlsEx before you try and create a List View
- And you have to get your flags write for LVM_INSERTCOLUMN
- etc

I am also using the List View macros rather than the messages, e.g. ListView_InsertColumn rather than SendMessage with LVM_INSERTCOLUMN (as shown here:
Using List-View Controls
http://msdn.microsoft.com/en-us/library/windows/desktop/bb774736%28v=vs.85%29.aspx )

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>

#pragma comment(lib, "comctl32.lib") // auto-link with required lib
// (if not using Visual C++, will prob have to add this lib to link options)

#define IDC_LISTVIEW  101        // Listview resource handle

const char g_szClassName[] = "List_View_Example_Window_Class";

HINSTANCE g_hInst = NULL;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int CALLBACK
WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{
    g_hInst = hInstance; // remember instance handle

    INITCOMMONCONTROLSEX icex = {0};   // initialize the common control library
    icex.dwICC = ICC_LISTVIEW_CLASSES; // listview class
    InitCommonControlsEx(&icex);

    WNDCLASSEX wc = {0};
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassExA(&wc))
    {
        MessageBoxA(NULL, "RegisterClassExA failed", "Error",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    HWND hwnd = CreateWindowExA(
        WS_EX_APPWINDOW,
        g_szClassName,
        "List View Example App",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);

    if(hwnd == NULL)
    {
        MessageBoxA(NULL, "CreateWindowExA failed", "Error",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    MSG Msg = {0};
    while(GetMessageA(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessageA(&Msg);
    }

    return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    static HWND s_hListView = NULL;

    switch (uMsg) {
        case WM_CREATE: {
            // Create the List View control
            s_hListView = CreateWindowExA(WS_EX_CLIENTEDGE,
                                          WC_LISTVIEW,          // from commctrl.h
                                          NULL,                 // no window text
                                          WS_VISIBLE | WS_CHILD | LVS_REPORT,
                                          0, 0, 0, 0,           // Size is set in WM_SIZE
                                          hwnd,
                                          (HMENU)IDC_LISTVIEW,  // The control ID for the list view
                                          g_hInst,
                                          NULL);
            if (s_hListView == NULL)        // Couldn't create List View for some reason.
                MessageBoxA(hwnd, "Could not create listview.", "Error", MB_OK | MB_ICONERROR);

            char buffer[256] = ""; // buffer to store strings in.

            // Add 3 columns

            LVCOLUMNA LvCol = {0};
            LvCol.mask    = LVCF_TEXT | LVCF_WIDTH;
            LvCol.pszText = buffer;

            for (int i = 0; i < 3; i++) {
                wsprintfA(buffer, "Column %d", i);
                LvCol.cx = 75 * (i + 1);
                ListView_InsertColumn(s_hListView, i, &LvCol);
            }

            // Add some items and subitems

            LVITEMA lvItem = {0};
            lvItem.mask    = LVIF_TEXT;
            lvItem.pszText = buffer;

            for (int j = 0; j < 10; j++) {
                for (int i = 0; i < 3; i++) {
                    if(0 == i) {
                        wsprintfA(buffer, "Row %d", j);
                        lvItem.iItem = j;
                        ListView_InsertItem(s_hListView, &lvItem);
                    } else {
                        wsprintfA(buffer, "Row %d, Subitem %d", j, i);
                        ListView_SetItemText(s_hListView, j, i, buffer);
                    }
                }
            }
        } break;

        case WM_SIZE: {            // When the window is resized
            if((wParam == SIZE_MAXIMIZED) || (wParam == SIZE_RESTORED)) {
                int width  = LOWORD(lParam);
                int height = HIWORD(lParam);
                SetWindowPos(s_hListView, NULL, 0, 0, width, height, SWP_NOZORDER);
            }
        } break;

        case WM_DESTROY: {
            PostQuitMessage(0);
        } break;

        // (... Other things go here ...)

        default:        // Let windows process unhandled messages
            return DefWindowProcA(hwnd, uMsg,wParam, lParam);
    }

    // Let windows know we handled the message ourselves.
    return 0;
}
Last edited on
Topic archived. No new replies allowed.