C 使用TextOut功能打印新行

C 使用TextOut功能打印新行,c,winapi,C,Winapi,我正在尝试使用TextOut功能打印新行 我试过了 TextOut(hDC, 10, 20, "Hello\nWorld", strlen(text)); 但输出是“HelloWorld” 如何使用文本输出打印新行?简单文本输出没有任何格式化功能。改用DrawText。请参见格式化标志以使文本居中、计算矩形等。您不必使用dtu EDITCONTROL标志来完成DrawText格式设置。比如说, HDC dc = ::GetDC(0); RECT rc; char *lpsz= "Hello\r

我正在尝试使用
TextOut
功能打印新行

我试过了

TextOut(hDC, 10, 20, "Hello\nWorld", strlen(text));
但输出是“HelloWorld”


如何使用
文本输出打印新行?

简单<代码>文本输出
没有任何格式化功能。改用
DrawText
。请参见格式化标志以使文本居中、计算矩形等。您不必使用
dtu EDITCONTROL
标志来完成
DrawText
格式设置。比如说,

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);

TextOut不会格式化特殊字符(如回车符),您可以使用DrawText来代替?

使用TextOut可以在每次需要新行时调用它,并根据字体大小和所选打印机等设置增加y坐标(如果所选打印机在文件端口中为“通用/仅文本”,请逐个保留)。否则,如果文本根本不出现,它将被置乱。考虑到这一点,此函数适用于纯文本意图,并考虑字体属性准确了解文本长度。因此,最好使用POS打印机或使用单空格字体,将所有文本包装操作留给您自己处理

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);

同意抽签文本优先

var
  idxRow    : integer    ; // loop counter
  strRow    : string     ; // string version
  strOut    : Ansistring ; // output buffer
  ptrOut    : PChar      ; // pointer to output buffer
  intOutLen : LongInt    ; // length of output data
  objRct    : TRect      ; // area to draw into
  intRowOfs : LongInt = 0; // how far down the window we want to draw into
  intRowHit : LongInt = 0; // how much vertical space was taken up by the output
begin // the procedure
  for idxRow  := 1 to 5 do begin                                // run the demo this many times
    Str(idxRow, strRow);                                        // convert loop counter from integer to string
    strOut    := 'Hello World row ' + strRow;                   // prepare        the output data
    ptrOut    := PChar (strOut);                                // get pointer to the output data
    intOutLen := Length(strOut);                                // get length  of the output data

    SetRect(objRct, 0, intRowOfs, ClientWidth, ClientHeight);   // set the area to draw into, at next row and left edge
    intRowHit := DrawText(intDC, ptrOut, intOutLen, objRct, 0); // draw the output into the area with default formatting
    intRowOfs := intRowOfs + intRowHit;                         // increment row offset by whatever line-height was returned by DrawText
  end;                                                          // done with demo
end; // the procedure

TextOut
函数中,我只给出x和y坐标。现在很难使用
DrawText
定位文本。但是我会尝试使用它。@CanVural你也可以使用SetRect。。。看看第三个和第四个参数。它们是X-Y代表绘图的“面积”。最好从窗口获取“增量”,而不是猜测。请参阅下面的其他答案。