C++ 在 MessageBox 中显示 int 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20433682/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:07:45  来源:igfitidea点击:

Display an int variable in a MessageBox

c++winapi

提问by user3076025

I am working on an old app written in Visual C++ 6.0. I am trying to display an intvariable in a MessageBoxfor debugging reasons. Here is my code, I thought this would be a simple process, but I am just learning C++. The two lines that are commented I have tried as well with similar errors. Below is the error I am getting.

我正在开发一个用 Visual C++ 6.0 编写的旧应用程序。出于调试原因,我试图int在 a 中显示一个变量MessageBox。这是我的代码,我认为这将是一个简单的过程,但我只是在学习 C++。评论的两行我也尝试过类似的错误。下面是我得到的错误。

int index1 = 1;
char test1 = index1;
// char var1[] = index1;
// char *varGo1 = index1;
MessageBox(NULL, test1, "testx", MB_OK);

error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'char' to 'const char *'

错误 C2664:“MessageBoxA”:无法将参数 2 从“char”转换为“const char *”

回答by Ivan Aksamentov - Drop

Why bother with C-style strings if you tagged C++?

如果您标记了 C++,为什么还要打扰 C 风格的字符串?

Although Mark Ransomprovided MFC solution(which is perfectly valid), here is a Standard C++ one:

尽管Mark Ransom提供了MFC 解决方案(这是完全有效的),但这里是标准 C++解决方案

int index1 = 1;
std::string test1 = std::to_string(index1);
MessageBoxA(NULL, test1.c_str(), "testx", MB_OK);

References:

参考:

Use boost::formatfor more sophisticated formatting.

使用boost::format更复杂的格式。

回答by Mark Ransom

CString str1;
str1.Format(_T("%d"), index1);
MessageBox(NULL, str1, "testx", MB_OK);

CString's Formatworks just like printfto populate the string with the parameter list.

CString 的Format工作就像printf用参数列表填充字符串一样。

回答by Oleh

int index1 = 1;
char buf[10];
itoa(index1,buf,10);
MessageBox(NULL,buf,"Caption",MB_OK);

Can try this

可以试试这个

回答by Bathsheba

The second parameter of MessageBoxneeds to be a pointer to a stringof chars, terminated with NULL. Passing a charwill not work.

的第二个参数MessageBox需要是一个指向s字符串的指针char,以 NULL 结尾。通过 achar将不起作用。

But, learning to use a debugger is an integral part to learning a language. Why not build a debug build and set a breakpoint on char test1 = index1;instead? You do that by pressing F9 when the cursor is on that line.

但是,学习使用调试器是学习语言不可或缺的一部分。为什么不构建调试版本并设置断点char test1 = index1;呢?当光标位于该行上时,您可以通过按 F9 来完成此操作。

回答by Jerry Coffin

For what it's worth, I prefer to use a manipulator for this:

对于它的价值,我更喜欢为此使用操纵器:

#include <sstream>
#include <iostream>
#include <windows.h>

using std::ostringstream;
using std::ostream;

ostream &msg_box(ostream &s) {
    ostringstream &os = dynamic_cast<ostringstream &>(s);

    MessageBox(NULL, os.str().c_str(), "testx", MB_OK);
    return s;
}

int main() { 
    ostringstream msg;
    msg << "The number is: " << 10 << msg_box;
    return 0;
}

This maintains (mostly) the same interface nearly everybody's already accustomed to with iostreams, avoids the type-unsafe CString::Format, and avoids having several lines of distraction everywhere you're going to display a little information for debugging. The other obvious good point is that if you've overloaded operator<<for your own type, that overload will work with this as well.

这保持了(大部分)相同的界面,几乎每个人都已经习惯了iostreams,避免了类型不安全CString::Format,并避免了在你要显示一些调试信息的任何地方都有几行分心。另一个明显的好处是,如果您operator<<为自己的类型进行了重载,那么该重载也将适用于此。

回答by HaSeeB MiR

Here is the pure Csolution using sprintfmethod to store all input in buffer and passing that buffer to MessageBox.

这是使用sprintf方法将所有输入存储在缓冲区中并将该缓冲区传递给MessageBox的纯C解决方案。

#include <stdio.h>
#include <windows.h>

int main(void)
{
    int intVal = 50;
    float fltVal = 5.5;
    char *str = "Test String";
    char buf[1024] = {'
const char * test1= new char();
'};//buffer to store formatted input. //convert formatted input into buffer. sprintf(buf,"Int value : %d\nFloat value : %f\nString : %s\n",intVal,fltVal,str); //display whole buffer. MessageBox(NULL,buf,"INFO",MB_ICONINFORMATION); return 0; }

回答by Hymanzawa

Acording to your error, you should declare a const pointer on the second parameter. Like this,

根据您的错误,您应该在第二个参数上声明一个 const 指针。像这样,

std::string test1= "";
MessageBox(NULL, test1.c_str(), "testx", MB_OK);

or use

或使用

##代码##

Also using just "Text" will work.

也可以只使用“文本”。