注册窗口类时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/