c++ - 如何使用 int 变量绘制 DrawString

标签 c++ gdi+

所以我正在使用 Gdiplus 并尝试使用 Drawstring 方法输出一些文本。

我有

graphics.DrawString(&myVariable, -1, &myFont, Gdiplus::PointF(x, y), &brushVariable);

但是对于 &myVariable 参数,我想从我的 for 循环中打印出整数。因为它是一个整数,我需要把它变成一个字符串。我研究了如何使用 stringstream 和 to_string(i) 执行此操作,但是 DrawString 方法仍然给我一个错误,说我没有输入正确的参数。我知道这是 &myVariable 参数的问题,因为我不确定如何将 int 转换为可以使用 & 取消引用的字符串。 论点是: DrawString(std::string *, int, Gdiplus::font *, Gdiplus::PointF, Gdiplus::SolidBrush *);

最佳答案

以下应该有效(包括 <sstream> header ):

//...
for (size_t i = 0; i < n; ++i)
{
    std::string printStr;
    {
        std::stringstream sstr;
        sstr << i;
        printStr = sstr.str();
    }
    //now, you can use printStr
    //...
}

关于c++ - 如何使用 int 变量绘制 DrawString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43245154/

相关文章:

c++ - 为什么这段代码可以编译

c++ - python 字符串使用特殊字符发送到 c++ dll,崩溃

c++ - Gdiplus::Bitmap::Lock/UnlockBits 总是复制数据吗?

C++ Gdiplus 单色像素值

c++ - 从图像中提取缩略图的最快方法是什么?

c++ - 是否有 C++ 习惯用法通过模板魔法将所谓的速率组(实时计算)编码到函数中?

c++ - 如何使用 RestartManager 通过 Windows Installer 自定义操作重新启动 explorer.exe?

c++ - 我应该#include 在我的库的命名空间中吗?

c# - 困惑。 SmoothingMode.AntiAlias 和 SmoothingMode.HighQuality 之间的区别

C++/WinAPI GDI+ 双缓冲