c++ - 动态加载窗口图标

标签 c++ winapi icons

注册窗口类时WNDCLASSEX wcex , 我用 wcex.hIcon = LoadIcon( hInstance, (LPCTSTR) IDI_APPLICATION )设置窗口的图标。

有没有办法从文件中动态加载图标来注册窗口? 类似于 LoadIcon ( hInstance, "iconfile.ico" )或者可以使用该文件创建图标资源。

最佳答案

您可以使用 LoadImage :

wcex.hIcon = (HICON) LoadImage( // returns a HANDLE so we have to cast to HICON
  NULL,             // hInstance must be NULL when loading from a file
  "iconfile.ico",   // the icon file name
  IMAGE_ICON,       // specifies that the file is an icon
  0,                // width of the image (we'll specify default later on)
  0,                // height of the image
  LR_LOADFROMFILE|  // we want to load a file (as opposed to a resource)
  LR_DEFAULTSIZE|   // default metrics based on the type (IMAGE_ICON, 32x32)
  LR_SHARED         // let the system release the handle when it's no longer used
);

确保将 wcex.hIconSm(小图标)设置为 NULL 或加载小图标。当你将它设置为 NULL 时,它会自动使用 hIcon 指定的图像。使用 LoadImage 加载小图标时,应将宽度和高度设置为 16,并移除 LR_DEFAULTSIZE 标志。如果是设计有透明部分的图标,添加LR_LOADTRANSPARENT标志

关于c++ - 动态加载窗口图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13187453/

相关文章:

powershell - 如何以编程方式设置 Windows 10 移动热点?

java - GWT favicon.ico 返回错误!(GWT 和 Java)

c++ - 矩阵算法任务

c++ - 在 C++ 中显示访问冲突的错误

vector 的 C++ vector ,无法编辑

linux - Qt5 linux如何设置窗口图标图片

javascript - 对于我的可点击下拉菜单,图标内的区域不可点击

c++ - 删除 Mat int 函数 (opencv)

winapi - 如何打开 "nul"文件?

Python 的 win32api 只打印到默认打印机