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;
}
|