0

I am trying to create a listview in C++ with the win32 api, however the code supplied on mdsn is giving me an error.

HWND CreateListView (HWND hwndParent) 
{
    INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);

    RECT rcClient;                       // The parent window's client area.

    GetClientRect (hwndParent, &rcClient); 

    // Create the list-view window in report view with label editing enabled.
    HWND hWndListView = CreateWindow(WC_LISTVIEW, //ERROR red line under create window
                                     L"",
                                     WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
                                     0, 0,
                                     rcClient.right - rcClient.left,
                                     rcClient.bottom - rcClient.top,
                                     hwndParent,
                                     (HMENU)IDM_CODE_SAMPLES, //ERROR IDM CODE SAMPLES undefined
                                     g_hInst, //ERROR
                                     NULL); 

    return (hWndListView);
}

This example is strait from mdsn and I have no idea why it is not working. I am getting IDM_CODE_SAMPLES Undefined, and something wrong with createwindow. Please help me to get this working it would be really helpful.

IInspectable
  • 46,945
  • 8
  • 85
  • 181

3 Answers3

2

IDM_CODE_SAMPLES is the ID you want to assign to your control. You can either define the symbol to a numeric value, or use the numeric value directly (choose 100, for example). The ID is useful if you want to reference a particular control, although its HWND is equally useful as an ID.

g_hInst is presumably a global variable of type HMODULE, initialized from WinMain. If you don't want to use the global variable, you can call GetModuleHandle(nullptr) in its place, provided that you are compiling an .exe as opposed to a .dll.

You'll get a lot of helpful information when working through Intro to Win32 programming in C++.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • Thankyou for your reply, I am now getting an error (1 unresolved externals) when I try to compile. I'm just trying to get a project working with a listview to see how it works. – smartypantz May 02 '20 at 11:11
  • It's very difficult in visual studio 2019, I have made projects work in older ides however I can't seem to get it to work in here. – smartypantz May 02 '20 at 11:12
  • [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/q/12573816/1889329) – IInspectable May 02 '20 at 11:48
1

I am now getting an error (1 unresolved externals)

We can find from InitCommonControlsEx function.

Ensures that the common control DLL (Comctl32.dll) is loaded, and registers specific common control classes from the DLL.

Add:

#include <commctrl.h>    
#pragma comment(lib,"Comctl32.lib")

Minimal code example:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>
#include <commctrl.h>

#pragma comment(lib,"Comctl32.lib")

#define IDM_CODE_SAMPLES 101

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND CreateListView(HWND hwndParent);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = { };

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );
    CreateListView(hwnd);
    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        EndPaint(hwnd, &ps);
    }
    return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

HWND CreateListView(HWND hwndParent)
{
    INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);

    RECT rcClient;                       // The parent window's client area.

    GetClientRect(hwndParent, &rcClient);

    // Create the list-view window in report view with label editing enabled.    
    HWND hWndListView = CreateWindow(WC_LISTVIEW, //ERROR red line under create window
        L"",
        WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
        0, 0,
        rcClient.right - rcClient.left,
        rcClient.bottom - rcClient.top,
        hwndParent,
        (HMENU)IDM_CODE_SAMPLES, //ERROR IDM CODE SAMPLES undefined
        GetModuleHandle(nullptr), //ERROR
        NULL);

    return (hWndListView);
}
Strive Sun
  • 5,988
  • 1
  • 9
  • 26
0

Just in case someone else is having issues around SysListView32:

#include <Ole2.h>
OleInitialize(NULL);
#include <commctrl.h>
#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "comctl32.lib")

INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwICC = ICC_LISTVIEW_CLASSES;
InitCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
BOOL bRet = InitCommonControlsEx(&InitCtrls);
...

For reference: fully working example with bells and whistles: https://github.com/jjYBdx4IL/Win32-List-View

user1050755
  • 11,218
  • 4
  • 45
  • 56