How can I load the same icon as used by MessageBox on Windows 10?

落爺英雄遲暮 提交于 2021-02-15 05:07:44

问题


On Windows 10 calling LoadIcon asking for the standard icon IDI_INFORMATION yields this icon:

On the other hand, calling MessageBox passing IDI_INFORMATION produces a dialog that uses this icon:

How can I obtain the second icon, if the obvious call to LoadIcon does not do so?


回答1:


This feels like a bug in user32.dll but Windows 8 has the same issue so I guess Microsoft doesn't care.

You can get the flat icon used by MessageBox by calling SHGetStockIconInfo:

SHSTOCKICONINFO sii;
sii.cbSize = sizeof(sii);
if (SUCCEEDED(SHGetStockIconInfo(SIID_INFO, SHGSI_ICON|SHGSI_LARGEICON, &sii)))
{
    // Use sii.hIcon here...
    DestroyIcon(sii.hIcon);
}

SHGetStockIconInfo is the documented way to get icons used in the Windows UI on Vista and later. Most of the icons come from imageres.dll but you should not assume that this is the case...




回答2:


we can try next code for test/demo

    MSGBOXPARAMSW mbi = { 
        sizeof(mbi), 
        HWND_DESKTOP, 
        NULL,
        L"lpszText",
        L"lpszCaption",
        MB_USERICON,
        IDI_INFORMATION
    };
    MessageBoxIndirectW(&mbi);

    if (HMODULE hmodImageRes = LoadLibraryEx(L"imageres", 0, LOAD_LIBRARY_AS_DATAFILE))
    {
        mbi.hInstance = hmodImageRes;
        mbi.lpszIcon = MAKEINTRESOURCE(81);
        MessageBoxIndirectW(&mbi);
        FreeLibrary(hmodImageRes);
    }

first message box use standard IDI_INFORMATION icon

when second the same icon on windows 7, and on windows 8.1 and windows 10.

are MAKEINTRESOURCE(81) from imageres.dll somehow documented and be stable - i doubt


so obtain the second icon you can by LoadIcon(hmodImageRes, MAKEINTRESOURCE(81)) where HMODULE hmodImageRes = LoadLibraryEx(L"imageres", 0, LOAD_LIBRARY_AS_DATAFILE) or simply LoadLibrary(L"imageres")



来源:https://stackoverflow.com/questions/44718680/how-can-i-load-the-same-icon-as-used-by-messagebox-on-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!