【发布时间】:2013-06-14 17:52:14
【问题描述】:
我需要从LPTSTR 转换为LPCWSTR。
我需要这个,因为我从GetDlgItemText 得到LPTSTR,我将把它提供给ExtTextOut,它接受LPCWSTR。
编辑:
在从GetDlgItemText 传递值之前,我将值存储在std::vector 中。之后我从std::vector 检索值,它返回给我一个空/胡言乱语。
对话框:
WORD lineLength = (WORD) SendDlgItemMessage(hwnd,IDC_EDIT1, EM_LINELENGTH, (WPARAM) 0, (LPARAM) 0);
if(lineLength > 0){
TCHAR line[16];
int number = GetDlgItemTextW(hwnd, IDC_EDIT1, line, 16);
HWND parent = (HWND)GetWindowLongPtr(hwnd, GWLP_HWNDPARENT);
LPCWSTR line2(line);
SendMessage(parent, WM_COMMAND, MAKEWPARAM(ADD_COMBO_ITEM,0), (LPARAM)line);
它向父窗口发送一条消息,父窗口将此值添加到向量 (push_back)。
带有父窗口的类:
std::vector<LPCWSTR> comboItems
这是我使用 ExtTextOut 输出值的函数的一部分:
RECT temp;
temp.left = listItemWidth;
temp.right = width;
SetBkColor(hdc, RGB(240,240,260));
LPCWSTR comboName = L"";
for(std::vector<item>::size_type i=0; i != comboItems.size(); i++){
temp.left = listItemWidth;
temp.right = width;
temp.top = (currentlyClicked + 1) * listItemHeight + i * listItemHeight;
temp.bottom = temp.top + listItemHeight;
comboName = comboItems[i];
ExtTextOut(hdc, temp.left+2, temp.top + 1, ETO_OPAQUE,
&temp, comboName, lstrlen(comboName), 0);
DrawEdge(hdc, &temp, EDGE_RAISED, BF_RECT | BF_FLAT | BF_ADJUST);
}
【问题讨论】:
-
仔细查看 ExtTextOut 的 MSDN Library article。并告诉我们它在哪里说它接受 LPCWSTR。您不必转换。
-
我遇到了问题,在从 EditBox(GetDltItemText) 传递值之前,我将值存储在 std::vector 中,然后在使用 ExtTextOut 时,我从向量中调用该值。跨度>
-
std::vector 不是一个很好的字符串类型。使用 std::wstring。
-
std::vector创建一个可变数组,我可以用我想要的任何数据填充它。而std::wstring是一个宽字符字符串。 -
除非您要填充的数据是字符数据。我们对这种类型的数组有一个特殊的名称,称为字符串。你应该仍然使用 std::wstring。
标签: c++ visual-studio-2010 winapi msdn