简体   繁体   English

const char* 到 WCHAR?

[英]const char* to WCHAR?

hi I need a WCHAR or some other LPCWSTR for the textout function.嗨,我需要一个 WCHAR 或其他一些 LPCWSTR 用于文本输出 function。 I have another function that outputs a string:我有另一个 function 输出字符串:

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 just returns a string: parseFilestr 只返回一个字符串:

string parseFilestr(int start, bool tweak) 
{

//lots of omitted code here
string info_Str = "hello";
return info_Str;//return the requested data
}

my header file looks like this:我的 header 文件如下所示:

#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);

The execution looks like this:执行看起来像这样:

#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;
}

The stupid question is, how do I change var to a type that will work with TextOut?愚蠢的问题是,如何将 var 更改为可以与 TextOut 一起使用的类型? Or maybe there is an alternative to textout?或者也许有文本输出的替代方法? Ive been busting my head over this and can't seem to figure it out.我一直在努力解决这个问题,似乎无法弄清楚。 Thanks谢谢

Use TextOutA instead.请改用TextOutA It takes an ANSI string (a [const] char * ).它需要一个 ANSI 字符串(一个[const] char * )。

That said, read up on Unicode and the options that you have for widechar/char interaction.也就是说,请阅读 Unicode 以及用于宽字符/字符交互的选项。 In the long run, the whole program should probably be written with widechars, except for the bits where you interact with multibyte filename and wire formats.从长远来看,整个程序可能应该用宽字符编写,除了与多字节文件名和有线格式交互的位。

Try this:尝试这个:

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);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM