使用“cout”在 Windows 对话框中显示消息- C++

发布于 2024-08-11 02:34:57 字数 247 浏览 3 评论 0原文

可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧人哭 2024-08-18 02:34:57

C++ 流与控制台或文件流一起使用。 Windows 在或多或少完全不同的范例上工作,因此 cout 上下文并不是真正适合处理此问题的环境。您可能可以完全混搭一些最终或多或少可以工作的东西,并且看起来或多或少与此语法相似,但是当您可以这样做时,这并不值得:

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

请参阅 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:

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

See the full docs on MessageBox for more info.

下壹個目標 2024-08-18 02:34:57

您应该考虑的第一件事是 MessageBox 会停止线程,直到您关闭窗口。如果这是您想要的行为,请继续。

您可以创建一个自定义streambuf并将其设置为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
}

每当刷新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:

#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.

始于初秋 2024-08-18 02:34:57

通过包含 sstream,您可以使用 std::ostringstream 并使用 iostream 库构建消息。然后,您可以调用 .str().c_str() 并获取 char * 传递给 MessageBox。

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.

拔了角的鹿 2024-08-18 02:34:57

过去遇到这个问题时,我使用了 stringstream 以及一个操纵器,该操纵器使用 MessageBox 显示 stringstream 的当前内容:

#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;
}

要使用它,语法看起来很像使用 cout,但用 MessageBox 替换 std::endl。例如:

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

编辑:主要用于 fnieto。在这种情况下,沮丧确实是必要的。原因相当简单:典型的插入器接收并返回对 ostream 的引用:

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

这将获取原始 stringstream 对象并静默(且安全)地将其转换为简单的 ostream。这本身就很好,并且对于大多数插入器和操纵器来说都可以正常工作,因为它们本身只与 ostream 接口交互。

然而,这个操纵器有点不同——它使用 str() 成员,而 ostream 根本没有定义该成员。为了调用 str() 进行解析和编译,我们必须将 ostream & 转换为 ostringstream &,因此编译器知道我们正在使用的对象确实会有一个 str() 成员。

为了消除这种沮丧,我们实际上只有一个选择:将其参数设为 ostringstream &。只要我们从不链接运算符,这就会起作用:

my_stream << x;
my_stream << MessageBox;

但尝试链接这些运算符会失败:

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

更糟糕的是,编译器的错误消息可能会尝试告诉用户有关 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 the 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:

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

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:

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.

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.

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:

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.

囚我心虐我身 2024-08-18 02:34:57

无论如何,没有简单的方法。

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.

浅黛梨妆こ 2024-08-18 02:34:57

可以使用 cout 语法显示 Windows 消息框吗?

您无法使用 std::cout 来做到这一点。 std::cout 甚至不承诺处理 Unicode/宽字符(请参阅 std::wcout),尽管 Windows 的 cout 没有任何问题具有宽字符。

您可以使用相同的语法轻松完成此操作;也就是说,您可以轻松编写一个重载 operator<< 的库来显示对话框。但是,尝试以这种方式将所有信息传递到对话框将非常困难(您怎么说要显示哪些按钮,按下这些按钮时应该做什么,这些按钮应该在哪里,以及按钮的大小和位置)窗口本身?)。

您可能想查看类似 ncurses 的内容。语法不同,但我有一个
感觉这就是您的同事正在寻找的东西。

Can a windows message box be display using the cout syntax?

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文