Reputation: 6321
I want to add columns to a ListView control. You can see what I tried below using ListView_InsertColumn
, but I'm getting "identifier x is undefined". It should be in CommCtrl.h
, but I don't know what's wrong. I've seen some people using CListView_InsertColumn
instead. Not sure what the difference is.
#include <Windows.h>
#include "resource.h"
#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
#include <CommCtrl.h> // LV_COLUMN and ListView_x
INT_PTR CALLBACK DialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
case WM_INITDIALOG:
LVCOLUMNW col;
col.mask = LVCF_TEXT | LVCF_WIDTH | LVIF_IMAGE;
col.cx = 40;
ListView_InsertColumn(GetDlgItem(hWnd, IDC_LIST2), 0, &col);
return TRUE;
case WM_NCDESTROY:
PostQuitMessage(0);
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
DestroyWindow(hWnd);
break;
default:
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd
)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
HWND hWnd = CreateDialogParamW(hInstance, MAKEINTRESOURCEW(IDD_MAIN), nullptr, &DialogProc, 0);
if (!hWnd)
{
MessageBoxW(nullptr, L"Dialog Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_MAIN, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 302
TOPMARGIN, 7
BOTTOMMARGIN, 169
END
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_MAIN DIALOGEX 0, 0, 309, 176
STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "Test"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
LISTBOX IDC_LIST2,29,44,235,56,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
END
/////////////////////////////////////////////////////////////////////////////
//
// AFX_DIALOG_LAYOUT
//
IDD_MAIN AFX_DIALOG_LAYOUT
BEGIN
0
END
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
Upvotes: 1
Views: 523
Reputation: 51825
As mentioned in the comments, the control you define in your resource script is a ListBox, not a ListView. For the latter, you'll need to use the generic CONTROL
statement with a control class of "SysListView32"
(this can be inserted from the Visual Resource Editor as a "List Control" object, with its "View" property set to "Report"). Note: Without the LVS_REPORT
style, you can't add columns to your control.
Here's a suggested replacement for the dialog resource you have:
IDD_MAIN DIALOGEX 0, 0, 309, 176
STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "Test"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
//LISTBOX IDC_LIST2, 29, 44, 235, 56, LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
CONTROL "", IDC_LIST2, "SysListView32", LVS_REPORT | LVS_SORTASCENDING | WS_VSCROLL | WS_TABSTOP,
29, 44, 235, 56
END
With only that change made, your program will crash/hang during the call to ListView_InsertColumn
, because you have specified the LVCF_WIDTH
and LVCF_IMAGE
flags in the mask
member of the LVCOLUMN
structure but have not provided valid data for those flags. I can't just 'invent' an image for you but, with some 'dummy' text values (and an extra column, for good measure), here's a suggestion for some 'fixed' code:
switch (Message) {
case WM_INITDIALOG: {
wchar_t c0txt[] = L"abc-0";
LVCOLUMNW col;
col.mask = LVCF_TEXT | LVCF_WIDTH;// | LVIF_IMAGE;
col.cx = 40;
col.pszText = c0txt;
ListView_InsertColumn(GetDlgItem(hWnd, IDC_LIST2), 0, &col);
// Add another column...
wchar_t c1txt[] = L"xyz-1";
col.mask = LVCF_TEXT | LVCF_WIDTH;// | LVIF_IMAGE;
col.cx = 80;
col.pszText = c1txt;
ListView_InsertColumn(GetDlgItem(hWnd, IDC_LIST2), 1, &col);
return TRUE;
}
When building your code with just the changes outlined above, I get a list view control with two columns:
Upvotes: 2