Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DrawString(GDI+) & CString

DrawString(GDI+) & CString

Scheduled Pinned Locked Moved C / C++ / MFC
graphicswinforms
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NoName II
    wrote on last edited by
    #1

    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);

    P M 2 Replies Last reply
    0
    • N NoName II

      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);

      P Offline
      P Offline
      Parthi_Appu
      wrote on last edited by
      #2

      DrawString will take WCHAR* as its first argument, CString::GetBuffer() won't workout. Try using WCHAR* instead str.GetBuffer()

      1 Reply Last reply
      0
      • N NoName II

        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);

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        There's no need to use GetBuffer, you can simply pass the CString object, if it's the right width. I suspect you're compiling without UNICODE defined, so your CString is actually a CStringA, which does not have a conversion operator to const 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 define UNICODE and _UNICODE, or select the 'Unicode' configuration if you already have one. You need to define both macros because the Windows headers use UNICODE while the C run-time uses _UNICODE. This forces all CString 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 using CStringW 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 a CStringW from byte-oriented national character set data (char*). You can convert at the point of use if necessary. Using CStringW is slightly more straightforward than trying to use the MultiByteToWideChar API directly. If explicitly using the wchar_t, WCHAR or CStringW datatypes, you should use the L"" 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups