简体   繁体   中英

Icon in Windows Explorer showing 2 different icons when using resource.rc

I'm trying to change the icon of my Windows application. I'm using the simple window code below and added a resource.h and resource.rc to the solution. I'm using a test.ico and a test2.ico -file which both contain 64x64, 32x32, 24x24 and 16x16 sizes.

The when I switch between icon and icon2 by simple changing this line TESTICON ICON "test.ico" to TESTICON ICON "test2.ico" in the.rc-file, the icons change accordingly in program header bar, taskbar and alt-tab view. But the icon in my windows explorer is acting totally strange. When I set the view to details,list or small I see the test.ico -icon, but for medium, large and extra large I see the test2.ico -icon, no matter what I set in the.rc-file. I'm totally lost here, which setting am I missing?

The 'resource.h'

#pragma once
#define TESTICON 1

and the 'resource.rc'

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

The simple windows source:

#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);
}

As pointed out by Jonathan Potter: Explorer has an icon cache; if it's showing the wrong icon you probably need to clear the cache. http://leodavidson.blogspot.com/2011/05/clear-icon-cache-1001.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM