Display message in dialog box using "cout" - C ++

Can a Windows message box be displayed using cout syntax?

I also need the command line window to be disabled / hidden.

There are ways to call a mailbox function and display text through its use, but the main limitation here is that the cout syntax should be used.

cout << "message"; 

I was thinking of calling the VB msgbox command in the output of cout, but could not find anything that worked.

Any ideas?

+4
source share
7 answers

First, you must bear in mind that the MessageBox stops the thread until you close the window. If this is the behavior you desire, continue.

You can create a custom streambuf and set it to std::cout :

 #include <windows.h> #include <sstream> #include <iostream> namespace { class mb_streambuf : public std::stringbuf { virtual ~mb_streambuf() { if (str().size() > 0) sync(); } virtual int sync() { MessageBoxA(0, str().c_str(), "", MB_OK); str(""); return 0; } } mb_buf; struct static_initializer { static_initializer() { std::cout.rdbuf(&mb_buf); } } cout_buffer_switch; } int main() { std::cout << "Hello \nworld!"; // Will show a popup } 

A popup will be displayed whenever the std :: cout stream is cleared.

+9
source

C ++ streams work with console or file streams. Windows is working on a more or less completely different paradigm, so the cout context is not very good at working with this. You may be completely confused about something that will ultimately work more or less, and looks more or less similar to this syntax, but it is not worth what you can just do:

 MessageBox( NULL, message, "", MB_OK ); 

See the MessageBox docs for more details.

+10
source

By enabling sstream , you can use std::ostringstream and create a message using the iostream library. Then you can call .str().c_str() and get char * to go to the MessageBox.

+6
source

When I came across this before, I used stringstream along with a manipulator that displays the current contents of stringstream using a MessageBox :

 #include <windows.h> #include <sstream> #include <ostream> std::ostream &MessageBox(std::ostream &s) { std::ostringstream *st = dynamic_cast<std::ostringstream *>(&s); if (NULL != st) ::MessageBox(NULL, st->str().c_str(), "", MB_OK); return s; } 

To use this, the syntax looks fair, like using cout , but with the replacement MessageBox std::endl . For instance:

 std::ostringstream stm; stm << " blah blah blah. Value: " << 1213.1231 << MessageBox; 

Edit: mainly for fnieto. In this case, a downgrade is actually necessary. The reason is quite simple: a typical pastor receives and returns a link to ostream:

 std::ostream &operator<<(std::ostream &os, T const &t) { // code here to insert t into os, then return os; } 

This takes the original stringstream object and silently (and safely) passes it to a simple ostream. This is fine in itself and great for most plugins and manipulators because they only interact with the ostream interface.

This manipulator, however, is slightly different - it uses the str() element, which ostream does not define at all. For our call to str() to solve and compile, we need to convert ostream & to ostringstream & , so the compiler knows that the object we are working with will actually have the str() member.

To exclude suppression, we will have only one choice: make its parameter ostringstream & . This will work until we never bind the operators:

 my_stream << x; my_stream << MessageBox; 

but trying to mate them will not:

 // should be equivalent: my_stream << x << MessageBox; 

Worse, a compiler error message will probably try to tell the user about std::basic_ostream<char>::str() , which is not mentioned at all in the user code. Worse, most people are sufficiently accustomed to the chain or do not give the same results, which will probably take some time to even figure out why the code sometimes worked fine, and in other cases it was not possible to compile with a completely illegible error message.

+6
source

In any case, there is no easy way.

C cout means console, so you're probably out of luck.

If this is just the syntax you want to copy, you can write your own stream class that creates the message box under the hood and displays it.

+3
source

Can a Windows message box be displayed using cout syntax?

You cannot do this with std::cout . std::cout does not even promise to handle Unicode / wide characters (see std::wcout ), although Windows cout has no problems with wide characters.

You can easily do this with the same syntax; that is, you can easily write a library that overloads operator<< to display dialog boxes. Trying to transfer all the information to the dialog box in this way will be very difficult, though (how would you say which buttons show what these buttons should do when clicked, where should these buttons be, and also the size and position of the window itself?).

You might want to look at something like ncurses . The syntax is different, but I have a feeling this is what your colleague is looking for.

+1
source

All Articles