Win32 API ListView Creation (C++)

12,313

Solution 1

Here is the link to original MSDN sample code of ListView control written in Windows API and C. It compiles in VC++ 2010.

Solution 2

WC_LISTVIEWW (notice the extra W on the end) is a wchar_t*, but you are type-casting it to a char*. That will only compile if UNICODE is not defined, making the generic CreateWindowEx() map to CreateWindowExA(). Which means you are trying to create a Unicode window with the Ansi version of CreateWindowEx(). That will not work.

You need to either:

  1. use the generic WC_LISTVIEW so it matches the generic CreateWindowEx(), and get rid of the type-cast:

    list = CreateWindowEx(..., WC_LISTVIEW, ...);
    
  2. keep using WC_LISTVIEWW, but call CreateWindowExW() instead:

    list = CreateWindowExW(..., WC_LISTVIEWW, ...);
    
Share:
12,313
das_j
Author by

das_j

Updated on June 04, 2022

Comments

  • das_j
    das_j about 2 years

    I want to create a ListView in c++. My code so far:

    InitCommonControls(); // Force the common controls DLL to be loaded.
    HWND list;
    
    // window is a handle to my window that is already created.
    list = CreateWindowEx(0, (LPCSTR) WC_LISTVIEWW, NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_SHOWSELALWAYS | LVS_REPORT, 0, 0, 250, 400, window, NULL, NULL, NULL);
    
    LVCOLUMN lvc; 
    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    lvc.iSubItem = 0;
    lvc.pszText = "Title";
    lvc.cx = 50;
    lvc.fmt = LVCFMT_LEFT;
    ListView_InsertColumn(list, 0, &lvc);
    

    But if I compile and execute the code, just a blank window is beeing showed. Compiler: MinGW on Windows 7 (x86).

    Can anybody help me showing the listview properly?

  • das_j
    das_j over 11 years
    how do I change the style to windows 7 design?
  • Remy Lebeau
    Remy Lebeau over 11 years
    What is "Windows 7 design" exactly? Are you referring to visual themes? You ned to provide a ComCtrl32 v6 manifest to enable that. Nothing in the code changes, unless you want to take advantage of new features introduced in ComCtrl32 v6.
  • Hanan N.
    Hanan N. about 11 years
    @das_j You can add the following compiler directive to enable that: #pragma comment(linker,"\"/manifestdependency:type='win32' \ name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
  • jrh
    jrh over 7 years
    Note that in Visual Studio 2013+ you will need to add #define _CRT_SECURE_NO_WARNINGS and #define _CRT_NONSTDC_NO_DEPRECATE to the top of LISTVIEW.C to run this code as-is. Alternatively you could replace sprintf with sprintf_s and strcmpi with _strcmpi.