Table of contents
original code
The pseudocode of the loadimage method in easyx.h in Visual Studio 2022:
// Image related functions
void loadimage(IMAGE *pDstImg, LPCTSTR pImgFile, int nWidth = 0, int nHeight = 0, bool bResize = false); // Load image from a file (bmp/gif/jpg/png/tif/emf/wmf/ico)
void loadimage(IMAGE *pDstImg, LPCTSTR pResType, LPCTSTR pResName, int nWidth = 0, int nHeight = 0, bool bResize = false); // Load image from resources (bmp/gif/jpg/png/tif/emf/wmf/ico)
An error occurred
Normally, we would write
IMAGE img;
loadimage(&img, "./resources/abc.png", WIDTH, HEIGHT);
But an error will be reported, so you need to use the _T method for conversion:
IMAGE img;
loadimage(&img, _T("./resources/abc.png"), WIDTH, HEIGHT);
Concatenation of strings also goes wrong
IMAGE img;
loadimage(&img, _T("./resources/"+"abc.png"), WIDTH, HEIGHT);
Including string variables also errors out
IMAGE img;
string p="./resources/";
loadimage(&img, _T(p+"abc.png"), WIDTH, HEIGHT);
Using string variables directly will also cause errors
IMAGE img;
string p="./resources/abc.png";
loadimage(&img, _T(p), WIDTH, HEIGHT);
Solution
Actually LPCTSTR is TCHAR*
The following BUFFERSIZE can modify the value by itself
char* to TCHAR*
#define BUFFERSIZE 1024
TCHAR* Transform(char c[BUFFERSIZE]) {
TCHAR result[BUFFERSIZE];
MultiByteToWideChar(CP_ACP, 0, c, -1, result, BUFFERSIZE);
return result;
}
string to TCHAR*
#define BUFFERSIZE 1024
TCHAR* Transform(string s) {
TCHAR result[BUFFERSIZE];
char c[BUFFERSIZE];
strcpy_s(c, s.c_str());
MultiByteToWideChar(CP_ACP, 0, c, -1, result, BUFFERSIZE);
return result;
}
The main program is changed to:
IMAGE img;
loadimage(&img, Transform(...), WIDTH, HEIGHT); // ...使用char*或string类型即可