BeginPath Textout EndPath绘制反转文本

时间:2016-03-31 21:54:50

标签: c++ delphi graphics gdi c++builder-10-seattle

这是我在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);

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

enter image description here

正如你所看到的那样,文本是倒置的(文本必须是蓝色和背景灰色,但它是相反的方式,黄色线是在背景而不是文本)。有谁知道我怎么解决这个问题?

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

谢谢

1 个答案:

答案 0 :(得分:4)

TextOut&#39; documentation中解释了这一点:

  

TextOut 函数放在路径括号内时,   system为TrueType文本生成包含每个文本的路径   字符加上其字符框。生成的区域是   字符框减去文本,而不是文本本身。您可以   获取由TrueType文本的轮廓包围的区域   在放置之前将背景模式设置为透明   路径括号中的 TextOut 功能。以下是演示此过程的示例代码。

以下是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;