Click to See Complete Forum and Search --> : Using DrawString to render a CString


robbiegregg
June 12th, 2006, 11:06 AM
Hi all

I'm would like to use DrawString to write the contents of a CString to screen however so far I have failed.

I have tried to find some easy way of converting my CString variables to unsigned short (WCHAR I think) so I can use them in DrawString but have had no luck with this.

Is there an easier way to use DrawString so I can just pass a CString object to the function rather than a WCHAR?

ovidiucucu
June 13th, 2006, 05:26 AM
CDC::DrawText functions has the following prototypes:
int DrawText(const CString& str, LPRECT lpRect, UINT nFormat);
// and...
virtual int DrawText(LPCTSTR lpszString, int nCount, LPRECT lpRect, UINT nFormat);
So you can directly pass a CString object. For the first one it's clear, For the second also possible, because CString has an operator LPCTSTR.

Note that LPCTSTR is a macro that, if UNICODE is defined, expands to LPCWSTR, which is of type CONST WCHAR* (or const wchar_t*)

robbiegregg
June 13th, 2006, 10:13 AM
I was hoping there was a GDI+ alternative. I think DrawText is a GDI (no plus) function?

Cheers

ovidiucucu
June 13th, 2006, 10:28 AM
Oh, sorry, you didn't specify GDI+, so I did a mistake.

The Gdiplus::Graphics::DrawString method takes indeed a parameter of type const WCHAR *, so if you declare UNICODE at Preprocessor project settings you can pass a CString object as well (see my first note).

robbiegregg
June 14th, 2006, 08:18 AM
Cheers - thanks for the advice

asiawinter
June 19th, 2006, 06:38 AM
you may convert a CString variable to WCHAR variable by "A2W" function.