Displaying a double using DrawString()

This is a discussion on Displaying a double using DrawString() within the Windows Programming forums, part of the Platform Specific Boards category; Hi everyone, I'm working on a program in which I want to display a reading (in the form of a ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    128

    Displaying a double using DrawString()

    Hi everyone,

    I'm working on a program in which I want to display a reading (in the form of a double) in a window using GDI+. I am able to use the DrawString method to display a given WCHAR, but I can't work out how to display a double. I know I will probably have to convert it to a WCHAR, but again, I'm lost.

    Can anyone help me out on this at all? Failing that, is there another GDI+ function I can use to display doubles on the screen?

    Thanks.

  2. #2

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    128
    DrawString requires (for some stupid reason) that you convert to a WCHAR before you can display the output. I can't find anything on how to convert a double to a WCHAR...

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,288
    I haven't dabbled much in wide characters, but have you looked into perhaps using "swprintf()" to print the value to a wide-character string?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    128
    Thanks for the reply - that seems to be the right way to go, but now I can't figure out how to use swprintf()! If anyone who knows about this kind of thing could give me or direct me to an example bit of code on how to convert a double to a wchar_t, then it would be a big help

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    128
    After a lot more trying, I came up with this method that works:

    Code:
    double d = 1.23
    std::wstringstream a;
    a << d;
    std::wstring result = a.str();
    graphics.DrawString(result.c_str(), -1, &font, PointF(x,y), &brush);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 04-15-2012, 07:10 PM
  2. Replies: 3
    Last Post: 04-05-2008, 07:44 AM
  3. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  4. Changing double time to double cost
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 10:29 AM
  5. the diffrence between static double and double
    By sayeem81 in forum C Programming
    Replies: 6
    Last Post: 02-18-2002, 11:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21