Here is a C++ program using the WinAPI that creates a graphical window application to display a graphical representation of the file system, including all disks. The application uses a TreeView control to display the disks and their directories.
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#include <string>
#pragma comment(lib, "comctl32.lib")
HINSTANCE hInst;
HWND hTreeView;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void InitTreeView(HWND hWnd);
void AddDriveItems(HWND hTreeView);
void AddDirectoryItems(HTREEITEM hParent, LPCTSTR lpPath);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Initialize global instance
hInst = hInstance;
// Initialize common controls
INITCOMMONCONTROLSEX icex; // INITCOMMONCONTROLSEX structure
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_TREEVIEW_CLASSES;
InitCommonControlsEx(&icex);
// Register window class
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = _T("FileSystemViewerClass");
RegisterClass(&wc);
// Create window
HWND hWnd = CreateWindow(
wc.lpszClassName,
_T("File System Viewer"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
nullptr,
nullptr,
hInstance,
nullptr
);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
// Window Procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
// Create TreeView control
InitTreeView(hWnd);
// Populate TreeView with drives and directories
AddDriveItems(hTreeView);
}
break;
case WM_SIZE:
{
// Resize TreeView control
if (hTreeView)
{
SetWindowPos(hTreeView, nullptr, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_NOZORDER);
}
}
break;
case WM_DESTROY:
{
// Clean up allocated memory
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Initialize TreeView Control
void InitTreeView(HWND hWnd)
{
RECT rcClient;
GetClientRect(hWnd, &rcClient);
hTreeView = CreateWindowEx(0,
WC_TREEVIEW,
_T("Tree View"),
WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT,
0,
0,
rcClient.right,
rcClient.bottom,
hWnd,
nullptr,
hInst,
nullptr);
}
// Add drive items to TreeView
void AddDriveItems(HWND hTreeView)
{
DWORD dwDrives = GetLogicalDrives();
for (int i = 0; i < 26; ++i)
{
if (dwDrives & (1 << i))
{
// Get drive letter
TCHAR szDriveRoot[] = { TCHAR('A' + i), _T(':'), _T('\\'), _T('\0') };
TVITEM tvi = {};
tvi.mask = TVIF_TEXT | TVIF_PARAM;
tvi.pszText = szDriveRoot;
tvi.lParam = (LPARAM)_tcsdup(szDriveRoot); // Duplicate string for later use
TVINSERTSTRUCT tvins = {};
tvins.item = tvi;
tvins.hInsertAfter = TVI_ROOT;
tvins.hParent = TVI_ROOT;
// Insert drive as root item in TreeView
HTREEITEM hDriveItem = TreeView_InsertItem(hTreeView, &tvins);
// Add directories under each drive
AddDirectoryItems(hDriveItem, szDriveRoot);
}
}
}
// Add directory items to TreeView recursively
void AddDirectoryItems(HTREEITEM hParent, LPCTSTR lpPath)
{
TCHAR szSearchPath[MAX_PATH];
_stprintf(szSearchPath, _T("%s*"), lpPath);
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(szSearchPath, &findFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
// Skip "." and ".." entries
if (_tcscmp(findFileData.cFileName, _T(".")) != 0 &&
_tcscmp(findFileData.cFileName, _T("..")) != 0)
{
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// It's a directory
TCHAR szFullPath[MAX_PATH];
_stprintf(szFullPath, _T("%s%s\\"), lpPath, findFileData.cFileName);
TVITEM tvi = {};
tvi.mask = TVIF_TEXT | TVIF_PARAM;
tvi.pszText = findFileData.cFileName;
tvi.lParam = (LPARAM)_tcsdup(szFullPath); // Duplicate string for later use
TVINSERTSTRUCT tvins = {};
tvins.item = tvi;
tvins.hInsertAfter = TVI_SORT;
tvins.hParent = hParent;
// Insert directory into TreeView
HTREEITEM hDirItem = TreeView_InsertItem(hTreeView, &tvins);
// Recursively add subdirectories
AddDirectoryItems(hDirItem, szFullPath);
}
else
{
// Optional: Add files to the TreeView (uncomment the following code if needed)
/*
TVITEM tvi = {};
tvi.mask = TVIF_TEXT;
tvi.pszText = findFileData.cFileName;
TVINSERTSTRUCT tvins = {};
tvins.item = tvi;
tvins.hInsertAfter = TVI_SORT;
tvins.hParent = hParent;
TreeView_InsertItem(hTreeView, &tvins);
*/
}
}
} while (FindNextFile(hFind, &findFileData));
FindClose(hFind);
}
}
Explanation:
Notes:
-
Memory Management:
- The code uses
_tcsdup to duplicate strings for the lParam of TreeView items. In a long-running application, you should free this memory when it's no longer needed to prevent memory leaks.
-
Including Files:
- If you wish to include files in the TreeView, uncomment the section marked in the
AddDirectoryItems function.
-
Unicode Support:
- Make sure your project is set to use Unicode character set. If you need to support ANSI, adjust the string functions accordingly.
-
Error Handling:
- For brevity, error handling is minimal. In production code, you should add error checks after API calls to handle failures appropriately.
How to Compile:
-
Using Visual Studio:
- Create a new Win32 Project.
- Replace the generated code with the code above.
- Make sure to include
comctl32.lib in your project settings (should be linked automatically with the #pragma comment(lib, "comctl32.lib") line).
- Build and run the project.
-
Using Command Line with Visual Studio Tools:
Screenshot of the Application:
When you run the application, it should display a window similar to the following, showing the drives and directories on your system:
[image: File System Viewer Screenshot] open (Replace with actual screenshot if needed)
Conclusion:
This application provides a graphical representation of the file system using standard WinAPI functions and controls. It demonstrates how to enumerate drives and directories and display them in a hierarchical TreeView control.