Add files via upload

This commit is contained in:
zsyg
2025-07-02 11:11:16 +08:00
committed by GitHub
parent 1cd722bf89
commit d5f944280e
23 changed files with 766 additions and 640 deletions

View File

@@ -1,8 +1,8 @@
#ifndef ABOUT_DIALOG_H #ifndef ABOUT_DIALOG_H
#define ABOUT_DIALOG_H #define ABOUT_DIALOG_H
#include <windows.h> #include <windows.h>
void show_about_dialog(HWND hParent); void show_about_dialog(HWND hParent);
#endif // ABOUT_DIALOG_H #endif // ABOUT_DIALOG_H

View File

@@ -1,10 +1,10 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
#define APP_NAME "系统信息查看器" #define APP_NAME "系统信息查看器"
#define APP_VERSION "1.0" #define APP_VERSION "1.0"
#define MAX_DISKS 26 #define MAX_DISKS 26
#define MAX_ADAPTERS 10 #define MAX_ADAPTERS 10
#define UPDATE_INTERVAL 5000 // 5秒 #define UPDATE_INTERVAL 5000 // 5秒
#endif // CONFIG_H #endif // CONFIG_H

View File

@@ -1,15 +1,15 @@
#ifndef DISK_INFO_H #ifndef DISK_INFO_H
#define DISK_INFO_H #define DISK_INFO_H
#include <windows.h> #include <windows.h>
typedef struct { typedef struct {
char driveLetter; char driveLetter;
DWORD64 totalBytes; DWORD64 totalBytes;
DWORD64 freeBytes; DWORD64 freeBytes;
char fileSystem[32]; char fileSystem[32];
} DiskInfo; } DiskInfo;
void get_disk_info(DiskInfo* disks, int* count); void get_disk_info(DiskInfo* disks, int* count);
#endif // DISK_INFO_H #endif // DISK_INFO_H

View File

@@ -1,15 +1,15 @@
#ifndef LOGGING_H #ifndef LOGGING_H
#define LOGGING_H #define LOGGING_H
#include <windows.h> #include <windows.h>
typedef enum { typedef enum {
LOG_DEBUG, LOG_DEBUG,
LOG_INFO, LOG_INFO,
LOG_WARNING, LOG_WARNING,
LOG_ERROR LOG_ERROR
} LogLevel; } LogLevel;
void log_message(LogLevel level, const char* format, ...); void log_message(LogLevel level, const char* format, ...);
#endif // LOGGING_H #endif // LOGGING_H

View File

@@ -5,5 +5,7 @@
#include "system_info.h" #include "system_info.h"
void update_main_window(HWND hWnd, SystemInfo* sysInfo); void update_main_window(HWND hWnd, SystemInfo* sysInfo);
void toggle_fullscreen(HWND hWnd);
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
#endif // MAIN_WINDOW_H #endif // MAIN_WINDOW_H

View File

@@ -1,23 +1,23 @@
#ifndef NETWORK_INFO_H #ifndef NETWORK_INFO_H
#define NETWORK_INFO_H #define NETWORK_INFO_H
#include <windows.h> #include <windows.h>
#include <iphlpapi.h> #include <iphlpapi.h>
// 确保GetAdaptersInfo函数声明 // 确保GetAdaptersInfo函数声明
#ifndef _IPHLPAPI_ #ifndef _IPHLPAPI_
#define _IPHLPAPI_ #define _IPHLPAPI_
DWORD GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen); DWORD GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen);
#endif #endif
typedef struct { typedef struct {
char adapterName[MAX_ADAPTER_NAME_LENGTH + 4]; char adapterName[MAX_ADAPTER_NAME_LENGTH + 4];
char description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; char description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
char ipAddress[16]; char ipAddress[16];
char macAddress[18]; char macAddress[18];
ULONG speed; // in Mbps ULONG speed; // in Mbps
} NetworkAdapterInfo; } NetworkAdapterInfo;
void get_network_adapters(NetworkAdapterInfo* adapters, int* count); void get_network_adapters(NetworkAdapterInfo* adapters, int* count);
#endif // NETWORK_INFO_H #endif // NETWORK_INFO_H

View File

@@ -1,26 +1,26 @@
#ifndef PERFORMANCE_INFO_H #ifndef PERFORMANCE_INFO_H
#define PERFORMANCE_INFO_H #define PERFORMANCE_INFO_H
#include <windows.h> #include <windows.h>
#include <pdh.h> #include <pdh.h>
// 确保PDH函数声明 // 确保PDH函数声明
#ifndef _PDH_H_ #ifndef _PDH_H_
#define _PDH_H_ #define _PDH_H_
PDH_STATUS PdhOpenQueryA(LPCSTR szDataSource, DWORD_PTR dwUserData, PDH_HQUERY* phQuery); PDH_STATUS PdhOpenQueryA(LPCSTR szDataSource, DWORD_PTR dwUserData, PDH_HQUERY* phQuery);
PDH_STATUS PdhAddCounterA(PDH_HQUERY hQuery, LPCSTR szFullCounterPath, DWORD_PTR dwUserData, PDH_HCOUNTER* phCounter); PDH_STATUS PdhAddCounterA(PDH_HQUERY hQuery, LPCSTR szFullCounterPath, DWORD_PTR dwUserData, PDH_HCOUNTER* phCounter);
PDH_STATUS PdhCollectQueryData(PDH_HQUERY hQuery); PDH_STATUS PdhCollectQueryData(PDH_HQUERY hQuery);
PDH_STATUS PdhGetFormattedCounterValue(PDH_HCOUNTER hCounter, DWORD dwFormat, LPDWORD lpdwType, PPDH_FMT_COUNTERVALUE pValue); PDH_STATUS PdhGetFormattedCounterValue(PDH_HCOUNTER hCounter, DWORD dwFormat, LPDWORD lpdwType, PPDH_FMT_COUNTERVALUE pValue);
#endif #endif
typedef struct { typedef struct {
DWORD cpuUsage; // CPU使用率百分比 DWORD cpuUsage; // CPU使用率百分比
DWORD memoryUsage; // 内存使用率百分比 DWORD memoryUsage; // 内存使用率百分比
DWORD processesCount; // 进程数量 DWORD processesCount; // 进程数量
DWORD threadsCount; // 线程数量 DWORD threadsCount; // 线程数量
DWORD handlesCount; // 句柄数量 DWORD handlesCount; // 句柄数量
} PerformanceInfo; } PerformanceInfo;
void get_performance_info(PerformanceInfo* perfInfo); void get_performance_info(PerformanceInfo* perfInfo);
#endif // PERFORMANCE_INFO_H #endif // PERFORMANCE_INFO_H

