Win32 Cannot LoadImageW at runtime.

huangapple go评论56阅读模式
英文:

Win32 Cannot LoadImageW at runtime

问题

尝试使用Win32 API加载应用程序的图标。

case WM_CREATE: {
    auto hIcon = LoadImageW(NULL, L"C:\\icon.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
    if (hIcon) {
        SendMessage(handle, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
    } else {
        auto lastError = GetLastError();
        MessageBox(handle, L"无法加载图标!", L"错误", MB_OK | MB_ICONERROR);
    }
}

我得到的是如上所示的消息框,lastError 的值为 0。图标位于指定的绝对路径,如果将文件重命名以使路径无效,最后的错误代码将为 2(文件未找到)。因此,我认为图标已经找到。

我尝试过的方法:

  • 尝试使用相对路径。
  • 使用Windows Paint保存为不同的文件格式,如.png和.bmp,以避免不正确的格式错误。
  • 添加不同的标志,如 LR_LOADTRANSPARENT
  • handle 为NULL或GetModuleHandle(NULL)
英文:

Trying to use Win32 API to load an icon for an application.

case WM_CREATE: {
auto hIcon = LoadImageW(NULL, L"C:\\icon.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if (hIcon) {
	SendMessage(handle, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
} else {
	auto lastError = GetLastError();
	MessageBox(handle, L"Could not load icon!", L"ERROR", MB_OK | MB_ICONERROR);
}
}

What I get is the message box as above, with lastError being 0. The icon is located at the absolute path specified, and if the file is renamed such that the path is invalid last erorr code is 2 (file not found). Therefore, I assume the icon is found.

What I've tried:

  • Playing with relative paths.
  • Saving in different file formats, .png and .bmp, using Windows Paint, to avoid improper format errors.
  • Adding different flags, such as LR_LOADTRANSPARENT.
  • handle is NULL or GetModuleHandle(NULL)

答案1

得分: 1

图片必须是特定格式的.ico文件。对我来说有效的格式是在GIMP中选择“Windows图标”格式导出的。图标之前是以.ico格式存在的,只是出了一些问题。从.bmp和.png加载图标的示例对我不起作用,这仅适用于位图,而不是图标。

如果找到文件,但格式不正确,最后的错误消息将为空。

英文:

The image must be an .ico file in some specific format. The one which worked correctly for me was exporting it from GIMP with 'Windows Icon' format selected. The icon was previously in the .ico format, it just wasn't right for some reason. Examples which load it from .bmp and .png did not work for me, that works only for bitmaps, which are not icons.

If the file is found, and the formats are not correct, there won't be anything in last error.

huangapple
  • 本文由 发表于 2023年2月10日 05:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定