DrawString(GDI+) & CString
-
Can't draw CString string with DrawString(GDI+) CString str=_T("test string"); FontFamily fontFamily(L"Times New Roman"); Font font(&fontFamily, 24, FontStyleRegular, UnitPixel); PointF pointF(30.0f, 10.0f); SolidBrush solidBrush(Color(255, 0, 0, 255)); graphics.DrawString(str.GetBuffer(), -1, &font, pointF, &solidBrush);
-
Can't draw CString string with DrawString(GDI+) CString str=_T("test string"); FontFamily fontFamily(L"Times New Roman"); Font font(&fontFamily, 24, FontStyleRegular, UnitPixel); PointF pointF(30.0f, 10.0f); SolidBrush solidBrush(Color(255, 0, 0, 255)); graphics.DrawString(str.GetBuffer(), -1, &font, pointF, &solidBrush);
DrawString will take WCHAR* as its first argument, CString::GetBuffer() won't workout. Try using WCHAR* instead str.GetBuffer()
-
Can't draw CString string with DrawString(GDI+) CString str=_T("test string"); FontFamily fontFamily(L"Times New Roman"); Font font(&fontFamily, 24, FontStyleRegular, UnitPixel); PointF pointF(30.0f, 10.0f); SolidBrush solidBrush(Color(255, 0, 0, 255)); graphics.DrawString(str.GetBuffer(), -1, &font, pointF, &solidBrush);
There's no need to use
GetBuffer
, you can simply pass theCString
object, if it's the right width. I suspect you're compiling withoutUNICODE
defined, so yourCString
is actually aCStringA
, which does not have a conversion operator toconst WCHAR*
. It stores byte-oriented national character set characters. GDI+ expects UTF-16 Unicode characters. You need to either store your strings in this format, or convert them when you need to use them. Unless you want compatibility with Windows 9x, the easiest thing to do is to simply defineUNICODE
and_UNICODE
, or select the 'Unicode' configuration if you already have one. You need to define both macros because the Windows headers useUNICODE
while the C run-time uses_UNICODE
. This forces allCString
objects to be UTF-16 Unicode. You'll have to fix any compile errors you get here, where you've used a string not wrapped in_T()
. Slightly more difficult is to store any strings you want to display on screen in the correct format. Here you should consider usingCStringW
objects (I'm assuming you're using at least MFC 7.0 from Visual Studio .NET 2002, rather than MFC 6.0). You can construct aCStringW
from byte-oriented national character set data (char*
). You can convert at the point of use if necessary. UsingCStringW
is slightly more straightforward than trying to use theMultiByteToWideChar
API directly. If explicitly using thewchar_t
,WCHAR
orCStringW
datatypes, you should use theL""
syntax to specify your string literals, rather than_T()
. Only use_T()
for values that should change format depending on whether_UNICODE
is defined. Stability. What an interesting concept. -- Chris Maunder