Win32 API ListView Creation (C++)
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:
-
use the generic
WC_LISTVIEW
so it matches the genericCreateWindowEx
(), and get rid of the type-cast:list = CreateWindowEx(..., WC_LISTVIEW, ...);
-
keep using
WC_LISTVIEWW
, but callCreateWindowExW()
instead:list = CreateWindowExW(..., WC_LISTVIEWW, ...);
das_j
Updated on June 04, 2022Comments
-
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 over 11 yearshow do I change the style to windows 7 design?
-
Remy Lebeau over 11 yearsWhat 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. 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 over 7 yearsNote 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 replacesprintf
withsprintf_s
andstrcmpi
with_strcmpi
.