怎么才能 MessageBox 显示一个变量的值啊

tanes 2018-03-15 02:09:34
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
double PI=3.14
MessageBox(NULL, TEXT(PI), TEXT("PI"), 0);
return 0;
}

网上有很多说法,都说很简单,用CString 转换一下,众说纷纭, 都不能实现啊。都说这么一下就可行。可是CString怎么声明定义都过不去,与<windows.h>冲突。如果操作,求高手帮忙解决一下。
CString str;
str.Format("%d", S*4/100);
MessageBox(NULL, TEXT(str), TEXT("PI"), 0);
...全文
4192 27 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
吴瀚 2018-03-31
  • 打赏
  • 举报
回复
用Format转成CString MessageBox(str) 或者 AfxMessageBox(str)
starytx 2018-03-31
  • 打赏
  • 举报
回复
不建议用MessageBox来测试变量值,推荐用DebugView这个工具来输出测试信息,在程序中使用OutputDebugString(多字节和Unicode版本分别是OutputDebugStringW和OutputDebugStringA),就可以将信息输出到DebugView中来查看,内容可以定义一个char或者wchar数组,然后sprintf来格式化生成。因为MessageBox会阻塞主程序,有副作用
「已注销」 2018-03-22
  • 打赏
  • 举报
回复
引用 11 楼 tanes 的回复:
我改良了一下, int 型%d ,可以通过。 换成 double 型, %lf 只输出一个f ,%f 也只输出一个f
你这样不行,wsprintf 不支持浮点数。你还是用 _stprintf 吧。包含 <stdio.h>、<stdlib.h>、<tchar.h>。
赵4老师 2018-03-17
  • 打赏
  • 举报
回复
CString::Format void Format( LPCTSTR lpszFormat, ... ); void Format( UINT nFormatID, ... ); Parameters lpszFormat A format-control string. nFormatID The string resource identifier that contains the format-control string. Remarks Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID. The call will fail if the string object itself is offered as a parameter to Format. For example, the following code: CString str = "Some Data"; str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list. will cause unpredictable results. When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, seeprintf in the Run-Time Library Reference.) A null character is appended to the end of the characters written. For more information, seesprintf in the Run-Time Library Reference. CString Overview | Class Members | Hierarchy Chart See Also CString::GetBuffer, CString::FormatV
鄢老 2018-03-17
  • 打赏
  • 举报
回复
刚才代码有误,更新如下: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) { TCHAR szText[20] ={0}; ... _stprintf(szText,L"%d", (int)S*4/100);//这里做了修改 double PI=3.14 TCHAR szPI[20] ={0}; _stprintf(szPI,L"%f", PI);//这里做了修改 ::MessageBox(NULL, szText,szPI[, MB_OK); return 0; }
鄢老 2018-03-17
  • 打赏
  • 举报
回复
楼主: 你的程序是win32程序,可你查找到网上的却是MFC的方法,当然楼主有点疑惑了. 解决办法: #include <windows.h> #include <tchar.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) { TCHAR szText[20] ={0}; ... _stprintf(L"%d", (int)S*4/100); double PI=3.14 TCHAR szPI[20] ={0}; _stprintf(L"%f", PI); ::MessageBox(NULL, szText,szPI[, MB_OK); return 0; } 顺便再温馨提示下:关于字符串操作一定要用TCHAR的方式,因为它兼容多字节和宽字节
mstlq 2018-03-17
  • 打赏
  • 举报
回复 1
double PI = 3.14;
char ch[100];
sprintf(ch, "%lf", PI);
MessageBoxA(NULL, ch, "PI", 0);
记得包含stdio.h 如果编译不通过,提示安全问题,请参考 http://blog.csdn.net/weixin_39449570/article/details/78801573
mstlq 2018-03-17
  • 打赏
  • 举报
回复
如果楼主不想处理宽字符问题,就请用MessageBoxA
include_zhao 2018-03-16
  • 打赏
  • 举报
回复
1 include <ctype> atoi() ● itoa():将整型值转换为字符串。 ● ltoa():将长整型值转换为字符串 2 string. format 另外,如果是易语言的话,到文本(),
schlafenhamster 2018-03-16
  • 打赏
  • 举报
回复
可以 不是 <afxwin.h> 而是 <afx.h>
schlafenhamster 2018-03-16
  • 打赏
  • 举报
回复
double PI=3.1415926; str.Format("%8.7f",PI); 输出: 3.1415926
schlafenhamster 2018-03-16
  • 打赏
  • 举报
回复

// W32Cstring.cpp : Defines the entry point for the application.
//

#include <afxwin.h>         // MFC core and standard components

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	CString str;
	double PI=3.14;
	str.Format("%f",PI);
	MessageBox(NULL, str, TEXT("PI"), 0);
	return 0;
}
1 因为 没有 stdafx.h 所以 “Setting“ 中 ,不要 使用 预编译头 ! 但 “General” 中 要 使用 Use MFC 2 CString 在<afx.h> 中。已包含 <windows.h> 3 所以 不要 #include <windows.h>
paschen 2018-03-15
  • 打赏
  • 举报
