从资源图像文件中设置墙纸
我想从资源图像设置背景壁纸。我找到并尝试了此代码,但是 findResource()
找不到任何东西。有人可以帮我吗?
HINSTANCE hInstance = GetModuleHandle(NULL);
std::cout << hInstance << std::endl;
HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(IDB_PNG1), RT_BITMAP);
std::cout << hResInfo<< std::endl;
HGLOBAL hRes = LoadResource(hInstance, hResInfo);
std::cout << hRes << std::endl;
LPVOID memRes = LockResource(hResInfo);
DWORD sizeRes = SizeofResource(hInstance, hResInfo);
std::cout << memRes<< std::endl;
std::cout << sizeRes<< std::endl;
HANDLE hFile = CreateFile("C:\\Users\\Asus\\Desktop\\test.png", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWritten = 0;
WriteFile(hFile, memRes, sizeRes, &dwWritten, NULL);
CloseHandle(hFile);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) "C:\\Users\\Asus\\Desktop\\test.png", SPIF_UPDATEINIFILE);
资源文件
#include "resource.h"
IDI_ICON1 ICON "chrome.ico"
IDB_PNG1 PNG "C:\\Users\\Asus\\Pictures\\jano.png"
resource.h
#define IDI_ICON1 101
#define IDB_PNG1 102
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中有两个错误:
您正在创建
IDB_PNG1
资源为typepng
,但是您是在询问findResource()
找到一个类型为bitmap
的资源。这些类型需要匹配,因此在调用text(“ png”)
的rt_bitmap
中,在调用findresource()
中。另外,在.rc
文件中使用rcdata
而不是png
,然后使用rt_rcdata
而不是rt_bitmap
/“ png”
在findResource()
。您将错误的句柄传递给
lockResource()
。您正在传递hresinfo
由findResource()
返回的,但是您需要传递hres
,该由load> loadsource返回()
。There are two mistakes in your code:
You are creating the
IDB_PNG1
resource as typePNG
, but you are askingFindResource()
to find a resource whose type isBITMAP
instead. The types need to match, so replaceRT_BITMAP
withTEXT("PNG")
in the call toFindResource()
. Alternatively, useRCDATA
instead ofPNG
in the.rc
file, and then useRT_RCDATA
instead ofRT_BITMAP
/"PNG"
in the call toFindResource()
.you are passing the wrong handle to
LockResource()
. You are passing inhResInfo
that is returned byFindResource()
, but you need to instead pass inhRes
that is returned byLoadResource()
.loadResource 返回。
带有语句:
IDR_PNG1 rcdata {“ pngwing.png”}
,loadResource 返回 pngwing.png 而 lockResource 在内存中检索指定 Resource>的指针。
RCDATA resource stores integers or strings of characters And the data is returned by LoadResource.
With the statement:
IDR_PNG1 RCDATA {"pngwing.png"}
,LoadResource returns pngwing.png while LockResource retrieves a pointer to the specified resource in memory.