0

如何使用 DirectX9 将变量的值绘制到屏幕上?我知道我可以使用 DrawText() 方法来绘制这样的文本:

m_pD3DFont->DrawText(0, "text", -1, &rect, DT_TOP | DT_RIGHT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

但是我如何让它来绘制变量的值呢?具体来说,一个整数。我尝试将整数转换为字符串,但随后出现错误消息

无法将参数 2 从“std::string *”转换为“LPCWSTR”

任何帮助表示赞赏。

4

2 回答 2

0

To convert integers to strings, strings to integers and so on you may use std::stringstream like that

std::stringstream ss;
std::string s = "123";
ss << s;
int result; 
ss >> result;

More information here http://cplusplus.com/reference/iostream/stringstream/

Or you can use std::wstringstream and std::wstring if you need Unicode-strings for your application.

于 2012-06-14T07:14:43.580 回答
0

As the previous poster mentioned, you can use stringstreams to convert ints to strings and vise versa, but with C++11 there are some new features that eliminate the need to do this. You can use std::to_string or std::to_wstring to convert ints to strings or wstrings, and you can use c_str() to get the raw char* or wchar_t* from the string object. You should be able to convert the appropriate type of pointer to your winapi string, but some can depend on your compiler's settings.

For your reference, here's what the winapi strings mean:

LPSTR = char*
LPCSTR = const char*
LPWSTR = wchar_t*
LPCWSTR = const wchar_t*
LPTSTR = char* or wchar_t* depending on _UNICODE
LPCTSTR = const char* or const wchar_t* depending on _UNICODE

And here is a quick sample of how to assign them to C++ strings:

#include <iostream>
#include <string>
#include <Windows.h>
#include <tchar.h>

int main()
{
    // Declare winapi strings
    LPSTR str_charPtr;
    LPCSTR str_constCharPtr;
    LPWSTR str_wcharPtr;
    LPCWSTR str_constWcharPtr;
    LPTSTR str_tcharPtr;
    LPCTSTR str_constTcharPtr;

    // Declare a test integer
    int num = 5001;

    // Convert the integer to a string and to a wstring
    std::string regString = std::to_string(num);
    std::wstring wideString = std::to_wstring(num);

    // Attempt to assign the winapi strings to the C++ standard strings
    str_charPtr = const_cast<char*>(regString.c_str());         // NOTE: removing const to store in non-const LPSTR
    str_constCharPtr = regString.c_str();
    str_wcharPtr = const_cast<wchar_t*>(wideString.c_str());    // NOTE: removing const to store in non-cost LPWSTR
    str_constWcharPtr = wideString.c_str();
    str_tcharPtr = const_cast<TCHAR*>(regString.c_str());       // Error if set to Unicode
    str_tcharPtr = const_cast<TCHAR*>(wideString.c_str());      // Error if NOT set to Unicode
    str_constTcharPtr = regString.c_str();                      // Error if set to Unicode
    str_constTcharPtr = wideString.c_str();                     // Error if NOT set to Unicde

    return 0;
}

If you've moved on to Visual Studio 2012 by now, you can adjust your settings like so:
1. Right-click the project in the solution explorer.
2. Click on Properties.
3. In the left pane under Configuration Properties, select General
4. In the right pane, look in the Project Defaults section.
5. Next to Character Set, choose Unicode or whichever option is appropriate for you.

于 2013-02-10T20:53:01.940 回答