KingRaider
KingRaider

Reputation: 19

How to display variables + text in Messagebox? Winapi32 C++

Alright so with CMD (Iostream.h) you can use << >> to pass data along. Like if I wanted to show text and a variable with text I would say.

cout << "You have " << numberofApples << " of apples.";

How can I display text and a variable into my messagebox / SetWindowText, etc. I did search around on google but I don't exactly know what you call it so I couldn't find any clean cut answers.

Thanks!

Upvotes: 0

Views: 2266

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283911

There's a std::stringstream class that allows you to make a stream similar to std::cout, but which places the formatted output into a string so you can do things with it (display in messagebox, send across network, etc)

For example, using your code with a message box would look like

#include <sstream>

#include <windows.h>

...

std::stringstream box_message;
box_message << "You have " << numberofApples << " of apples.";
MessageBoxA(0, box_message.str().c_str(), "My Message Box", MB_OK);

There's also std::wstringstream which can be used with Unicode (UCS-2) to display Eastern languages (and use MessageBoxW)

Upvotes: 2

Related Questions