我想让这个字符串的主机名部分可变.. 目前,它只修复了这个 URL:
_T(" --url=http://www.myurl.com/ --out=c:\\current.png");
我想做这样的东西,所以URL是可变的..
_T(" --url=http://www." + myurl + "/ --out=c:\\current.png");
更新。以下是我最近的尝试:
CString one = _T(" --url=http://www.");
CString two(url->bstrVal);
CString three = _T("/ --out=c:\\current.png");
CString full = one + two + three;
ShellExecute(0,
_T("open"), // Operation to perform
_T("c:\\IECapt"), // Application name
_T(full),// Additional parameters
0, // Default directory
SW_HIDE);
错误是:错误 1 error C2065: 'Lfull' : undeclared identifier c:\test.cpp
最佳答案
它不起作用,因为 _T()
宏仅适用于常量 字符串文字。 _T()
的定义如下所示:
#ifdef UNICODE
#define _T(str) L##str
#else
#define _T(str) str
由于您显然是在 Unicode 模式下编译,_T(full)
扩展为 Lfull
,这显然不是您想要的。
在您的情况下,只需传入 full
而无需 _T()
宏,因为 CString
将转换运算符定义为 const Unicode 模式下为 wchar_t*
,非 Unicode 模式下为 const char*
。
ShellExecute(0, _T("open"), _T("c:\\IECapt"), full, 0, SW_HIDE);
请注意,标准 C++ 还提供了一个 std::string
类型和一个 std::wstring
类型,它们的功能几乎与 CString
的功能相同,因此字符串操作实际上不需要 MFC。 std::string
不 提供转换运算符,但通过c_str()
提供对底层 C 风格字符串的访问。
关于c++ - 如何在 _T 包装器中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4515310/