回复
还可以用gcvt实现浮点数转字符串
paschen 2018-03-15
  • 打赏
  • 举报
回复
一般不会,检查函数返回值判断原因 http://en.cppreference.com/w/c/io/fprintf 当然你也可以自己实现: http://blog.csdn.net/paschen/article/details/74554955
tanes 2018-03-15
  • 打赏
  • 举报
回复
#include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { WCHAR szBuffer[100]; double number = 3.14; wsprintf(szBuffer, L"%lf", number); MessageBox(NULL, szBuffer, TEXT("格式化字符串"), 0); } 我改良了一下, int 型%d ,可以通过。 换成 double 型, %lf 只输出一个f ,%f 也只输出一个f
tanes 2018-03-15
  • 打赏
  • 举报
回复
引用 9 楼 qq_39850605 的回复:
不一定非要使用cstring 啊 就像你楼上说的,你可以 WCHAR msg[] = L"How are you!"; MessageBox(NULL,msg,L"Hello",0); 想用格式 使用sprintf 或者 sprintfs_s
说白了吧,我要把一个double 型的数字,在message 上显示出来。
@风轻云淡_ 2018-03-15
  • 打赏
  • 举报
回复
不一定非要使用cstring 啊 就像你楼上说的,你可以 WCHAR msg[] = L"How are you!"; MessageBox(NULL,msg,L"Hello",0); 想用格式 使用sprintf 或者 sprintfs_s
paschen 2018-03-15
  • 打赏
  • 举报
回复
引用 6 楼 tanes的回复:
不对啊,提示未定义标识符 "_stprintf" 我新建的不是 MFC工程 ,就是一个普通的win32程序。我是初学者,原来都是玩控制台,现在想移植一些小东西到 MessageBox ,怎么也弄不成功啊。理论上很简单,要把数字转变成字符,并且还得是宽字符。 [quote=引用 4 楼 paschen 的回复:]

	double PI = 3.14;
	TCHAR ch[100];
	_stprintf(ch, TEXT("%lf"), PI);
	MessageBox(NULL, ch, TEXT("PI"), 0);
如果是整数转字符串用%d、%u
[/quote] 包含tchar.h头文件
tanes 2018-03-15
  • 打赏
  • 举报
回复
网上有无数人说 转换成cstring 然后Messagebox 太精简了, CString str; str.Format("%d", PI); MessageBox(NULL, str, TEXT("PI"), 0); 实际当中通不过,CString 未定义,这个 东西定义还挺麻烦的,有一个定义文件,写上,就提示和 windows.h 冲突。 这个代码很简单,就是一个小例子,麻烦高手帮忙帮到家,来个完整的代码。声明定义文件怎么定义啊。
tanes 2018-03-15
  • 打赏
  • 举报
回复
不对啊,提示未定义标识符 "_stprintf" 我新建的不是 MFC工程 ,就是一个普通的win32程序。我是初学者,原来都是玩控制台,现在想移植一些小东西到 MessageBox ,怎么也弄不成功啊。理论上很简单,要把数字转变成字符,并且还得是宽字符。
引用 4 楼 paschen 的回复:

	double PI = 3.14;
	TCHAR ch[100];
	_stprintf(ch, TEXT("%lf"), PI);
	MessageBox(NULL, ch, TEXT("PI"), 0);
如果是整数转字符串用%d、%u
加载更多回复(4)

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