Archived

This topic is now archived and is closed to further replies.

DarrenKarstens

LPCWSTR

Recommended Posts

I am trying to use the DrawText command from a D3DXFONT object. The first paramater is of type LPCWSTR which from what I can work out is some kind of pointer to a string. The text im trying to display is in string format but I can''t seem to convert the string into LPCWSTR format. Any help with this problem would be greatly recieved. Darren Karstens.

Share this post


Link to post
Share on other sites
All you need to do to draw a text is put the text in quotes, or if you want a number or series of numbers and text to appear use the sprintf function.

Here are 2 examples:

RECT rect={0,0,200,100};


char FPS[20];
float FramesPerSecond=1/timeGetTime();

sprintf(FPS,"Frames per second %0.2f",FramesPerSecond);

Font->DrawText(FPS,-1,&rect,0,0xffffffff);

or

Font->DrawText("THis will appear on the screen",
-1,&rect,0,0xffffffff);


Share this post


Link to post
Share on other sites
I would use sprintf to create your string like I described above,

or you could just declare

char string[]="This is a string";

or

char *string="This is a string";

then Font->DrawText(string,-1,&rect,0xffffffff);

Share this post


Link to post
Share on other sites
LPCWSTR == (const) pointer to wide (2 byte unicode) string.

Define your string literals as WCHAR arrays like this:
WCHAR myString[] = L"the string";

The L in front of the quotes tells the compiler to use unicode encoding rather than ANSI. To pass a pointer, just pass the array name or use the address of the first character

LPCWSTR pWideString = myString;
or
LPCWSTR pWideString = &myString[0];


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

[edited by - Paradigm Shifter on January 28, 2003 7:18:28 AM]

[edited by - Paradigm Shifter on January 28, 2003 7:20:51 AM]

Share this post


Link to post
Share on other sites
Everyone seems to be missing the fact that he is using the wide string version, he must be compiling for UNICODE.

There are portable versions of the string types, check out TCHAR, the TEXT("stuff") macro (or _T("stuff")), and functions like _stprintf.

Share this post


Link to post
Share on other sites
Wide strings are UNICODE and often have embedded zeroes in them, since it's 2 bytes per character.
sprintf stops reading bytes as soon as it hits a zero terminator and adds a single zero terminator at the end.
Wide strings are terminated with 2 zero chars.

If a function is asking for a LPCWSTR, then a char* LPSTR or const char* LPCSTR won't do; there's no implicit conversion from char* to WCHAR* (WCHAR == wchar_t typedef) in C. If you want portable code that will work whether or not UNICODE is #define'd, look up TCHAR, _T and _stprintf and related.

Another alternative is to use a C++ class string type which has a conversion operator defined from char* to WCHAR*. That will allocate a buffer and copy the ANSI string into a UNICODE one which will be twice as long in memory.

[edited by - Paradigm Shifter on January 28, 2003 8:16:55 AM]

Share this post


Link to post
Share on other sites
Paradigm Shifter is right. However, be careful in assuming that a wchar_t is always 2 bytes... I''ve seen *nix machines that use 4 byte values.

-Glyph

Share this post


Link to post
Share on other sites