問題描述
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++ 技巧,我也可以使用它。
謝謝你
參考解法
方法 1:
This is explained in TextOut
's documentation:
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.
The below is a Delphi adaption of the mentioned sample code and your snippet, draws yellow outlined blue text:
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;
(by Sam、Sertac Akyuz)