View File

@@ -1,11 +1,11 @@
#ifndef RESOURCE_H #ifndef RESOURCE_H
#define RESOURCE_H #define RESOURCE_H
#define IDI_MAIN_ICON 101 #define IDI_MAIN_ICON 101
#define IDR_MAIN_MENU 102 #define IDR_MAIN_MENU 102
#define IDM_EXIT 1001 #define IDM_EXIT 1001
#define IDM_ABOUT 1002 #define IDM_ABOUT 1002
#define IDM_SHOW_INFO 1003 #define IDM_SHOW_INFO 1003
#endif // RESOURCE_H #endif // RESOURCE_H

View File

@@ -1,21 +1,21 @@
#ifndef SYSTEM_INFO_H #ifndef SYSTEM_INFO_H
#define SYSTEM_INFO_H #define SYSTEM_INFO_H
#include <windows.h> #include <windows.h>
typedef struct { typedef struct {
char cpuName[256]; char cpuName[256];
DWORD cpuCores; DWORD cpuCores;
DWORD cpuThreads; DWORD cpuThreads;
MEMORYSTATUSEX memoryStatus; MEMORYSTATUSEX memoryStatus;
SYSTEM_INFO systemInfo; SYSTEM_INFO systemInfo;
OSVERSIONINFOEX osVersion; OSVERSIONINFOEX osVersion;
} SystemInfo; } SystemInfo;
// 初始化系统信息 // 初始化系统信息
void init_system_info(SystemInfo* sysInfo); void init_system_info(SystemInfo* sysInfo);
// 创建主窗口 // 创建主窗口
int create_main_window(HINSTANCE hInstance, SystemInfo* sysInfo, UINT codePage); int create_main_window(HINSTANCE hInstance, SystemInfo* sysInfo, UINT codePage);
#endif // SYSTEM_INFO_H #endif // SYSTEM_INFO_H

View File

@@ -1,13 +1,13 @@
#ifndef SYSTEM_TRAY_H #ifndef SYSTEM_TRAY_H
#define SYSTEM_TRAY_H #define SYSTEM_TRAY_H
#include <windows.h> #include <windows.h>
#define WM_TRAYICON (WM_USER + 1) #define WM_TRAYICON (WM_USER + 1)
#define ID_TRAYICON 100 #define ID_TRAYICON 100
void create_tray_icon(HWND hWnd, HICON hIcon); void create_tray_icon(HWND hWnd, HICON hIcon);
void update_tray_icon(HWND hWnd, HICON hIcon, LPCTSTR tooltip); void update_tray_icon(HWND hWnd, HICON hIcon, LPCTSTR tooltip);
void remove_tray_icon(HWND hWnd); void remove_tray_icon(HWND hWnd);
#endif // SYSTEM_TRAY_H #endif // SYSTEM_TRAY_H

View File

@@ -1,18 +1,18 @@
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
#include <windows.h> #include <windows.h>
// 安全释放内存 // 安全释放内存
#define SAFE_FREE(ptr) if (ptr) { free(ptr); ptr = NULL; } #define SAFE_FREE(ptr) if (ptr) { free(ptr); ptr = NULL; }
// 宽字符转多字节字符串 // 宽字符转多字节字符串
char* wchar_to_mb(const wchar_t* wstr); char* wchar_to_mb(const wchar_t* wstr);
// 多字节字符串转宽字符 // 多字节字符串转宽字符
wchar_t* mb_to_wchar(const char* str); wchar_t* mb_to_wchar(const char* str);
// 获取当前时间字符串 // 获取当前时间字符串
char* get_current_time_string(); char* get_current_time_string();
#endif // UTILS_H #endif // UTILS_H

View File

@@ -1,16 +1,16 @@
#ifndef WINDOW_UTILS_H #ifndef WINDOW_UTILS_H
#define WINDOW_UTILS_H #define WINDOW_UTILS_H
#include <windows.h> #include <windows.h>
#include "system_info.h" #include "system_info.h"
// 窗口过程函数 // 窗口过程函数
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// 创建主窗口 // 创建主窗口
int create_main_window(HINSTANCE hInstance, SystemInfo* sysInfo, UINT codePage); int create_main_window(HINSTANCE hInstance, SystemInfo* sysInfo, UINT codePage);
// 注册窗口类 // 注册窗口类
BOOL register_window_class(HINSTANCE hInstance); BOOL register_window_class(HINSTANCE hInstance);
#endif // WINDOW_UTILS_H #endif // WINDOW_UTILS_H

View File

@@ -1,9 +1,9 @@
#include "about_dialog.h" #include "about_dialog.h"
#include <tchar.h> #include <tchar.h>
void show_about_dialog(HWND hParent) { void show_about_dialog(HWND hParent) {
MessageBox(hParent, MessageBox(hParent,
_T("系统信息查看器\n版本 1.0\n\n一个简单的Windows系统信息工具"), _T("系统信息查看器\n版本 1.0\n\n一个简单的Windows系统信息工具"),
_T("关于"), _T("关于"),
MB_OK | MB_ICONINFORMATION); MB_OK | MB_ICONINFORMATION);
} }

View File

