How can I use DrawText() to display a variable?

匆匆过客 提交于 2019-12-12 01:39:37

问题


I can draw a string literal via DrawText():

DrawText (hdcWindow, "abc123", -1, &rc, DT_SINGLELINE);

However, this doesn't work with anything else. Specifically, I can't output the value stored in a variable, such as an int:

int variable = 5;
DrawText (hdcWindow, variable, -1, &rc, DT_SINGLELINE);

Or a char:

char variable = a;
DrawText (hdcWindow, variable, -1, &rc, DT_SINGLELINE);

How can I use DrawText() to display the contents of a variable? Why does using a string literal like "abc123" work but substituting it with variable doesn't?


回答1:


DrawText only knows how to display character strings. To display anything else, you need to convert to a character string first, then display that.

void show_int(int x, /* ... */) { 
     std::stringstream buffer;
     buffer << x;

     DrawText(hdcWindow, buffer.str().c_str(), -1, &rc, DT_SINGLELINE);
}


来源:https://stackoverflow.com/questions/17518471/how-can-i-use-drawtext-to-display-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!