Как вывести содержимое переменной в MessageBox?
У меня есть функция с MessageBox.
Мне нужно сделать, чтобы в содержимое MessageBox выводилась переменная a.
Попытался использовать вот это, но мало того, что переменная не вывелась, так еще и половина текста куда-то пропала:
int a = 10;
int DisplayResourceNAMessageBox()
{
int msgboxID = MessageBox(
NULL,
(LPCTSTR)L"Resource not available\nDo you want to try again? "+a,
(LPCTSTR)L"Account Details",
MB_ICONWARNING | MB_OK | MB_DEFBUTTON1
);
return msgboxID;
}
int main()
{
DisplayResourceNAMessageBox();
}
P.S. Пример взял с сайта Майкрософта
Ответы (2 шт):
Автор решения: KoVadim
→ Ссылка
где то так
wchar_t buff[1024];
swprintf(buff, 1024, L"Resource not available\nDo you want to try again? a = %d", a);
int msgboxID = MessageBox(
NULL,
(LPCTSTR)buff,
(LPCTSTR)L"Account Details",
MB_ICONWARNING | MB_OK | MB_DEFBUTTON1
);
Автор решения: Harry
→ Ссылка
#define UNICODE
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32")
int a = 10;
int DisplayResourceNAMessageBox()
{
wchar_t buf[100];
swprintf(buf,100,L"%s%d",L"Resource not available\nDo you want to try again? ",a);
// Так тоже работает, хотя и выводит предупреждение...
// swprintf(buf,L"%s%d",L"Resource not available\nDo you want to try again? ",a);
int msgboxID = MessageBox(
NULL, buf,
(LPCTSTR)L"Account Details",
MB_ICONWARNING | MB_OK | MB_DEFBUTTON1
);
return msgboxID;
}
int main()
{
DisplayResourceNAMessageBox();
}
Вариант с wstring:
#define UNICODE
#include <windows.h>
#include <string>
#pragma comment(lib, "user32")
int a = 10;
int DisplayResourceNAMessageBox()
{
std::wstring buf = L"Resource not available\nDo you want to try again? "
+ std::to_wstring(a);
int msgboxID = MessageBox(
NULL, buf.c_str(),
(LPCTSTR)L"Account Details",
MB_ICONWARNING | MB_OK | MB_DEFBUTTON1
);
return msgboxID;
}
int main()
{
DisplayResourceNAMessageBox();
}