1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include <windows.h>
- #define BUTTON_SIZE 40
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- // `shell32.dll` 中图标的索引
- int iconIndexes[16] = {4, 5, 7, 10, 14, 16, 18, 21,
- 23, 27, 31, 35, 39, 43, 45, 60};
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow) {
- // 注册窗口类
- const char CLASS_NAME[] = "SimpleWindowClass";
- WNDCLASS wc = {};
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = CLASS_NAME;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- RegisterClass(&wc);
- // 窗口宽度和高度
- int w = 320;
- int h = 80;
- // 计算窗口显示位置
- int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
- int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
- // 创建窗口
- HWND hwnd =
- CreateWindowEx(WS_EX_TOPMOST, CLASS_NAME, "", WS_POPUP | WS_VISIBLE, x, y,
- w, h, NULL, NULL, hInstance, NULL);
- if (!hwnd) {
- return 0;
- }
- // 显示窗口
- ShowWindow(hwnd, nCmdShow);
- // 进入消息循环
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
- LPARAM lParam) {
- switch (uMsg) {
- case WM_CREATE: {
- // 获取 shell32.dll 的路径
- char shell32Path[MAX_PATH];
- GetSystemDirectory(shell32Path, MAX_PATH);
- strcat(shell32Path, "\\shell32.dll");
- // 创建16个按钮并加载不同图标
- for (int i = 0; i < 16; ++i) {
- int x = (i % 8) * BUTTON_SIZE;
- int y = (i / 8) * BUTTON_SIZE;
- HWND hButton = CreateWindow(
- "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_ICON, x, y, BUTTON_SIZE,
- BUTTON_SIZE, hwnd, (HMENU)(1000 + i), GetModuleHandle(NULL), NULL);
- // 使用 ExtractIcon 从 shell32.dll 中提取图标
- HICON hIcon =
- ExtractIcon(GetModuleHandle(NULL), shell32Path, iconIndexes[i]);
- SendMessage(hButton, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
- }
- return 0;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
|