TextOut giving gibberish ..

Hey,

I have spent almost one day to figre the problem out but do not have nay clue what is going. I am trying to print a 'double' number as text on to MDIchild Window. Here is a snippet of my code:
PAINTSTRUCT ps;
hRefDC = BeginPaint(hWnd, &ps);
SetTextColor(hRefDC, RGB(0, 0, 0));
dose = MaxD/5; //Dose is a double precision number
char sBuf[5]={0};
printf_s(sBuf, 10, "Gy %4.3f" , dose);
TextOut( hRefDC, 1, x, LPCTSTR(sBuf), 5);
}
EndPaint(hWnd, &ps);
But all I see is a bunch of rectangular boxes (gibberish), being dispalyed in the window. Can somebody please help me out?

Thanks
Vijay
Don't cast around compiler errors.

TextOut was giving you an error before you added the LPCTSTR cast, right? That's because you're doing it wrong. Casting doesn't solve the problem, it just tells the compiler to shut up.

If you're using a char array, you need to use the ASCII version of the function, so you should use TextOutA:

TextOutA( hRefDC, 1, x, sBuf, 5);

More information here:

http://www.cplusplus.com/forum/articles/16820/
Hey Disch,

Thanks for the reply. It did not strike me that their could be a ASCII version of TextOut. But unluckily TextOutA does not work either. I still see the same boxes for the text. Any idea what I could be messing up.

Thanks
Vijay
closed account (z05DSL3A)
Try something along the lines of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case WM_PAINT:
    {
        hdc = BeginPaint(hWnd, &ps);

        const int bufferSize = 20;
        double dose = 12455.268; 
        TCHAR sBuf[bufferSize]={0};

        _sntprintf_s(sBuf, bufferSize, TEXT("A double: %4.3f") , dose);

        SetTextColor(hdc, RGB(0, 0, 0));

        TextOut( hdc, 1, 1, sBuf, bufferSize);
        EndPaint(hWnd, &ps);
    }
    break;


PS. TextOut() is just a macro that calls a different version of depending on if UNICODE is defined or not TextOutA()/TextOutW(). There are lots of API calls that are hidden behind macros in this way. It is best to write your code with TCHARS and such to allow clean builds no matter the definition of UNICODE. (See: Disch's link)
Last edited on
Hello Grey Wolf,

The TCHAR does not seem to wor k with _snprintf_s. _snprintf_s is looking for a char for it's first parameter. However I tried to the following snippet it worked for me. Thank you very much to help me get so far. I would like to see TCHAR work just to save the mess in later parts of my project. I would appreciate it if you could help me resolve the issue.

const int bufferSize = 20;
char sBuf[bufferSize]={0};
_snprintf_s(sBuf, bufferSize, ("%4.3f"), dose);
TextOutA(hRefDC, 1, x, (sBuf), bufferSize );


Thanks again
Vijay
Besides the UNICODE problem:

1
2
char sBuf[5]={0}; // Here you have a buffer with 5 char
sprintf_s(sBuf, 10, "Gy %4.3f" , dose); // Here you tell sprintf_s your buffer has 10 char which should chrash 


printf_s does nothing with the sBuf hence gibberish
closed account (z05DSL3A)
note the t in the code I posted: _sntprintf_s(sBuf, bufferSize, TEXT("A double: %4.3f") , dose);. This function works with TCHAR.
Last edited on
@ COder777 .. sorry for that mess, but I had the correct format in my program. So that was not really an issue.

@ Grey Wolf ... my compiler is unable to find _sntprintf_s. I thought it should be defined in cstdio. Can you please direct me to the correct header to use the function.


Thanks
Vijay
closed account (z05DSL3A)
_sntprintf_s should be defined in 'tchar.h'.

Is is another macro, it resolves to either _snwprintf_s or _snprintf_s
Last edited on
Oh ok .. Thanks Grey Wolf. That works great. Thanks a lot.


Regards
Vijay
Topic archived. No new replies allowed.