@@ -1,40 +1,40 @@
#include "disk_info.h" #include "disk_info.h"
#include <tchar.h> #include <tchar.h>
void get_disk_info(DiskInfo* disks, int* count) { void get_disk_info(DiskInfo* disks, int* count) {
DWORD drives = GetLogicalDrives(); DWORD drives = GetLogicalDrives();
*count = 0; *count = 0;
for (char drive = 'A'; drive <= 'Z'; drive++) { for (char drive = 'A'; drive <= 'Z'; drive++) {
if (drives & (1 << (drive - 'A'))) { if (drives & (1 << (drive - 'A'))) {
TCHAR rootPath[4] = {drive, ':', '\\', '\0'}; TCHAR rootPath[4] = {drive, ':', '\\', '\0'};
TCHAR fileSystem[32]; TCHAR fileSystem[32];
DWORD serialNumber, maxComponentLength, fileSystemFlags; DWORD serialNumber, maxComponentLength, fileSystemFlags;
if (GetVolumeInformation( if (GetVolumeInformation(
rootPath, rootPath,
NULL, 0, NULL, 0,
&serialNumber, &serialNumber,
&maxComponentLength, &maxComponentLength,
&fileSystemFlags, &fileSystemFlags,
fileSystem, sizeof(fileSystem))) { fileSystem, sizeof(fileSystem))) {
DiskInfo* disk = &disks[(*count)++]; DiskInfo* disk = &disks[(*count)++];
disk->driveLetter = drive; disk->driveLetter = drive;
ULARGE_INTEGER freeBytes, totalBytes, totalFreeBytes; ULARGE_INTEGER freeBytes, totalBytes, totalFreeBytes;
if (GetDiskFreeSpaceEx( if (GetDiskFreeSpaceEx(
rootPath, rootPath,
&freeBytes, &freeBytes,
&totalBytes, &totalBytes,
&totalFreeBytes)) { &totalFreeBytes)) {
disk->totalBytes = totalBytes.QuadPart; disk->totalBytes = totalBytes.QuadPart;
disk->freeBytes = freeBytes.QuadPart; disk->freeBytes = freeBytes.QuadPart;
} }
strncpy(disk->fileSystem, fileSystem, sizeof(disk->fileSystem) - 1); strncpy(disk->fileSystem, fileSystem, sizeof(disk->fileSystem) - 1);
disk->fileSystem[sizeof(disk->fileSystem) - 1] = '\0'; disk->fileSystem[sizeof(disk->fileSystem) - 1] = '\0';
} }
} }
} }
} }

View File

@@ -1,41 +1,41 @@
#include "logging.h" #include "logging.h"
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <time.h> #include <time.h>
static const char* level_strings[] = { static const char* level_strings[] = {
"DEBUG", "DEBUG",
"INFO", "INFO",
"WARNING", "WARNING",
"ERROR" "ERROR"
}; };
void log_message(LogLevel level, const char* format, ...) { void log_message(LogLevel level, const char* format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
// 获取当前时间 // 获取当前时间
time_t now; time_t now;
time(&now); time(&now);
struct tm* timeinfo = localtime(&now); struct tm* timeinfo = localtime(&now);
char time_str[20]; char time_str[20];
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", timeinfo); strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", timeinfo);
// 格式化日志消息 // 格式化日志消息
char message[1024]; char message[1024];
vsnprintf(message, sizeof(message), format, args); vsnprintf(message, sizeof(message), format, args);
// 输出到调试控制台 // 输出到调试控制台
char output[2048]; char output[2048];
snprintf(output, sizeof(output), "[%s] [%s] %s\n", time_str, level_strings[level], message); snprintf(output, sizeof(output), "[%s] [%s] %s\n", time_str, level_strings[level], message);
OutputDebugStringA(output); OutputDebugStringA(output);
// 输出到文件 // 输出到文件
FILE* log_file = fopen("system_info.log", "a"); FILE* log_file = fopen("system_info.log", "a");
if (log_file) { if (log_file) {
fprintf(log_file, "%s", output); fprintf(log_file, "%s", output);
fclose(log_file); fclose(log_file);
} }
va_end(args); va_end(args);
} }

View File

@@ -1,29 +1,29 @@
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>
#include <locale.h> #include <locale.h>
#include "system_info.h" #include "system_info.h"
#include "window_utils.h" #include "window_utils.h"
#include "logging.h" #include "logging.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow); UNREFERENCED_PARAMETER(nCmdShow);
// 设置控制台编码为UTF-8 // 设置控制台编码为UTF-8
SetConsoleOutputCP(65001); SetConsoleOutputCP(65001);
setlocale(LC_ALL, "chs"); setlocale(LC_ALL, "chs");
// 初始化日志系统 // 初始化日志系统
log_message(LOG_INFO, "应用程序启动"); log_message(LOG_INFO, "应用程序启动");
// 初始化系统信息收集 // 初始化系统信息收集
SystemInfo sysInfo; SystemInfo sysInfo;
init_system_info(&sysInfo); init_system_info(&sysInfo);
// 创建并显示主窗口传递UTF-8编码标识 // 创建并显示主窗口传递UTF-8编码标识
int result = create_main_window(hInstance, &sysInfo, 65001); int result = create_main_window(hInstance, &sysInfo, 65001);
log_message(LOG_INFO, "应用程序退出"); log_message(LOG_INFO, "应用程序退出");
return result; return result;
} }

View File

