从资源图像文件中设置墙纸

发布于 2025-01-28 02:12:53 字数 1352 浏览 4 评论 0 原文

我想从资源图像设置背景壁纸。我找到并尝试了此代码,但是 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

I would like to set the background wallpaper from a resource image. I found and tried this code, but FindResource() can't find anything. Can anybody help me please?

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);

resource file

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳根太软 2025-02-04 02:12:53

您的代码中有两个错误:

  • 您正在创建 IDB_PNG1 资源为type png ,但是您是在询问 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 type PNG, but you are asking FindResource() to find a resource whose type is BITMAP instead. The types need to match, so replace RT_BITMAP with TEXT("PNG") in the call to FindResource(). Alternatively, use RCDATA instead of PNG in the .rc file, and then use RT_RCDATA instead of RT_BITMAP/"PNG" in the call to FindResource().

  • you are passing the wrong handle to LockResource(). You are passing in hResInfo that is returned by FindResource(), but you need to instead pass in hRes that is returned by LoadResource().

朕就是辣么酷 2025-02-04 02:12:53

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文