使用“cout”在 Windows 对话框中显示消息 - C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1730427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display message in windows dialogue box using "cout" - C++
提问by CheeseConQueso
Can a windows message box be display using the cout syntax?
可以使用 cout 语法显示 Windows 消息框吗?
I also need the command prompt window to be suppressed / hidden.
我还需要抑制/隐藏命令提示符窗口。
There are ways to call the messagebox function and display text through its usage, but the main constraint here is that cout syntax must be used.
有多种方法可以调用 messagebox 函数并通过其用法显示文本,但这里的主要约束是必须使用 cout 语法。
cout << "message";
I was thinking of invoking the VB msgbox command in the cout output, but couldn't find anything that worked.
我想在 cout 输出中调用 VB msgbox 命令,但找不到任何有用的东西。
Any ideas?
有任何想法吗?
回答by Russell Newquist
C++ streams work with console or file streams. Windows work on a more or less completely different paradigm, so the cout context isn't really a good one for working with this. You could probably completely mash up something that would end up more or less working, and looking more or less similar to this syntax, but it's not really worth it when you can just do:
C++ 流与控制台或文件流一起工作。Windows 工作在或多或少完全不同的范式上,因此 cout 上下文并不是真正适合处理此问题的上下文。您可能可以完全混合一些最终会或多或少起作用的东西,并且看起来或多或少类似于这种语法,但是当您可以这样做时,这并不值得:
MessageBox( NULL, message, "", MB_OK );
See the full docs on MessageBoxfor more info.
有关详细信息,请参阅MessageBox上的完整文档。
回答by fnieto - Fernando Nieto
First thing you should take into account is that MessageBox stops the thread until you close the window. If that is the behavior you desire, go ahead.
您应该考虑的第一件事是 MessageBox 会停止线程,直到您关闭窗口。如果这是您想要的行为,请继续。
You can create a custom streambuf and set it to std::cout
:
您可以创建自定义流缓冲并将其设置为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 shown whenever std::cout stream is flushed.
每当刷新 std::cout 流时,都会显示一个弹出窗口。
回答by Jerry Coffin
When confronted with this in the past, I've used a stringstream
along with a manipulator that displays the current contents of the stringstream
using MessageBox
:
过去遇到这种情况时,我使用了 astringstream
和一个显示stringstream
using的当前内容的操纵器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 a fair amount like using cout
, but with MessageBox
replacing std::endl
. For example:
要使用它,语法看起来很像 using cout
,但MessageBox
替换std::endl
. 例如:
std::ostringstream stm;
stm << " blah blah blah. Value: " << 1213.1231 << MessageBox;
Edit: mostly for fnieto. In this case, the downcast really isnecessary. The reason is fairly simple: a typical inserter receives and returns a reference to an ostream:
编辑:主要用于 fnieto。在这种情况下,沮丧确实是必要的。原因很简单:典型的插入器接收并返回对 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) casts it up to a simple ostream. That's fine in itself, and works fine for most inserters and manipulators, because they only interact with the ostream
interface themselves.
这需要原始的 stringstream 对象并默默地(并且安全地)将其转换为一个简单的 ostream。这本身很好,并且适用于大多数插入器和操纵器,因为它们只与ostream
界面本身交互。
This manipulator, however, is a bit different -- it uses the str()
member, which ostream
doesn't define at all. For our call to str()
to resolve and compile, we have to convert the ostream &
to an ostringstream &
, so the compiler is aware that the object we're working with really will have a str()
member.
然而,这个操纵器有点不同——它使用完全没有定义的str()
成员ostream
。对于我们str()
对解析和编译的调用,我们必须将 转换ostream &
为ostringstream &
,因此编译器知道我们正在使用的对象确实会有一个str()
成员。
To eliminate the downcast, we'd really only have one choice: make its parameter an ostringstream &
. That would work as long as we never chained operators:
为了消除沮丧,我们真的只有一个选择:将其参数设为ostringstream &
。只要我们从不链接运算符,这就会起作用:
my_stream << x;
my_stream << MessageBox;
but trying to chain those would fail:
但试图链接这些会失败:
// should be equivalent:
my_stream << x << MessageBox;
Worse, the compiler's error message will probably try to tell the user something about std::basic_ostream<char>::str()
, which isn't mentioned in the user's code at all. Worse still, most people are sufficiently accustomed to chaining or not giving identical results that it would probably take them a while to even figure out why the code sometimes worked fine, and other times failed to compile, with a completely indecipherable error message.
更糟糕的是,编译器的错误消息可能会试图告诉用户一些关于 的信息std::basic_ostream<char>::str()
,而用户代码中根本没有提到这一点。更糟糕的是,大多数人已经习惯于链接或不给出相同的结果,以至于他们可能需要一段时间才能弄清楚为什么代码有时可以正常工作,而有时却无法编译,并出现完全无法理解的错误消息。
回答by Stephen Newell
By including sstream
, you can use std::ostringstream
and build a message using the iostream library. You can then call .str().c_str()
and get a char *
to pass to MessageBox.
通过包含sstream
,您可以使用std::ostringstream
iostream 库来使用和构建消息。然后.str().c_str()
,您可以调用并获取一个char *
传递给 MessageBox。
回答by Glen
No simple way anyway.
反正没有简单的方法。
The c in cout stands for console, so you're probably out of luck.
cout 中的 c 代表控制台,所以你可能不走运。
If it's just the syntax you're looking to copy, then you could write your own stream class that creates a message box under the hood and displays it.
如果这只是您要复制的语法,那么您可以编写自己的流类,在后台创建一个消息框并显示它。
回答by BostonLogan
You may want to check this out: How can I redirect stdout to some visible display in a Windows Application?
您可能想看看这个: 如何将标准输出重定向到 Windows 应用程序中的某些可见显示?
回答by Max Lybbert
Can a windows message box be display using the cout syntax?
可以使用 cout 语法显示 Windows 消息框吗?
You can't do it with std::cout
. std::cout
doesn't even promise to handle Unicode/wide characters (see std::wcout
), although Windows's cout
has no trouble with wide characters.
你不能用std::cout
. std::cout
甚至不承诺处理 Unicode/宽字符(请参阅 参考资料std::wcout
),尽管 Windowscout
处理宽字符没有问题。
You could easily do it with the same syntax; that is, you could easily write a library that overloads operator<<
to display dialog boxes. Trying to pass all the information to the dialog box that way would be very difficult, though (how would you you say which buttons to show, what those buttons should do when pressed, where those buttons should be, and the size and position of the window itself?).
您可以使用相同的语法轻松完成;也就是说,您可以轻松编写一个重载operator<<
以显示对话框的库。但是,尝试以这种方式将所有信息传递给对话框将非常困难(您如何说要显示哪些按钮,按下这些按钮时应该做什么,这些按钮应该在哪里,以及按钮的大小和位置)窗口本身?)。
You may want to look at something like ncurses. The syntax is different, but I have a feeling it's what your coworker is looking for.
您可能想查看类似ncurses 的内容。语法不同,但我有一种感觉,这就是你的同事正在寻找的。