@@ -1,31 +1,101 @@
#include "main_window.h" #include "main_window.h"
#include "disk_info.h"
#include <tchar.h> #include <tchar.h>
#include <commctrl.h> #include <commctrl.h>
#include <wchar.h> #include <wchar.h>
#include <stdio.h> #include <stdio.h>
#define IDC_INFO_TEXT 1002 #define IDC_INFO_TEXT 1002
#define IDM_FULLSCREEN 1003
// 全屏状态标志
static BOOL g_isFullScreen = FALSE;
// 保存原始窗口位置和大小
static RECT g_windowRect;
void toggle_fullscreen(HWND hWnd) {
g_isFullScreen = !g_isFullScreen;
if (g_isFullScreen) {
// 保存当前窗口位置和大小
GetWindowRect(hWnd, &g_windowRect);
// 设置全屏样式
SetWindowLong(hWnd, GWL_STYLE,
WS_OVERLAPPEDWINDOW & ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU));
// 设置全屏尺寸
MONITORINFO mi = {0};
mi.cbSize = sizeof(mi);
GetMonitorInfo(MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), &mi);
SetWindowPos(hWnd, HWND_TOP,
mi.rcMonitor.left,
mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_FRAMECHANGED);
} else {
// 恢复窗口样式
SetWindowLong(hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);
// 恢复原始大小和位置
SetWindowPos(hWnd, NULL,
g_windowRect.left,
g_windowRect.top,
g_windowRect.right - g_windowRect.left,
g_windowRect.bottom - g_windowRect.top,
SWP_FRAMECHANGED);
}
}
void update_main_window(HWND hWnd, SystemInfo* sysInfo) { void update_main_window(HWND hWnd, SystemInfo* sysInfo) {
// 根据全屏状态计算窗口尺寸
int windowWidth, windowHeight;
if (g_isFullScreen) {
MONITORINFO mi = {0};
mi.cbSize = sizeof(mi);
GetMonitorInfo(MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), &mi);
windowWidth = mi.rcMonitor.right - mi.rcMonitor.left;
windowHeight = mi.rcMonitor.bottom - mi.rcMonitor.top;
} else {
// 普通模式下使用70%屏幕尺寸
windowWidth = (int)(GetSystemMetrics(SM_CXSCREEN) * 0.7);
windowHeight = (int)(GetSystemMetrics(SM_CYSCREEN) * 0.7);
}
HWND hInfoText = GetDlgItem(hWnd, IDC_INFO_TEXT); HWND hInfoText = GetDlgItem(hWnd, IDC_INFO_TEXT);
if (!hInfoText) { if (hInfoText) {
// 更新现有控件大小和位置
SetWindowPos(hInfoText, NULL,
30, 50, windowWidth - 60, windowHeight - 80,
SWP_NOZORDER);
} else {
// 创建信息显示控件 // 创建信息显示控件
hInfoText = CreateWindow( hInfoText = CreateWindow(
_T("EDIT"), _T("EDIT"),
_T(""), _T(""),
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_READONLY, WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_READONLY | WS_BORDER,
20, 50, 800, 550, 30, 50, windowWidth - 60, windowHeight - 80,
hWnd, hWnd,
(HMENU)IDC_INFO_TEXT, (HMENU)IDC_INFO_TEXT,
(HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
NULL); NULL);
SendMessage(hInfoText, WM_SETFONT, // 计算动态字体大小
(WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); int fontSize = max(16, windowHeight / 30);
// 创建支持中文的字体
HFONT hFont = CreateFont(
fontSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
_T("Microsoft YaHei"));
SendMessage(hInfoText, WM_SETFONT, (WPARAM)hFont, TRUE);
} }
// 使用宽字符处理所有文本 // 使用宽字符处理所有文本
wchar_t infoText[2048]; wchar_t infoText[4096]; // 增大缓冲区以适应磁盘信息
wchar_t cpuNameW[256]; wchar_t cpuNameW[256];
MultiByteToWideChar(CP_UTF8, 0, sysInfo->cpuName, -1, cpuNameW, 256); MultiByteToWideChar(CP_UTF8, 0, sysInfo->cpuName, -1, cpuNameW, 256);
@@ -51,5 +121,26 @@ void update_main_window(HWND hWnd, SystemInfo* sysInfo) {
sysInfo->osVersion.dwMinorVersion, sysInfo->osVersion.dwMinorVersion,
sysInfo->osVersion.dwBuildNumber); sysInfo->osVersion.dwBuildNumber);
// 添加磁盘信息
DiskInfo disks[26];
int diskCount;
get_disk_info(disks, &diskCount);
wchar_t diskInfoText[2048];
swprintf(diskInfoText, 2048,
L"\r\n[磁盘信息]\r\n"
L"磁盘数量: %d\r\n", diskCount);
wcscat(infoText, diskInfoText);
for (int i = 0; i < diskCount; i++) {
swprintf(diskInfoText, 2048,
L"%c: 文件系统: %ls, 总容量: %.2f GB, 剩余容量: %.2f GB\r\n",
disks[i].driveLetter,
disks[i].fileSystem[0] ? L"NTFS" : L"",
(float)disks[i].totalBytes / (1024 * 1024 * 1024),
(float)disks[i].freeBytes / (1024 * 1024 * 1024));
wcscat(infoText, diskInfoText);
}
SetWindowTextW(hInfoText, infoText); SetWindowTextW(hInfoText, infoText);
} }

View File

