嗨,我需要一个WCHAR或其他一些LPCWSTR的文本功能。我有另一个输出字符串的函数:
string run_TypeSt(char op_Sel) {
switch (op_Sel) {
case 'm'://model
{
searchv_Str = "boatsmodel_s";
return parseFilestr(42, true);
}
case 't'://type (this is going to be really tricky)
{
}
case 'i'://make
{
searchv_Str = "boatsmake_s";
}
}parseFilestr只返回一个字符串:
string parseFilestr(int start, bool tweak)
{
//lots of omitted code here
string info_Str = "hello";
return info_Str;//return the requested data
}我的头文件如下所示:
#pragma once
#include <string>
int parseFilenum(int start, bool tweak);
int run_Type(char op_Sel);
std::string parseFilestr(int start, bool tweak);
std::string run_TypeSt(char op_Sel);执行过程如下:
#include <string>
using namespace std;
string var;
int APIENTRY wWinMain(...)
{
var = run_TypeSt('m');
}
LRESULT CALLBACK WndProc(...)
{
case WM_PAINT:
{
TextOut(hdc, 5, 5, var, 6);// issue here, var is not compatible with TextOut. Solutions?
break;
}最愚蠢的问题是,如何将var更改为使用TextOut的类型?或者除了发短信之外还有其他的选择?我一直为这件事绞尽脑汁,似乎弄不明白。谢谢
发布于 2022-09-14 18:21:49
使用TextOutA代替。它需要一个ANSI字符串(一个[const] char *)。
尽管如此,请阅读Unicode和widechar/char交互的选项。从长远来看,整个程序可能应该使用widechars编写,但与多字节(或单字节)文件和有线格式交互的位除外。
发布于 2022-09-15 00:38:50
试试这个:
std::string FromWideString(const std::wstring& WIDESTRING)
{
std::string result;
size_t widelength = WIDESTRING.length();
char* temp = new char[widelength+2];
temp[widelength+1]=NULL;
size_t res = wcstombs(temp, WIDESTRING.c_str(), widelength+1);
if (res != (size_t) -1)
result=temp;
delete temp;
return(result);
}https://stackoverflow.com/questions/73721366
复制相似问题