2

here is the code that I have in OnPaint event of my form:

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

but here is what I get on the form:

enter image description here

as you can see the text comes out inverted (the text has to be blue and background gray but it is the other way around and the yellow line is around the background instead of text). Does anyone know how I can fix this?

I am using C++ Builder 10 Seattle but if anyone knows that Delphi or pure C++ trick, I can work with that as well.

Thank you

4

1 回答 1

4

这在TextOut文档中有解释:

TextOut函数放在路径括号内时,系统会为 TrueType 文本生成一个路径,该路径包括每个字符及其字符框。生成的区域是字符框减去文本,而不是文本本身。在将TextOut函数放入路径括号之前,您可以通过将背景模式设置为透明来获得 TrueType 文本的轮廓所包围的区域 。以下是演示此过程的示例代码。

下面是提到的示例代码和您的代码段的 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;
于 2016-03-31T22:53:55.523 回答