// day005.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "day005.h" #include<stdio.h> #include"resource.h" #include <CommCtrl.h> #include <windowsx.h> #pragma comment(lib, "Comctl32.lib") #define MAX_LOADSTRING 100 // 全局变量: HINSTANCE hInst; // 当前实例 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 HWND hWnd; // 此代码模块中包含的函数的前向声明: //ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); INT_PTR CALLBACK WndProc( _In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam ); int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 MSG msg; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_DAY005, szWindowClass, MAX_LOADSTRING); //MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } // 主消息循环: while (GetMessage(&msg, NULL, 0, 0)) { if (!IsDialogMessage(hWnd, &msg)){ TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // //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_DAY005)); // wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // wcex.lpszMenuName = MAKEINTRESOURCE(IDC_DAY005); // wcex.lpszClassName = szWindowClass; // wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); // // return RegisterClassEx(&wcex); //} // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL,WndProc); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // INT_PTR CALLBACK WndProc( _In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam ) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (uMsg) { case WM_INITDIALOG:{ HWND hList = GetDlgItem(hWnd, IDC_LIST1); RECT rt = {}; GetClientRect(hList, &rt); LVCOLUMN lc; lc.mask = LVCF_WIDTH | LVCF_TEXT; lc.pszText = TEXT("盘符"); /*lc.cx = (rt.right - rt.left) / 6; lc.cchTextMax = 3;*/ lc.cx = 90; ListView_InsertColumn(hList, 0, &lc); lc.pszText = TEXT("标题"); lc.cx = 120; ListView_InsertColumn(hList, 1, &lc); lc.pszText = TEXT("文件系统"); lc.cx = 100; ListView_InsertColumn(hList, 2, &lc); lc.pszText = TEXT("总空间"); lc.cx = 150; ListView_InsertColumn(hList, 3, &lc); lc.pszText = TEXT("可用空间"); lc.cx = 120; ListView_InsertColumn(hList, 4, &lc); lc.pszText = TEXT("描述"); lc.cx = 250; ListView_InsertColumn(hList, 5, &lc); //插入多行 TCHAR buf[100] = {}; //获取所有的盘符 GetLogicalDriveStrings(100, buf); TCHAR* pDirve = buf; UINT nType = 0; DWORD 每簇的扇区数量 = 0; DWORD 每个扇区的容量 = 0; DWORD 空闲簇的总量 = 0; DWORD 所有簇的数量 = 0; int memoryCount = 0; while (*pDirve != 0){ TCHAR bufTemp[10] = {}; TCHAR bufTempDeadLine[100] = {}; TCHAR bufTempFileSystem[100] = {}; TCHAR bufTempTatalSpace[30] = {}; TCHAR bufTempAvailableSpace[30] = {}; TCHAR bufTempScrible[50] = {}; // _stprintf_s(bufTemp, TEXT("%s"), buf); GetVolumeInformation(TEXT("C"), bufTempDeadLine, 20, NULL, NULL,NULL,NULL,NULL); LVITEM li = {}; li.iItem = memoryCount; ListView_InsertItem(hList, &li); ListView_InsertItem(hList, &li); ListView_InsertItem(hList, &li); ListView_SetItemText(hList, memoryCount, 0,pDirve); GetVolumeInformation(pDirve, bufTempDeadLine, 100, 0, 0, 0, bufTempFileSystem, 100); ListView_SetItemText(hList, memoryCount, 1, bufTempDeadLine); ListView_SetItemText(hList, memoryCount, 2, bufTempFileSystem); GetDiskFreeSpace(pDirve, &每簇的扇区数量, &每个扇区的容量, &空闲簇的总量, &所有簇的数量); _stprintf_s(bufTempTatalSpace, 30, _T("%.2f %s"), 所有簇的数量*((每簇的扇区数量*每个扇区的容量 / 1024.0) / 1024.0 / 1024.0), TEXT("GB")); ListView_SetItemText(hList, memoryCount, 3, bufTempTatalSpace); //可用空间 _stprintf_s(bufTempAvailableSpace, 30, _T("%.2f %s"), 空闲簇的总量*((每簇的扇区数量*每个扇区的容量 / 1024.0) / 1024.0 / 1024.0), TEXT( "GB")); ListView_SetItemText(hList, memoryCount, 4, bufTempAvailableSpace); //描述 nType = GetDriveType(pDirve); switch (nType) { case DRIVE_UNKNOWN: ListView_SetItemText(hList, memoryCount, 5, TEXT("无法识别的设备")); break; case DRIVE_NO_ROOT_DIR: ListView_SetItemText(hList, memoryCount, 5, TEXT("逻辑磁盘不存在")); break; case DRIVE_REMOVABLE: ListView_SetItemText(hList, memoryCount, 5, TEXT("可移除的驱动器")); break; case DRIVE_FIXED: ListView_SetItemText(hList, memoryCount, 5, TEXT("不可移除的驱动器")); break; case DRIVE_REMOTE: ListView_SetItemText(hList, memoryCount, 5, TEXT("不可移设备")); break; case DRIVE_CDROM: ListView_SetItemText(hList, memoryCount, 5, TEXT("CD_ROM驱动器")); break; case DRIVE_RAMDISK: ListView_SetItemText(hList, memoryCount, 5, TEXT("虚拟盘")); break; default: ListView_SetItemText(hList, memoryCount, 5, TEXT("其它类型")); break; } //GetDiskFreeSpace(pDirve, // &每簇的扇区数量, // &每个扇区的容量, // &空闲簇的总量, // &所有簇的数量); //printf("总容量:%.2f,空闲量:%.2f", // 所有簇的数量*((每簇的扇区数量*每个扇区的容量 / 1024.0) / 1024.0 / 1024.0), // 空闲簇的总量*((每簇的扇区数量*每个扇区的容量 / 1024.0) / 1024.0 / 1024.0)); pDirve += _tcsclen(pDirve); pDirve++; memoryCount++; } /*ListView_SetItemText(hList, 0, 1, TEXT("系统")); ListView_SetItemText(hList, 0, 2, TEXT("NTFS")); ListView_SetItemText(hList, 0, 3, TEXT("70G"));*/ }break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: 在此添加任意绘图代码... EndPaint(hWnd, &ps); break; case WM_CLOSE: DestroyWindow(hWnd); PostQuitMessage(0); break; default: break; } return 0; }