Wie kann ich Dateinamen und Ordner in einem Listview anzeigen lassen?



  • Wie bekomme ich in meinem Listview alle Dateien und Ordner mit dem entsprechenden Icon und Spalteninformationen angezeigt?

    // ShowFiles.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "ShowFiles.h"
    
    #include <commctrl.h> 
    #pragma comment(lib, "comctl32.lib")
    
    #define MAX_LOADSTRING 100
    
    // Global Variables:
    HINSTANCE hInst;								// current instance
    TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
    
    // Forward declarations of functions included in this code module:
    ATOM				MyRegisterClass(HINSTANCE hInstance);
    BOOL				InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                         _In_opt_ HINSTANCE hPrevInstance,
                         _In_ LPTSTR    lpCmdLine,
                         _In_ int       nCmdShow)
    {
    	UNREFERENCED_PARAMETER(hPrevInstance);
    	UNREFERENCED_PARAMETER(lpCmdLine);
    
     	// TODO: Place code here.
    	MSG msg;
    	HACCEL hAccelTable;
    
    	// Initialize global strings
    	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    	LoadString(hInstance, IDC_SHOWFILES, szWindowClass, MAX_LOADSTRING);
    	MyRegisterClass(hInstance);
    
    	// Perform application initialization:
    	if (!InitInstance (hInstance, nCmdShow))
    	{
    		return FALSE;
    	}
    
    	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SHOWFILES));
    
    	// Main message loop:
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    
    	return (int) msg.wParam;
    }
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    
    	wcex.style			= CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc	= WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInstance;
    	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SHOWFILES));
    	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_SHOWFILES);
    	wcex.lpszClassName	= szWindowClass;
    	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    
    	return RegisterClassEx(&wcex);
    }
    
    //
    //   FUNCTION: InitInstance(HINSTANCE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
    
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    
    //
    //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND	- process the application menu
    //  WM_PAINT	- Paint the main window
    //  WM_DESTROY	- post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    
    	DWORD       dwStyle;
    	HWND        hwndListView;
    
    	LV_COLUMN   lvColumn;
    	int         i;
    	TCHAR       szString[5][20] = { L"File", L"Ext", L"Size", L"Created", L"Attributes" };
    
    	WIN32_FIND_DATA ffd;
    	LARGE_INTEGER filesize;
    	TCHAR szDir[MAX_PATH];
    	size_t length_of_arg;
    	HANDLE hFind = INVALID_HANDLE_VALUE;
    	DWORD dwError = 0;
    
    	switch (message)
    	{
    
    	case WM_CREATE:
    		INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    		icex.dwICC = ICC_LISTVIEW_CLASSES;
    		InitCommonControlsEx(&icex);
    
    		dwStyle = WS_TABSTOP |
    			WS_CHILD |
    			WS_VISIBLE |
    			LVS_AUTOARRANGE |
    			LVS_REPORT |
    			LVS_OWNERDATA;
    
    		hwndListView = CreateWindowEx(WS_EX_CLIENTEDGE,          // ex style
    			WC_LISTVIEW,               // class name - defined in commctrl.h
    			L"",                        // dummy text
    			dwStyle,                   // style
    			10,                         // x position
    			18,                         // y position
    			1024,                         // width
    			600,                         // height
    			hWnd,                // parent
    			(HMENU)668,				// ID
    			hInst,                   // instance
    			NULL);                     // no extra data
    
    		// ----------- add columns
    
    		//empty the list
    		ListView_DeleteAllItems(hwndListView);
    
    		//initialize the columns
    		lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    		lvColumn.fmt = LVCFMT_LEFT;
    		lvColumn.cx = 180;
    		for (i = 0; i < 5; i++)
    		{
    			lvColumn.pszText = szString[i];
    			ListView_InsertColumn(hwndListView, i, &lvColumn);
    		}
    
    		// Set Gridlines
    		ListView_SetExtendedListViewStyle(hwndListView, LVS_EX_GRIDLINES);
    
    		// test 1 failes
    
    		LVITEM item;
    		item.mask = LVIF_TEXT;
    		item.cchTextMax = 6;
    
    		item.iSubItem = 1;
    		item.pszText = L"12345";
    		item.iItem = 0;
    		ListView_InsertItem(hwndListView, &item);
    
    		item.iSubItem = 2; // zero based index of column
    		item.pszText = L"23456";
    		ListView_InsertItem(hwndListView, &item);
    
    		item.iSubItem = 3; // zero based index of column
    		item.pszText = L"34567";
    		ListView_InsertItem(hwndListView, &item);
    
    		// test 2 failes
    
    		ListView_SetItemText(hwndListView, 0, 1, L"12345");
    		ListView_SetItemText(hwndListView, 0, 2, L"23456");
    		ListView_SetItemText(hwndListView, 0, 3, L"34567");
    
    		// list and show files...
    		do
    		{
    			if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    			{
    				_tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
    			}
    			else
    			{
    				filesize.LowPart = ffd.nFileSizeLow;
    				filesize.HighPart = ffd.nFileSizeHigh;
    				_tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
    			}
    		} while (FindNextFile(hFind, &ffd) != 0);
    
    		break;
    
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		// Parse the menu selections:
    		switch (wmId)
    		{
    		case IDM_\1:
    			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    			break;
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		// TODO: Add any drawing code here...
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    // Message handler for about box.
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	UNREFERENCED_PARAMETER(lParam);
    	switch (message)
    	{
    	case WM_INITDIALOG:
    		return (INT_PTR)TRUE;
    
    	case WM_COMMAND:
    		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    		{
    			EndDialog(hDlg, LOWORD(wParam));
    			return (INT_PTR)TRUE;
    		}
    		break;
    	}
    	return (INT_PTR)FALSE;
    }
    


  • Schlag dich doch nicht mit sowas rum 😃
    http://www.qt.io/



  • Vielleicht hilft das hier weiter.

    http://www.codeproject.com/Articles/2890/Using-ListView-control-under-Win-API

    [url]http://www.codeproject.com/Articles/7891/Using-virtual-lists
    [/url]

    Ausserdem ist ffd nicht initialisiert, was am fehlenden FindFirst liegt.

    Anmerkung MSDN:

    https://msdn.microsoft.com/en-us/library/windows/desktop/bb761107.aspx

    The LVM_INSERTITEM message will insert the new item in the proper position in the sort order if the following conditions hold:
    You are using one of the LVS_SORTXXX styles.
    You are not using the LVS_OWNERDRAW style.

    Oder mit anderen Worten:

    LVS_OWNERDATA erfordert besondere Aktionen ...





  • So ungefähr könnte das aussehen ...

    static vector<basic_string<TCHAR>> g_MainList;
    
    switch (message)
    {
    
    case WM_CREATE:
    
       ...
    
       // Fill in items
       g_MainList.push_back(_T("erster"));
       g_MainList.push_back(_T("zweiter"));
       g_MainList.push_back(_T("dritter"));
       g_MainList.push_back(_T("vierter"));
       g_MainList.push_back(_T("fünfter"));
    
       // Sets the virtual number of items in list view. 
       ListView_SetItemCount(hwndListView, g_MainList.size()); 
    
       break;
    
      case WM_NOTIFY: {
          NMHDR* pnmh = (NMHDR*) lParam;   
    
          switch(pnmh->code)  
          {   
          case LVN_GETDISPINFO:   
                                    {
            LV_DISPINFO* pdi = (LV_DISPINFO*)lParam;   
    
            if(0 == pdi->item.iSubItem) {   
               if(pdi->item.mask & LVIF_TEXT)   
                  swprintf(pdi->item.pszText, g_MainList[pdi->item.iItem].c_str());   
               if(pdi->item.mask & LVIF_IMAGE)   
                  pdi->item.iImage = 0;   
               }   
            }
          }
      }
      break;
    

Log in to reply