使用“cout”在 Windows 对话框中显示消息- C++
可以使用 cout 语法显示 Windows 消息框吗?
我还需要抑制/隐藏命令提示符窗口。
有多种方法可以调用 messagebox 函数并通过其使用来显示文本,但这里的主要限制是必须使用 cout 语法。
cout << "message";
我正在考虑在 cout 输出中调用 VB msgbox 命令,但找不到任何有效的方法。
有什么想法吗?
Can a windows message box be display using the cout syntax?
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.
cout << "message";
I was thinking of invoking the VB msgbox command in the cout output, but couldn't find anything that worked.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C++ 流与控制台或文件流一起使用。 Windows 在或多或少完全不同的范例上工作,因此 cout 上下文并不是真正适合处理此问题的环境。您可能可以完全混搭一些最终或多或少可以工作的东西,并且看起来或多或少与此语法相似,但是当您可以这样做时,这并不值得:
请参阅 MessageBox 了解更多信息。
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:
See the full docs on MessageBox for more info.
您应该考虑的第一件事是 MessageBox 会停止线程,直到您关闭窗口。如果这是您想要的行为,请继续。
您可以创建一个自定义streambuf并将其设置为
std::cout
:每当刷新std::cout流时都会显示一个弹出窗口。
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.
You can create a custom streambuf and set it to
std::cout
:A popup will be shown whenever std::cout stream is flushed.
通过包含
sstream
,您可以使用std::ostringstream
并使用 iostream 库构建消息。然后,您可以调用.str().c_str()
并获取char *
传递给 MessageBox。By including
sstream
, you can usestd::ostringstream
and build a message using the iostream library. You can then call.str().c_str()
and get achar *
to pass to MessageBox.过去遇到这个问题时,我使用了
stringstream
以及一个操纵器,该操纵器使用MessageBox
显示stringstream
的当前内容:要使用它,语法看起来很像使用
cout
,但用MessageBox
替换std::endl
。例如:编辑:主要用于 fnieto。在这种情况下,沮丧确实是必要的。原因相当简单:典型的插入器接收并返回对 ostream 的引用:
这将获取原始 stringstream 对象并静默(且安全)地将其转换为简单的 ostream。这本身就很好,并且对于大多数插入器和操纵器来说都可以正常工作,因为它们本身只与
ostream
接口交互。然而,这个操纵器有点不同——它使用
str()
成员,而ostream
根本没有定义该成员。为了调用str()
进行解析和编译,我们必须将ostream &
转换为ostringstream &
,因此编译器知道我们正在使用的对象确实会有一个str()
成员。为了消除这种沮丧,我们实际上只有一个选择:将其参数设为
ostringstream &
。只要我们从不链接运算符,这就会起作用:但尝试链接这些运算符会失败:
更糟糕的是,编译器的错误消息可能会尝试告诉用户有关
std::basic_ostream::str()< /code>,用户代码中根本没有提及。更糟糕的是,大多数人已经足够习惯于链接或不给出相同的结果,以至于他们可能需要一段时间才能弄清楚为什么代码有时工作正常,而有时却无法编译,并出现完全无法解读的错误消息。
When confronted with this in the past, I've used a
stringstream
along with a manipulator that displays the current contents of thestringstream
usingMessageBox
:To use this, the syntax looks a fair amount like using
cout
, but withMessageBox
replacingstd::endl
. For example:Edit: mostly for fnieto. In this case, the downcast really is necessary. The reason is fairly simple: a typical inserter receives and returns a reference to an ostream:
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.This manipulator, however, is a bit different -- it uses the
str()
member, whichostream
doesn't define at all. For our call tostr()
to resolve and compile, we have to convert theostream &
to anostringstream &
, so the compiler is aware that the object we're working with really will have astr()
member.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:but trying to chain those would fail:
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.无论如何,没有简单的方法。
cout 中的 c 代表控制台,所以您可能不走运。
如果这只是您要复制的语法,那么您可以编写自己的流类,在后台创建一个消息框并显示它。
No simple way anyway.
The c in cout stands for console, so you're probably out of luck.
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.
您可能想检查一下:
如何将 stdout 重定向到Windows 应用程序中的一些可见显示?
You may want to check this out:
How can I redirect stdout to some visible display in a Windows Application?
您无法使用
std::cout
来做到这一点。std::cout
甚至不承诺处理 Unicode/宽字符(请参阅std::wcout
),尽管 Windows 的cout
没有任何问题具有宽字符。您可以使用相同的语法轻松完成此操作;也就是说,您可以轻松编写一个重载
operator<<
的库来显示对话框。但是,尝试以这种方式将所有信息传递到对话框将非常困难(您怎么说要显示哪些按钮,按下这些按钮时应该做什么,这些按钮应该在哪里,以及按钮的大小和位置)窗口本身?)。您可能想查看类似 ncurses 的内容。语法不同,但我有一个
感觉这就是您的同事正在寻找的东西。
You can't do it with
std::cout
.std::cout
doesn't even promise to handle Unicode/wide characters (seestd::wcout
), although Windows'scout
has no trouble with wide characters.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?).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.