Code:
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "comctl32.lib")
#include <iostream>
using namespace std;
#define IDB_ADD 1001
#define IDB_REMOVE 1002
#define IDB_GETINFO 1003
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInstance;
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode)
{
HWND hWnd;
MSG Message;
WNDCLASSEX WinClass = { 0 };
INITCOMMONCONTROLSEX icc = { 0 };
hInstance = hThisInst;
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icc);
WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.hInstance = hThisInst;
WinClass.lpszClassName = TEXT("ListViewSample");
WinClass.lpfnWndProc = WindowFunc;
WinClass.style = 0;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.lpszMenuName = NULL;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
if(!RegisterClassEx(&WinClass)) return 0;
hWnd = CreateWindow(TEXT("ListViewSample"),
TEXT("List View Sample"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
400,
NULL,
NULL,
hThisInst,
NULL);
ShowWindow(hWnd, WinMode);
UpdateWindow(hWnd);
while(GetMessage(&Message,NULL,0,0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
static HWND hListView;
LVCOLUMN lvc = { 0 };
LVITEM lvi = { 0 };
char Text[1024];
int i=0;
switch(Message)
{
case WM_CREATE:
{
hListView = CreateWindow(WC_LISTVIEW,
NULL,
WS_CHILD | WS_VISIBLE | LVS_REPORT| LVS_SINGLESEL,
0,
0,
500,
300,
hWnd,
(HMENU) 500,
hInstance,
NULL);
ListView_SetExtendedListViewStyle(hListView, LVS_EX_CHECKBOXES |
LVS_EX_FULLROWSELECT |
LVS_EX_HEADERDRAGDROP |
LVS_EX_GRIDLINES);
lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_FMT;
lvc.fmt = LVCFMT_LEFT;
/* Add four columns to the list-view (first column contains check box). */
lvc.iSubItem = 0;
lvc.cx = 50;
lvc.pszText = TEXT("Record");
ListView_InsertColumn(hListView, 0, &lvc);
lvc.iSubItem = 1;
lvc.cx = 100;
lvc.pszText = TEXT("Name");
ListView_InsertColumn(hListView, 1, &lvc);
lvc.iSubItem = 2;
lvc.cx = 70;
lvc.pszText = TEXT("Episodes");
ListView_InsertColumn(hListView, 2, &lvc);
lvc.iSubItem = 3;
lvc.cx = 150;
lvc.pszText = TEXT("Any Good?");
ListView_InsertColumn(hListView, 3, &lvc);
/* Add some rows. iItem=rows */
lvi.iItem = 0;
ListView_InsertItem(hListView, &lvi);
ListView_SetItemText(hListView, 0, 1, TEXT("Friends"));
ListView_SetItemText(hListView, 0, 2, TEXT("500"));
ListView_SetItemText(hListView, 0, 3, TEXT("Alright"));
ListView_SetCheckState(hListView, 0, TRUE);
lvi.iItem = 1;
ListView_InsertItem(hListView, &lvi);
ListView_SetItemText(hListView, 1, 1, TEXT("Survivor"));
ListView_SetItemText(hListView, 1, 2, TEXT("970"));
ListView_SetItemText(hListView, 1, 3, TEXT("Please, not again"));
ListView_SetCheckState(hListView, 1, FALSE);
CreateWindow("button", "Add",
WS_VISIBLE | WS_CHILD,
20, 330, 80, 20,
hWnd, (HMENU) IDB_ADD, NULL, NULL);
CreateWindow("button", "Remove",
WS_VISIBLE | WS_CHILD,
120, 330, 80, 20,
hWnd, (HMENU) IDB_REMOVE, NULL, NULL);
CreateWindow("button", "Info",
WS_VISIBLE | WS_CHILD,
240, 330, 80, 20,
hWnd, (HMENU) IDB_GETINFO, NULL, NULL);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,Message,wParam,lParam);
}
return 0;
}