c++ - 如何在 const wchar_t* 参数处进行串联?

标签 c++ string gdi+

在这种情况下,如何在 const wchar_t* 参数处进行连接?

我正在尝试制作自动保存的屏幕截图,名称如下:

screen-1.jpg
screen-2.jpg
screen-3.jpg
...
screen-i.jpg`

代码:

p_bmp->Save(L"C:/Users/PCUSER/AppData/screen-" + filenumber + ".jpg", &pngClsid, NULL);
 //filenumber is ant int that increases automatically

但是它给我一个错误:

expression must have integral or unscoped

最佳答案

原始 C 风格字符串指针(如 const wchar_t*)不能使用 operator+ 与字符串语义连接在一起。但是,您可以连接 C++ 字符串类 的实例,例如 ATL CStringstd::wstring,仅举几例。

因为您还有 integer 值要连接,您可以先将它们转换为字符串对象(例如使用 std::to_wstring()),然后使用重载operator+ 连接各种字符串。

#include <string> // for std::wstring and to_wstring()
...

// Build the file name string using the std::wstring class
std::wstring filename = L"C:/Users/PCUSER/AppData/screen-";
filename += std::to_wstring(filenumber); // from integer to wstring
filename += L".jpg";

p_bmp->Save(filename.c_str(), // convert from wstring to const wchar_t*
            &pngClsid, 
            NULL);

如果您使用 ATL CString 类,您可以采用的另一种方法是以类似于 printf() 的方式格式化结果字符串,调用 CString: :Format() 方法,例如:

CStringW filename;
filename.Format(L"C:/Users/PCUSER/AppData/screen-%d.jpg", filenumber);

p_bmp->Save(filename, // implicit conversion from CStringW to const wchar_t*
            &pngClsid, 
            NULL);

关于c++ - 如何在 const wchar_t* 参数处进行串联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326980/

相关文章:

C++ 多维字符串数组初始化 (std::map)

c++ - 在存在不可预测的类型别名的情况下如何处理显式模板实例化?

c# - 将数据发送到 C++ Visual Studio 中的 tcp/端口

java - 带双引号的列表项

c++ - memorystream - stringstream,字符串,其他?

c# - 对标记为 ASCII 的 EXIF 属性使用 UTF8 解码是否安全?

c++ - 使用 stringstream 打印四舍五入的 float

c++ - 谁能帮我让 glutBitmapString 工作?

c++ - gdiplus 从字符串构造图像

c# - GraphicsPath.Flatten() 对绘图性能有何影响?