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?
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:
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.
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)
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.
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
@ 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.