-2

I am coding a Win32 Program,and I want to text out the X pos and Y pos to the Screen, and I want to know how to convert SHORT to TCHAR. don't use the atoi or itoa function.

This is a toy program, and I want to text out the position of the mouse, but I donnot konw how to convert short to TCHAR.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user3116182
  • 45
  • 1
  • 4
  • 1
    use [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string) or its wide char sibling, don't use `TCHAR` type. `TCHAR` and friends were a Microsoft technology in support of Windows 9x. Are you targeting Windows 9x? – Cheers and hth. - Alf Dec 18 '13 at 18:05
  • When you say "TCHAR", do you mean "a single TCHAR (which is an alias for *either* `char` or `wchar_t`, depending on how you compile your code), or do you mean "an array of TCHAR" which, again, is either an array of `char` or of `wchar_t`, in other words, a C-style string. – jalf Dec 18 '13 at 18:23
  • 1
    Um, you are leaking the DC. Also, you are drawing outside the paint cycle, which will cause repaint corruption. – Raymond Chen Dec 18 '13 at 19:04
  • Yes, I am leaking the DC, but I want to know why drawing ouside the paint cycle can cause repaint corruption. and how the textout function works. – user3116182 Dec 18 '13 at 19:13
  • If you draw in WM_MOUSEMOVE and your entire window ends up being redrawn with a WM_PAINT, you will lose your text display at 100,100. This is because a WM_ERASEBKGND will cause your entire screen to be erased. You basically need to store the x,y coordinates and paint the same thing in the WM_PAINT handler – Paladine Dec 18 '13 at 19:29

3 Answers3

1

Maybe you want to convert a unsigned int to a string. You can use std::to_wstring if TCHAR is defined as a WCHAR:

short x = 123;    
std::wstring s = std::to_wstring(x);

Then convert s.c_str() to a TCHAR*.

Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
1

You could use stringstream.

#include <sstream>

std::stringstream ss("");
ss << nX << " " << nY;

TextOut(GetDC(hWnd), 100, 100, reinterpret_cast<TCHAR *>(ss.str().c_str()), ss.str().size());
Chad Befus
  • 1,918
  • 1
  • 18
  • 18
0
SHORT myVal;
TCHAR buf[32]; // 32 is big enough to contain a 16-bit int as a string

_stprintf_s(buf,ARRAYSIZE(buf),"%hd",myVal);
// now buf contains the text as a TCHAR
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Paladine
  • 513
  • 3
  • 11