0

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?

In silico
  • 51,091
  • 10
  • 150
  • 143
  • There are lots of examples of converting an int to a string, and a constructor of `string` that takes a `char` and a size. – chris Jul 08 '13 at 02:40
  • Repost : http://stackoverflow.com/questions/17518784/how-to-use-d-in-c-particularly-in-drawtext?answertab=oldest#tab-top – Pierre Lebon Jul 08 '13 at 03:42

1 Answers1

7

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);
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111