使用 resource.rc 时,Windows 资源管理器中的图标显示 2 个不同的图标

Posted

技术标签:

【中文标题】使用 resource.rc 时,Windows 资源管理器中的图标显示 2 个不同的图标【英文标题】:Icon in Windows Explorer showing 2 different icons when using resource.rc 【发布时间】:2020-06-14 14:57:44 【问题描述】:

我正在尝试更改我的 Windows 应用程序的图标。我正在使用下面的简单窗口代码并将resource.hresource.rc 添加到解决方案中。我正在使用 test.icotest2.ico 文件,它们都包含 64x64、32x32、24x24 和 16x16 尺寸。

当我通过在 .rc 文件中将这一行 TESTICON ICON "test.ico" 更改为 TESTICON ICON "test2.ico" 在 icon 和 icon2 之间切换时,程序标题栏、任务栏和 alt-tab 视图中的图标会相应更改。但是我的 Windows 资源管理器中的图标表现得非常奇怪。当我将视图设置为详细信息、列表或小时,我会看到 test.ico-icon,但对于中型、大型和超大,我会看到 test2.ico-icon,无论我在 .rc 文件中设置什么。我完全迷路了,我错过了哪个设置?

'resource.h'

#pragma once
#define TESTICON 1

和'resource.rc'

#include "resource.h"
TESTICON ICON "test.ico"

简单的windows源码:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)


    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASSEX wc =  ;

    //Step 1: Registering the Window Class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = CLASS_NAME;
    wc.hIcon = (HICON)LoadImage(hInstance,
        MAKEINTRESOURCE(TESTICON), IMAGE_ICON,
        ::GetSystemMetrics(SM_CXICON),
        ::GetSystemMetrics(SM_CYICON), 0);
    wc.hIconSm = (HICON)LoadImage(hInstance,
         MAKEINTRESOURCE(TESTICON), IMAGE_ICON,
        ::GetSystemMetrics(SM_CXSMICON),
        ::GetSystemMetrics(SM_CYSMICON), 0);

    RegisterClassEx(&wc);

    // Create the window.
    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL)
    
        return 0;
    

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.
    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_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        // All painting occurs here, between BeginPaint and EndPaint.
        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
        EndPaint(hwnd, &ps);
    
    return 0;
    

    return DefWindowProc(hwnd, uMsg, wParam, lParam);

【问题讨论】:

Explorer 有一个图标缓存;如果它显示错误的图标,您可能需要清除缓存。 leodavidson.blogspot.com/2011/05/clear-icon-cache-1001.html 无论我在文件资源管理器中更改什么视图,它都会向我展示 "test.ico"。您能否提供更多信息和重现此问题的步骤? @JonathanPotter 感谢您的提示,我会检查一下。在某些时候它有点让我发疯。 @RitaHan-MSFT 示例 .rc 文件确实显示了 test.ico。我手动将其更改为 test2.ico 是的,我知道,但我无法重现您提到的内容“当我将视图设置为详细信息、列表或小时,我看到了 test.ico-icon,但对于中等,无论我在 .rc 文件中设置什么,我都看到了 test2.ico 图标。” 你能展示一些快照来展示你所看到的吗? 【参考方案1】:

正如 Jonathan Potter 所指出的:Explorer 有一个图标缓存;如果它显示错误的图标,您可能需要清除缓存。 http://leodavidson.blogspot.com/2011/05/clear-icon-cache-1001.html

【讨论】:

您好 mca2,请随时 accept your own answer。

以上是关于使用 resource.rc 时,Windows 资源管理器中的图标显示 2 个不同的图标的主要内容,如果未能解决你的问题,请参考以下文章

Windows 资源字段溢出到下一个字段

资源 (.rc) 文件包含标头忽略 Visual Studio 项目包含路径

C++ 调试 LoadImage() 和跳过断点

生成的资源文件包含奇怪的包含文件

QT 库连接问题

资源管理器中未显示 DLL 版本信息