Print a new line using the TextOut function

I am trying to print a new line using the TextOut function.

I tried

 TextOut(hDC, 10, 20, "Hello\nWorld", strlen(text)); 

But the conclusion is "HelloWorld."

How to print a new line using TextOut ?

+4
source share
3 answers

Simple TextOut has no formatting capability. Use DrawText . See Formatting flags in the center of the text, calculating a rectangle, etc. You do not need to use the DT_EDITCONTROL flag to perform DrawText formatting. For instance,

 HDC dc = ::GetDC(0); RECT rc; char *lpsz= "Hello\r\nWorld"; ::SetRect(&rc,0,0,300,300); ::DrawText(dc,lpsz,::strlen(lpsz),&rc,DT_LEFT | DT_EXTERNALLEADING | DT_WORDBREAK); ::ReleaseDC(0,dc); 
+3
source

TextOut does not format special characters such as carriage returns, can you use DrawText instead?

+2
source

All you can do with TextOut calls it every time you need a new line, and increase the y coordinate according to the settings, such as the font size and the selected printer (if the selected printer is "Generic / Text Only" in FILE port, just leave it alone alone). Otherwise, the text will be scrambled if it simply does not appear at all. With this in mind, this function is suitable for the intent of plain text and knows exactly the length of the text based on the font attributes. Therefore, it is best to use a POS printer or use a monospace font, leaving all text-breaking operations to your concern.

 int increment, y; char *text, *text0; increment=25; y=0; text="Hello"; text0="World"; TextOut(hDC,10,y+=increment,text,strlen(text)); TextOut(hDC,10,y+=increment,text0,strlen(text0)); TextOut(hDC,10,y+=increment,"",0); TextOut(hDC,10,y+=increment,"",0); 
+1
source

All Articles