@@ -1,85 +1,85 @@
#include "network_info.h" #include "network_info.h"
#include "config.h" #include "config.h"
#include <stdio.h> #include <stdio.h>
// 定义函数指针类型 // 定义函数指针类型
typedef DWORD (WINAPI *GetAdaptersInfoFunc)(_Out_ PIP_ADAPTER_INFO, _Inout_ PULONG); typedef DWORD (WINAPI *GetAdaptersInfoFunc)(_Out_ PIP_ADAPTER_INFO, _Inout_ PULONG);
// 安全的函数指针转换函数 // 安全的函数指针转换函数
static FARPROC safe_get_proc_address(HMODULE module, const char* name) { static FARPROC safe_get_proc_address(HMODULE module, const char* name) {
FARPROC proc = GetProcAddress(module, name); FARPROC proc = GetProcAddress(module, name);
if (!proc) { if (!proc) {
return NULL; return NULL;
} }
return proc; return proc;
} }
// 辅助宏用于安全的函数指针转换 // 辅助宏用于安全的函数指针转换
#define GET_PROC_ADDRESS(module, name, type) \ #define GET_PROC_ADDRESS(module, name, type) \
((type)(void*)safe_get_proc_address(module, name)) ((type)(void*)safe_get_proc_address(module, name))
void get_network_adapters(NetworkAdapterInfo* adapters, int* count) { void get_network_adapters(NetworkAdapterInfo* adapters, int* count) {
HMODULE hModule = LoadLibrary("iphlpapi.dll"); HMODULE hModule = LoadLibrary("iphlpapi.dll");
if (!hModule) return; if (!hModule) return;
// 使用类型安全的函数指针转换 // 使用类型安全的函数指针转换
GetAdaptersInfoFunc pGetAdaptersInfo = GET_PROC_ADDRESS(hModule, "GetAdaptersInfo", GetAdaptersInfoFunc); GetAdaptersInfoFunc pGetAdaptersInfo = GET_PROC_ADDRESS(hModule, "GetAdaptersInfo", GetAdaptersInfoFunc);
if (!pGetAdaptersInfo) { if (!pGetAdaptersInfo) {
FreeLibrary(hModule); FreeLibrary(hModule);
return; return;
} }
PIP_ADAPTER_INFO pAdapterInfo = NULL; PIP_ADAPTER_INFO pAdapterInfo = NULL;
PIP_ADAPTER_INFO pAdapter = NULL; PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0; DWORD dwRetVal = 0;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
*count = 0; *count = 0;
pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO)); pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) { if (pAdapterInfo == NULL) {
FreeLibrary(hModule); FreeLibrary(hModule);
return; return;
} }
// 第一次调用获取缓冲区大小 // 第一次调用获取缓冲区大小
if (pGetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { if (pGetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo); free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen); pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);
if (pAdapterInfo == NULL) { if (pAdapterInfo == NULL) {
FreeLibrary(hModule); FreeLibrary(hModule);
return; return;
} }
} }
if ((dwRetVal = pGetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { if ((dwRetVal = pGetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo; pAdapter = pAdapterInfo;
while (pAdapter && *count < MAX_ADAPTERS) { while (pAdapter && *count < MAX_ADAPTERS) {
NetworkAdapterInfo* adapter = &adapters[(*count)++]; NetworkAdapterInfo* adapter = &adapters[(*count)++];
// 复制适配器名称和描述 // 复制适配器名称和描述
strncpy(adapter->adapterName, pAdapter->AdapterName, MAX_ADAPTER_NAME_LENGTH); strncpy(adapter->adapterName, pAdapter->AdapterName, MAX_ADAPTER_NAME_LENGTH);
strncpy(adapter->description, pAdapter->Description, MAX_ADAPTER_DESCRIPTION_LENGTH); strncpy(adapter->description, pAdapter->Description, MAX_ADAPTER_DESCRIPTION_LENGTH);
// 格式化IP地址 // 格式化IP地址
if (pAdapter->IpAddressList.IpAddress.String[0] != '\0') { if (pAdapter->IpAddressList.IpAddress.String[0] != '\0') {
strncpy(adapter->ipAddress, pAdapter->IpAddressList.IpAddress.String, 15); strncpy(adapter->ipAddress, pAdapter->IpAddressList.IpAddress.String, 15);
adapter->ipAddress[15] = '\0'; adapter->ipAddress[15] = '\0';
} else { } else {
strcpy(adapter->ipAddress, "N/A"); strcpy(adapter->ipAddress, "N/A");
} }
// 格式化MAC地址 // 格式化MAC地址
sprintf(adapter->macAddress, "%02X:%02X:%02X:%02X:%02X:%02X", sprintf(adapter->macAddress, "%02X:%02X:%02X:%02X:%02X:%02X",
pAdapter->Address[0], pAdapter->Address[1], pAdapter->Address[0], pAdapter->Address[1],
pAdapter->Address[2], pAdapter->Address[3], pAdapter->Address[2], pAdapter->Address[3],
pAdapter->Address[4], pAdapter->Address[5]); pAdapter->Address[4], pAdapter->Address[5]);
adapter->speed = pAdapter->DhcpEnabled ? 100 : 10; // 简化处理,实际项目中应使用更精确的方法 adapter->speed = pAdapter->DhcpEnabled ? 100 : 10; // 简化处理,实际项目中应使用更精确的方法
pAdapter = pAdapter->Next; pAdapter = pAdapter->Next;
} }
} }
free(pAdapterInfo); free(pAdapterInfo);
} }

View File

@@ -1,137 +1,137 @@
#include "performance_info.h" #include "performance_info.h"
#include <pdh.h> #include <pdh.h>
#include <psapi.h> #include <psapi.h>
#include "performance_info.h" #include "performance_info.h"
#include <windows.h> #include <windows.h>
#include <psapi.h> #include <psapi.h>
// 定义函数指针类型 // 定义函数指针类型
typedef PDH_STATUS (WINAPI *PdhOpenQueryFunc)(_In_opt_ LPCSTR, _In_ DWORD_PTR, _Out_ PDH_HQUERY*); typedef PDH_STATUS (WINAPI *PdhOpenQueryFunc)(_In_opt_ LPCSTR, _In_ DWORD_PTR, _Out_ PDH_HQUERY*);
typedef PDH_STATUS (WINAPI *PdhAddCounterFunc)(_In_ PDH_HQUERY, _In_ LPCSTR, _In_ DWORD_PTR, _Out_ PDH_HCOUNTER*); typedef PDH_STATUS (WINAPI *PdhAddCounterFunc)(_In_ PDH_HQUERY, _In_ LPCSTR, _In_ DWORD_PTR, _Out_ PDH_HCOUNTER*);
typedef PDH_STATUS (WINAPI *PdhCollectQueryDataFunc)(_In_ PDH_HQUERY); typedef PDH_STATUS (WINAPI *PdhCollectQueryDataFunc)(_In_ PDH_HQUERY);
typedef PDH_STATUS (WINAPI *PdhGetFormattedCounterValueFunc)(_In_ PDH_HCOUNTER, _In_ DWORD, _Out_opt_ LPDWORD, _Out_ PPDH_FMT_COUNTERVALUE); typedef PDH_STATUS (WINAPI *PdhGetFormattedCounterValueFunc)(_In_ PDH_HCOUNTER, _In_ DWORD, _Out_opt_ LPDWORD, _Out_ PPDH_FMT_COUNTERVALUE);
// 安全的函数指针转换函数 // 安全的函数指针转换函数
static FARPROC safe_get_proc_address(HMODULE module, const char* name) { static FARPROC safe_get_proc_address(HMODULE module, const char* name) {
FARPROC proc = GetProcAddress(module, name); FARPROC proc = GetProcAddress(module, name);
if (!proc) { if (!proc) {
return NULL; return NULL;
} }
return proc; return proc;
} }
// 辅助宏用于安全的函数指针转换 // 辅助宏用于安全的函数指针转换
#define GET_PROC_ADDRESS(module, name, type) \ #define GET_PROC_ADDRESS(module, name, type) \
((type)(void*)safe_get_proc_address(module, name)) ((type)(void*)safe_get_proc_address(module, name))
static PDH_HQUERY cpuQuery; static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal; static PDH_HCOUNTER cpuTotal;
static HMODULE hPdhModule = NULL; static HMODULE hPdhModule = NULL;
static PdhOpenQueryFunc pPdhOpenQuery = NULL; static PdhOpenQueryFunc pPdhOpenQuery = NULL;
static PdhAddCounterFunc pPdhAddCounter = NULL; static PdhAddCounterFunc pPdhAddCounter = NULL;
static PdhCollectQueryDataFunc pPdhCollectQueryData = NULL; static PdhCollectQueryDataFunc pPdhCollectQueryData = NULL;
static PdhGetFormattedCounterValueFunc pPdhGetFormattedCounterValue = NULL; static PdhGetFormattedCounterValueFunc pPdhGetFormattedCounterValue = NULL;
void init_performance_counters() { void init_performance_counters() {
hPdhModule = LoadLibrary("pdh.dll"); hPdhModule = LoadLibrary("pdh.dll");
if (!hPdhModule) { if (!hPdhModule) {
return; return;
} }
pPdhOpenQuery = GET_PROC_ADDRESS(hPdhModule, "PdhOpenQueryA", PdhOpenQueryFunc); pPdhOpenQuery = GET_PROC_ADDRESS(hPdhModule, "PdhOpenQueryA", PdhOpenQueryFunc);
pPdhAddCounter = GET_PROC_ADDRESS(hPdhModule, "PdhAddCounterA", PdhAddCounterFunc); pPdhAddCounter = GET_PROC_ADDRESS(hPdhModule, "PdhAddCounterA", PdhAddCounterFunc);
pPdhCollectQueryData = GET_PROC_ADDRESS(hPdhModule, "PdhCollectQueryData", PdhCollectQueryDataFunc); pPdhCollectQueryData = GET_PROC_ADDRESS(hPdhModule, "PdhCollectQueryData", PdhCollectQueryDataFunc);
pPdhGetFormattedCounterValue = GET_PROC_ADDRESS(hPdhModule, "PdhGetFormattedCounterValue", PdhGetFormattedCounterValueFunc); pPdhGetFormattedCounterValue = GET_PROC_ADDRESS(hPdhModule, "PdhGetFormattedCounterValue", PdhGetFormattedCounterValueFunc);
if (pPdhOpenQuery && pPdhAddCounter && pPdhCollectQueryData) { if (pPdhOpenQuery && pPdhAddCounter && pPdhCollectQueryData) {
// 初始化性能计数器 // 初始化性能计数器
if (pPdhOpenQuery(NULL, 0, &cpuQuery) != ERROR_SUCCESS) { if (pPdhOpenQuery(NULL, 0, &cpuQuery) != ERROR_SUCCESS) {
return; return;
} }
// 添加CPU计数器 // 添加CPU计数器
if (pPdhAddCounter(cpuQuery, "\\Processor(_Total)\\% Processor Time", 0, &cpuTotal) != ERROR_SUCCESS) { if (pPdhAddCounter(cpuQuery, "\\Processor(_Total)\\% Processor Time", 0, &cpuTotal) != ERROR_SUCCESS) {
return; return;
} }
// 第一次收集数据,用于初始化 // 第一次收集数据,用于初始化
pPdhCollectQueryData(cpuQuery); pPdhCollectQueryData(cpuQuery);
Sleep(1000); // 等待1秒获取基准数据 Sleep(1000); // 等待1秒获取基准数据
pPdhCollectQueryData(cpuQuery); pPdhCollectQueryData(cpuQuery);
} }
} }
void get_performance_info(PerformanceInfo* perfInfo) { void get_performance_info(PerformanceInfo* perfInfo) {
static BOOL initialized = FALSE; static BOOL initialized = FALSE;
if (!initialized) { if (!initialized) {
init_performance_counters(); init_performance_counters();
initialized = TRUE; initialized = TRUE;
} }
// 获取CPU使用率 // 获取CPU使用率
if (pPdhCollectQueryData && pPdhGetFormattedCounterValue) { if (pPdhCollectQueryData && pPdhGetFormattedCounterValue) {
// 第一次收集数据作为基准 // 第一次收集数据作为基准
pPdhCollectQueryData(cpuQuery); pPdhCollectQueryData(cpuQuery);
Sleep(1000); // 等待1秒 Sleep(1000); // 等待1秒
pPdhCollectQueryData(cpuQuery); // 第二次收集数据 pPdhCollectQueryData(cpuQuery); // 第二次收集数据
PDH_FMT_COUNTERVALUE counterVal; PDH_FMT_COUNTERVALUE counterVal;
if (pPdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal) == ERROR_SUCCESS) { if (pPdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal) == ERROR_SUCCESS) {
perfInfo->cpuUsage = (DWORD)counterVal.doubleValue; perfInfo->cpuUsage = (DWORD)counterVal.doubleValue;
} else { } else {
perfInfo->cpuUsage = 0; perfInfo->cpuUsage = 0;
} }
} else { } else {
// 如果PDH不可用使用GetSystemTimes作为备用方案 // 如果PDH不可用使用GetSystemTimes作为备用方案
FILETIME idleTime, kernelTime, userTime; FILETIME idleTime, kernelTime, userTime;
if (GetSystemTimes(&idleTime, &kernelTime, &userTime)) { if (GetSystemTimes(&idleTime, &kernelTime, &userTime)) {
ULONGLONG idle = ((ULONGLONG)idleTime.dwHighDateTime << 32) | idleTime.dwLowDateTime; ULONGLONG idle = ((ULONGLONG)idleTime.dwHighDateTime << 32) | idleTime.dwLowDateTime;
ULONGLONG kernel = ((ULONGLONG)kernelTime.dwHighDateTime << 32) | kernelTime.dwLowDateTime; ULONGLONG kernel = ((ULONGLONG)kernelTime.dwHighDateTime << 32) | kernelTime.dwLowDateTime;
ULONGLONG user = ((ULONGLONG)userTime.dwHighDateTime << 32) | userTime.dwLowDateTime; ULONGLONG user = ((ULONGLONG)userTime.dwHighDateTime << 32) | userTime.dwLowDateTime;
static ULONGLONG prevIdle = 0, prevKernel = 0, prevUser = 0; static ULONGLONG prevIdle = 0, prevKernel = 0, prevUser = 0;
ULONGLONG idleDiff = idle - prevIdle; ULONGLONG idleDiff = idle - prevIdle;
ULONGLONG kernelDiff = kernel - prevKernel; ULONGLONG kernelDiff = kernel - prevKernel;
ULONGLONG userDiff = user - prevUser; ULONGLONG userDiff = user - prevUser;
if (prevIdle != 0 && (kernelDiff + userDiff) > 0) { if (prevIdle != 0 && (kernelDiff + userDiff) > 0) {
perfInfo->cpuUsage = (DWORD)(100.0 - (100.0 * idleDiff) / (kernelDiff + userDiff)); perfInfo->cpuUsage = (DWORD)(100.0 - (100.0 * idleDiff) / (kernelDiff + userDiff));
} else { } else {
perfInfo->cpuUsage = 0; perfInfo->cpuUsage = 0;
} }
prevIdle = idle; prevIdle = idle;
prevKernel = kernel; prevKernel = kernel;
prevUser = user; prevUser = user;
} else { } else {
perfInfo->cpuUsage = 0; perfInfo->cpuUsage = 0;
} }
} }
// 获取内存使用率 // 获取内存使用率
MEMORYSTATUSEX memInfo; MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX); memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo); GlobalMemoryStatusEx(&memInfo);
// 使用GlobalMemoryStatusEx获取更精确的内存使用率 // 使用GlobalMemoryStatusEx获取更精确的内存使用率
MEMORYSTATUSEX memStatus; MEMORYSTATUSEX memStatus;
memStatus.dwLength = sizeof(memStatus); memStatus.dwLength = sizeof(memStatus);
GlobalMemoryStatusEx(&memStatus); GlobalMemoryStatusEx(&memStatus);
perfInfo->memoryUsage = memStatus.dwMemoryLoad; perfInfo->memoryUsage = memStatus.dwMemoryLoad;
// 获取进程和线程数量 // 获取进程和线程数量
PERFORMANCE_INFORMATION perfInfoStruct; PERFORMANCE_INFORMATION perfInfoStruct;
GetPerformanceInfo(&perfInfoStruct, sizeof(perfInfoStruct)); GetPerformanceInfo(&perfInfoStruct, sizeof(perfInfoStruct));
perfInfo->processesCount = perfInfoStruct.ProcessCount; perfInfo->processesCount = perfInfoStruct.ProcessCount;
perfInfo->threadsCount = perfInfoStruct.ThreadCount; perfInfo->threadsCount = perfInfoStruct.ThreadCount;
perfInfo->handlesCount = perfInfoStruct.HandleCount; perfInfo->handlesCount = perfInfoStruct.HandleCount;
} }
void cleanup_performance_counters() { void cleanup_performance_counters() {
if (hPdhModule) { if (hPdhModule) {
FreeLibrary(hPdhModule); FreeLibrary(hPdhModule);
hPdhModule = NULL; hPdhModule = NULL;
} }
} }

