Exemple sans MFC
//-----------------------------------------------------------------------------
// FILELIST.C
// Lib: comctl32.lib ou libcomctl32.a
//-----------------------------------------------------------------------------
#define _WIN32_IE 0x0400
#include <windows.h>
#include <commctrl.h>
#define IDC_STATUS 101
#define IDC_LISTVIEW 102
#define FILEPATH "C:\\windows\\*.*" // Nom du dossier a modifier
HINSTANCE hInst;
HWND hStatus;
HWND hListview;
//-----------------------------------------------------------------------------
HWND CreateListView(HINSTANCE hInstance, HWND hParent)
{
LV_COLUMN lvColumn;
int i, iWidth[] = {200, 100, 100};
char *szTitre[] = {"Nom", "Taille", "Date"};
DWORD dwStyle = LVS_REPORT|LVS_SINGLESEL|WS_CHILD|WS_BORDER|WS_VISIBLE;
HWND hListview = CreateWindowEx(0,
WC_LISTVIEW,
NULL,
dwStyle,
0, 0, 0, 0,
hParent,
(HMENU)IDC_LISTVIEW,
hInstance,
NULL);
if(!hListview) return NULL;
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for(i = 0; i < 3; i++)
{
lvColumn.cx = iWidth[i];
lvColumn.pszText = szTitre[i];
lvColumn.fmt = (i == 1) ? LVCFMT_RIGHT : LVCFMT_LEFT;
ListView_InsertColumn(hListview, i, &lvColumn );
}
ListView_SetExtendedListViewStyleEx(hListview, 0, LVS_EX_GRIDLINES);
return hListview;
}
//-----------------------------------------------------------------------------
int ListViewAddItem(HWND hListview, char *szText, int Item, int SubItem)
{
LV_ITEM lvi;
if(SubItem)
{
lvi.mask = LVIF_TEXT;
lvi.iItem = Item;
lvi.iSubItem = SubItem;
lvi.pszText = szText;
lvi.cchTextMax = szText ? lstrlen(szText) : 0;
return ListView_SetItem(hListview, &lvi);
}
else
{
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
lvi.iItem = Item;
lvi.iSubItem = 0;
lvi.state = 0;
lvi.stateMask = 0;
lvi.iImage = 0;
lvi.pszText = szText;
lvi.cchTextMax = szText ? lstrlen(szText) : 0;
return ListView_InsertItem(hListview, &lvi);
}
}
//-----------------------------------------------------------------------------
void ListViewResize(HWND hListview, HWND hParent)
{
RECT rcsb, rc;
GetWindowRect(GetDlgItem(hParent,IDC_STATUS), &rcsb);
GetClientRect(hParent, &rc);
MoveWindow(hListview,
0, rc.top,
rc.right - rc.left,
(rc.bottom - (rcsb.bottom - rcsb.top)) - rc.top, TRUE);
}
//-----------------------------------------------------------------------------
void WINAPI FileList(char *filePath)
{
WIN32_FIND_DATA fData;
HANDLE hfind;
char szBuf[64];
INT64 fileSize;
DWORD numFiles = 0;
FILETIME ft;
SYSTEMTIME st;
hfind = FindFirstFile(filePath, &fData);
if(hfind != INVALID_HANDLE_VALUE)
{
ListView_DeleteAllItems(hListview);
do
{
if(!(fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{ //Nom de fichier
ListViewAddItem(hListview, fData.cFileName, numFiles, 0);
wsprintf(szBuf, " %s", fData.cFileName);
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)szBuf);
//Taille de fichier
fileSize = (INT64)(fData.nFileSizeHigh * MAXDWORD) + fData.nFileSizeLow;
wsprintf(szBuf, "%I64d", fileSize);
ListViewAddItem(hListview, szBuf, numFiles, 1);
//Date de modification
FileTimeToLocalFileTime(&(fData.ftLastWriteTime), &ft);
FileTimeToSystemTime(&ft, &st);
wsprintf(szBuf, "%02d/%02d/%4d %02d:%02d",
st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute);
ListViewAddItem(hListview, szBuf, numFiles, 2);
numFiles++;
}
}
while (FindNextFile(hfind, &fData));
FindClose(hfind);
}
wsprintf(szBuf, " Nombre de fichier(s): %d [%s]", numFiles, filePath);
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)szBuf);
}
//-----------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
hListview = CreateListView(hInst, hWnd);
hStatus = CreateStatusWindow(WS_CHILD|WS_VISIBLE, NULL, hWnd, IDC_STATUS);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)FileList, FILEPATH, 0, NULL);
return 0;
case WM_SIZE:
ListViewResize(hListview, hWnd);
SendMessage(hStatus, WM_SIZE, 0, 0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HWND hWnd;
WNDCLASSEX wcex;
INITCOMMONCONTROLSEX iccex;
char szClassName[] = "FileList";
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_LISTVIEW_CLASSES|ICC_BAR_CLASSES;
if(!InitCommonControlsEx(&iccex)) return 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW|CS_VREDRAW;;
wcex.lpfnWndProc = (WNDPROC)MainWndProc;
wcex.cbClsExtra = wcex.cbWndExtra = 0;
wcex.hInstance = hInst = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = szClassName;
wcex.hIcon = wcex.hIconSm = NULL;
wcex.lpszMenuName = NULL;
if(!RegisterClassEx(&wcex)) return 0;
hWnd = CreateWindowEx(0, szClassName, szClassName,
WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if(!hWnd) return 0;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}