8

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

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

4

1 回答 1

16

您可以使用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 标志

于 2012-11-02T00:57:04.157 回答