本文介绍了如何使用DrawText()来显示一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过 DrawText()绘制字符串文字:

I can draw a string literal via DrawText():

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

但是,这不适用于任何其他操作。具体来说,我不能输出存储在变量中的值,例如 int

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

char

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

如何使用 DrawText()显示变量的内容?为什么使用abc123的字符串文字工作,但用 variable 替换它不会?

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?

推荐答案

DrawText 只知道如何显示字符串。要显示其他任何内容,您需要先转换为字符串,然后显示该字符串。

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

这篇关于如何使用DrawText()来显示一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 21:52