View File

@@ -1,26 +1,26 @@
#include "system_info.h" #include "system_info.h"
#include <intrin.h> #include <intrin.h>
void init_system_info(SystemInfo* sysInfo) { void init_system_info(SystemInfo* sysInfo) {
// 获取CPU信息 // 获取CPU信息
int cpuInfo[4] = {0}; int cpuInfo[4] = {0};
__cpuid(cpuInfo, 0x80000002); __cpuid(cpuInfo, 0x80000002);
memcpy(sysInfo->cpuName, cpuInfo, sizeof(cpuInfo)); memcpy(sysInfo->cpuName, cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000003); __cpuid(cpuInfo, 0x80000003);
memcpy(sysInfo->cpuName + 16, cpuInfo, sizeof(cpuInfo)); memcpy(sysInfo->cpuName + 16, cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000004); __cpuid(cpuInfo, 0x80000004);
memcpy(sysInfo->cpuName + 32, cpuInfo, sizeof(cpuInfo)); memcpy(sysInfo->cpuName + 32, cpuInfo, sizeof(cpuInfo));
// 获取系统信息 // 获取系统信息
GetSystemInfo(&sysInfo->systemInfo); GetSystemInfo(&sysInfo->systemInfo);
sysInfo->cpuCores = sysInfo->systemInfo.dwNumberOfProcessors; sysInfo->cpuCores = sysInfo->systemInfo.dwNumberOfProcessors;
sysInfo->cpuThreads = sysInfo->cpuCores; // 简化处理 sysInfo->cpuThreads = sysInfo->cpuCores; // 简化处理
// 获取内存信息 // 获取内存信息
sysInfo->memoryStatus.dwLength = sizeof(sysInfo->memoryStatus); sysInfo->memoryStatus.dwLength = sizeof(sysInfo->memoryStatus);
GlobalMemoryStatusEx(&sysInfo->memoryStatus); GlobalMemoryStatusEx(&sysInfo->memoryStatus);
// 获取操作系统版本 // 获取操作系统版本
sysInfo->osVersion.dwOSVersionInfoSize = sizeof(sysInfo->osVersion); sysInfo->osVersion.dwOSVersionInfoSize = sizeof(sysInfo->osVersion);
GetVersionEx((LPOSVERSIONINFO)&sysInfo->osVersion); GetVersionEx((LPOSVERSIONINFO)&sysInfo->osVersion);
} }

