BeginPath Textout EndPath绘制倒排文字

BeginPath Textout EndPath draws inverted text

这是我在表单的 OnPaint 事件中的代码:

int elementCount;
String tStr = L"15:00";

::BeginPath(Canvas->Handle);
::TextOut(Canvas->Handle, 5, 5, tStr.c_str(), tStr.Length());
::EndPath(Canvas->Handle);
elementCount = ::GetPath(Canvas->Handle, NULL, NULL, 0);
Canvas->Brush->Color = clBlue;
Canvas->Pen->Color = clYellow;
Canvas->Pen->Width = 4;
if(0 < elementCount)
{
    boost::scoped_array<TPoint> mPoints(new TPoint[elementCount]);
    boost::scoped_array<BYTE> mTypes(new BYTE[elementCount]);

    ::GetPath(Canvas->Handle, mPoints.get(), mTypes.get(), elementCount);
    ::FillPath(Canvas->Handle);
    ::PolyDraw(Canvas->Handle, mPoints.get(), mTypes.get(), elementCount);
}
else
    ::StrokeAndFillPath(Canvas->Handle);

但这是我在表格上得到的:

如您所见,文本是倒过来的(文本必须是蓝色的,背景是灰色的,但它是相反的,黄线是背景而不是文本)。有谁知道我该如何解决这个问题?

我正在使用 C++ Builder 10 Seattle,但如果有人知道 Delphi 或纯 C++ 技巧,我也可以使用它。

谢谢

这在 TextOutdocumentation 中有解释:

When the TextOut function is placed inside a path bracket, the system generates a path for the TrueType text that includes each character plus its character box. The region generated is the character box minus the text, rather than the text itself. You can obtain the region enclosed by the outline of the TrueType text by setting the background mode to transparent before placing the TextOut function in the path bracket. Following is sample code that demonstrates this procedure.

下面是上述示例代码和您的代码段的 Delphi 改编,绘制了黄色轮廓的蓝色文本:

procedure TForm1.FormPaint(Sender: TObject);
var
  elementCount: Integer;
  mPoints: array of TPoint;
  mTypes: array of Byte;
const
  tStr = '15:00';
begin
  BeginPath(Canvas.Handle);
  Canvas.Brush.Style := bsClear;
  TextOut(Canvas.Handle, 5, 5, PChar(tStr), Length(tStr));
  EndPath(Canvas.Handle);

  Canvas.Brush.Color := clBlue;
  Canvas.Pen.Color := clYellow;
  Canvas.Pen.Width := 4;

  elementCount := GetPath(Canvas.Handle, Pointer(nil)^, Pointer(nil)^, 0);
  if elementCount > 0 then begin
    SetLength(mPoints, elementCount);
    SetLength(mTypes, elementCount);
    GetPath(Canvas.Handle, mPoints[0], mTypes[0], elementCount);

    Canvas.Brush.Style := bsSolid;
    SelectClipPath(Canvas.Handle, RGN_AND);
    Canvas.FillRect(ClientRect);

    SelectClipRgn(Canvas.Handle, 0);
    PolyDraw(Canvas.Handle, mPoints[0], mTypes[0], elementCount);
  end else
    StrokeAndFillPath(Canvas.Handle);
end;