#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);
uj5u.com熱心網友回復:
提示什么錯誤?上圖uj5u.com熱心網友回復:
要把整數或浮點數轉換成字串再顯示uj5u.com熱心網友回復:
double PI = 3.14;
TCHAR ch[100];
_stprintf(ch, TEXT("%lf"), PI);
MessageBox(NULL, ch, TEXT("PI"), 0);
如果是整數轉字串用%d、%u
uj5u.com熱心網友回復:
咦,我的3 樓呢,,如果你新建的不是MFC工程,而是win32控制臺應用程式, 在使用messageBOX的時候可能需要加一個宏 ‘L’uj5u.com熱心網友回復:
不對啊,提示未定義識別符號 "_stprintf"我新建的不是 MFC工程 ,就是一個普通的win32程式。我是初學者,原來都是玩控制臺,現在想移植一些小東西到 MessageBox ,怎么也弄不成功啊。理論上很簡單,要把數字轉變成字符,并且還得是寬字符。
uj5u.com熱心網友回復:
網上有無數人說 轉換成cstring 然后Messagebox太精簡了,
CString str;
str.Format("%d", PI);
MessageBox(NULL, str, TEXT("PI"), 0); 實際當中通不過,CString 未定義,這個 東西定義還挺麻煩的,有一個定義檔案,寫上,就提示和 windows.h 沖突。 這個代碼很簡單,就是一個小例子,麻煩高手幫忙幫到家,來個完整的代碼。宣告定義檔案怎么定義啊。
uj5u.com熱心網友回復:
包含tchar.h頭檔案
uj5u.com熱心網友回復:
不一定非要使用cstring 啊 就像你樓上說的,你可以WCHAR msg[] = L"How are you!";
MessageBox(NULL,msg,L"Hello",0);
想用格式 使用sprintf 或者 sprintfs_s
uj5u.com熱心網友回復:
不一定非要使用cstring 啊 就像你樓上說的,你可以
WCHAR msg[] = L"How are you!";
MessageBox(NULL,msg,L"Hello",0);
想用格式 使用sprintf 或者 sprintfs_s
說白了吧,我要把一個double 型的數字,在message 上顯示出來。
uj5u.com熱心網友回復:
#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
uj5u.com熱心網友回復:
一般不會,檢查函式回傳值判斷原因http://en.cppreference.com/w/c/io/fprintf
當然你也可以自己實作:
http://blog.csdn.net/paschen/article/details/74554955
uj5u.com熱心網友回復:
還可以用gcvt實作浮點數轉字串uj5u.com熱心網友回復:
// 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>
uj5u.com熱心網友回復:
double PI=3.1415926;str.Format("%8.7f",PI);
輸出:
3.1415926
uj5u.com熱心網友回復:
可以 不是 <afxwin.h> 而是 <afx.h>uj5u.com熱心網友回復:
1 include <ctype> atoi()● itoa():將整型值轉換為字串。
● ltoa():將長整型值轉換為字串
2 string. format
另外,如果是易語言的話,到文本(),
uj5u.com熱心網友回復:
如果樓主不想處理寬字符問題,就請用MessageBoxAuj5u.com熱心網友回復:
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
uj5u.com熱心網友回復:
樓主:你的程式是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的方式,因為它兼容多位元組和寬位元組
uj5u.com熱心網友回復:
剛才代碼有誤,更新如下: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;
}
uj5u.com熱心網友回復:
CString::Formatvoid 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
uj5u.com熱心網友回復:
我改良了一下, int 型%d ,可以通過。 換成 double 型, %lf 只輸出一個f ,%f 也只輸出一個f
你這樣不行,wsprintf 不支持浮點數。你還是用 _stprintf 吧。包含 <stdio.h>、<stdlib.h>、<tchar.h>。
uj5u.com熱心網友回復:
不建議用MessageBox來測驗變數值,推薦用DebugView這個工具來輸出測驗資訊,在程式中使用OutputDebugString(多位元組和Unicode版本分別是OutputDebugStringW和OutputDebugStringA),就可以將資訊輸出到DebugView中來查看,內容可以定義一個char或者wchar陣列,然后sprintf來格式化生成。因為MessageBox會阻塞主程式,有副作用uj5u.com熱心網友回復:
用Format轉成CString MessageBox(str) 或者 AfxMessageBox(str)轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/72205.html
標籤:界面
下一篇:關于阻塞recv超時回傳判斷