View File

@@ -1,45 +1,45 @@
#include "system_tray.h" #include "system_tray.h"
#include <shellapi.h> #include <shellapi.h>
#include <strsafe.h> #include <strsafe.h>
void create_tray_icon(HWND hWnd, HICON hIcon) { void create_tray_icon(HWND hWnd, HICON hIcon) {
NOTIFYICONDATA nid = {0}; NOTIFYICONDATA nid = {0};
nid.cbSize = sizeof(NOTIFYICONDATA); nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd; nid.hWnd = hWnd;
nid.uID = ID_TRAYICON; nid.uID = ID_TRAYICON;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_TRAYICON; nid.uCallbackMessage = WM_TRAYICON;
nid.hIcon = hIcon; nid.hIcon = hIcon;
#if defined(UNICODE) || defined(_UNICODE) #if defined(UNICODE) || defined(_UNICODE)
StringCbCopyW(nid.szTip, sizeof(nid.szTip), L"系统信息查看器"); StringCbCopyW(nid.szTip, sizeof(nid.szTip), L"系统信息查看器");
#else #else
StringCbCopyA(nid.szTip, sizeof(nid.szTip), "系统信息查看器"); StringCbCopyA(nid.szTip, sizeof(nid.szTip), "系统信息查看器");
#endif #endif
Shell_NotifyIcon(NIM_ADD, &nid); Shell_NotifyIcon(NIM_ADD, &nid);
} }
void update_tray_icon(HWND hWnd, HICON hIcon, LPCTSTR tooltip) { void update_tray_icon(HWND hWnd, HICON hIcon, LPCTSTR tooltip) {
NOTIFYICONDATA nid = {0}; NOTIFYICONDATA nid = {0};
nid.cbSize = sizeof(NOTIFYICONDATA); nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd; nid.hWnd = hWnd;
nid.uID = ID_TRAYICON; nid.uID = ID_TRAYICON;
nid.uFlags = NIF_ICON | NIF_TIP; nid.uFlags = NIF_ICON | NIF_TIP;
nid.hIcon = hIcon; nid.hIcon = hIcon;
#if defined(UNICODE) || defined(_UNICODE) #if defined(UNICODE) || defined(_UNICODE)
StringCbCopyW(nid.szTip, sizeof(nid.szTip), tooltip); StringCbCopyW(nid.szTip, sizeof(nid.szTip), tooltip);
#else #else
StringCbCopyA(nid.szTip, sizeof(nid.szTip), tooltip); StringCbCopyA(nid.szTip, sizeof(nid.szTip), tooltip);
#endif #endif
Shell_NotifyIcon(NIM_MODIFY, &nid); Shell_NotifyIcon(NIM_MODIFY, &nid);
} }
void remove_tray_icon(HWND hWnd) { void remove_tray_icon(HWND hWnd) {
NOTIFYICONDATA nid = {0}; NOTIFYICONDATA nid = {0};
nid.cbSize = sizeof(NOTIFYICONDATA); nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd; nid.hWnd = hWnd;
nid.uID = ID_TRAYICON; nid.uID = ID_TRAYICON;
Shell_NotifyIcon(NIM_DELETE, &nid); Shell_NotifyIcon(NIM_DELETE, &nid);
} }

