问题描述
使用MFC创建一个exe,该exe中使用了一张bmp作为背景图。后期需要根据应用修改背景图而不重新编译exe。
问题处理
// Test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//In my resource file of the WINAPI PROGRAM:
//IDI_ICON ICON "Resources/Icon.ico"
//IDB_BACKGROUND BITMAP DISCARDABLE "Resources/1.bmp"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdint.h>
using namespace std;
bool Update(int ResourceID, std::string ModulePath, string FilePath)
{
/*BeginUpdateResourceA(
__in LPCSTR pFileName,
__in BOOL bDeleteExistingResources
);*/
HANDLE hResource = BeginUpdateResource(ModulePath.c_str(), /*false*/false); // true:则删除所有资源
if (hResource != nullptr)
{
std::fstream File(FilePath.c_str(), std::ios::in | std::ios::binary);
if (File.is_open())
{
File.seekg(0, std::ios::end);
std::size_t FileSize = File.tellg();
File.seekg(0, std::ios::beg);
std::vector<uint8_t> Data(FileSize); //Also used a pointer.. makes no difference..
File.read(reinterpret_cast<char*>(Data.data()), FileSize);
File.close();
/*if (UpdateResource(hResource, RT_BITMAP, MAKEINTRESOURCE(ResourceID), MAKELANGID(0x0409, 0x1), Data.data(), FileSize))
{
EndUpdateResource(hResource, false);
return true;
}*/
//SetThreadUILanguage(MAKELANGID(LANG_ENGLISH, SUBLANG_CHINESE_SIMPLIFIED)); // [英语(美国)
if (UpdateResource(hResource, RT_BITMAP, MAKEINTRESOURCE(ResourceID), MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), Data.data() + sizeof(BITMAPFILEHEADER), FileSize - sizeof(BITMAPFILEHEADER))) //Notice the sizeof.
{
EndUpdateResource(hResource, false);
return true;
}
}
}
return false;
}
int main()
{
if (Update(130, "MfcWindow.exe", "res/2.bmp"))
{
std::cout<<"Updated Successfully";
}
else
{
std::cout<<"Failed To Update";
}
return 0;
}