View File

@@ -1,38 +1,38 @@
#include "utils.h" #include "utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
char* wchar_to_mb(const wchar_t* wstr) { char* wchar_to_mb(const wchar_t* wstr) {
if (!wstr) return NULL; if (!wstr) return NULL;
int size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); int size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* mbstr = (char*)malloc(size); char* mbstr = (char*)malloc(size);
if (mbstr) { if (mbstr) {
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, mbstr, size, NULL, NULL); WideCharToMultiByte(CP_UTF8, 0, wstr, -1, mbstr, size, NULL, NULL);
} }
return mbstr; return mbstr;
} }
wchar_t* mb_to_wchar(const char* str) { wchar_t* mb_to_wchar(const char* str) {
if (!str) return NULL; if (!str) return NULL;
int size = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); int size = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
wchar_t* wstr = (wchar_t*)malloc(size * sizeof(wchar_t)); wchar_t* wstr = (wchar_t*)malloc(size * sizeof(wchar_t));
if (wstr) { if (wstr) {
MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, size); MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, size);
} }
return wstr; return wstr;
} }
char* get_current_time_string() { char* get_current_time_string() {
time_t now; time_t now;
time(&now); time(&now);
struct tm* timeinfo = localtime(&now); struct tm* timeinfo = localtime(&now);
char* time_str = (char*)malloc(20); char* time_str = (char*)malloc(20);
if (time_str) { if (time_str) {
strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", timeinfo); strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", timeinfo);
} }
return time_str; return time_str;
} }

View File

@@ -21,7 +21,7 @@ BOOL register_window_class(HINSTANCE hInstance) {
wcex.hInstance = hInstance; wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.hbrBackground = CreateSolidBrush(RGB(240, 240, 240));
wcex.lpszMenuName = NULL; wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("SystemInfoWindowClass"); wcex.lpszClassName = _T("SystemInfoWindowClass");
wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION); wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
@@ -47,10 +47,10 @@ int create_main_window(HINSTANCE hInstance, SystemInfo* sysInfo, UINT codePage)
HWND hWnd = CreateWindowW( HWND hWnd = CreateWindowW(
L"SystemInfoWindowClass", L"SystemInfoWindowClass",
windowTitle, windowTitle,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_CLIPCHILDREN, WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
450, 300, 800, 600,
NULL, NULL, hInstance, NULL); NULL, NULL, hInstance, sysInfo);
if (!hWnd) { if (!hWnd) {
return 0; return 0;
@@ -75,14 +75,28 @@ int create_main_window(HINSTANCE hInstance, SystemInfo* sysInfo, UINT codePage)
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) { switch (message) {
case WM_CREATE: { case WM_CREATE: {
// 安全初始化系统信息指针
if (lParam) {
g_sysInfo = (SystemInfo*)((CREATESTRUCT*)lParam)->lpCreateParams;
}
if (!g_sysInfo) {
MessageBoxW(hWnd, L"系统信息初始化失败", L"错误", MB_ICONERROR);
return -1;
}
// 创建显示系统信息的按钮 // 创建显示系统信息的按钮
CreateWindowW(L"BUTTON", L"刷新信息", CreateWindowW(L"BUTTON", L"刷新信息",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_BORDER,
10, 10, 150, 30, 10, 10, 150, 30,
hWnd, (HMENU)IDC_MAIN_BUTTON, hWnd, (HMENU)IDC_MAIN_BUTTON,
(HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL); (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);
break; break;
} }
case WM_SIZE: {
// 窗口大小变化时更新布局
update_main_window(hWnd, g_sysInfo);
break;
}
case WM_COMMAND: { case WM_COMMAND: {
if (LOWORD(wParam) == IDC_MAIN_BUTTON) { if (LOWORD(wParam) == IDC_MAIN_BUTTON) {
// 刷新系统信息 // 刷新系统信息
@@ -91,6 +105,25 @@ LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
} }
break; break;
} }
case WM_KEYDOWN: {
// F11键切换全屏
if (wParam == VK_F11) {
toggle_fullscreen(hWnd);
update_main_window(hWnd, g_sysInfo);
}
break;
}
case WM_NCCALCSIZE:
if (wParam) {
return 0;
}
break;
case WM_ERASEBKGND: {
RECT rc;
GetClientRect(hWnd, &rc);
FillRect((HDC)wParam, &rc, (HBRUSH)GetClassLongPtr(hWnd, GCLP_HBRBACKGROUND));
return 1;
}
case WM_DESTROY: { case WM_DESTROY: {
PostQuitMessage(0); PostQuitMessage(0);
break; break;