BeginPath Textout EndPath 繪製反轉文本 (BeginPath Textout EndPath draws inverted text)


問題描述

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 SamSertac Akyuz)

參考文件

  1. BeginPath Textout EndPath draws inverted text (CC BY‑SA 2.5/3.0/4.0)

#gdi #graphics #delphi #c++builder-10-seattle #C++






相關問題

繪製鼠標光標 (Draw mouse cursor)

C 和 Windows GDI 中的雙緩衝 *framework* (Double-buffering *framework* in C and Windows GDI)

一個進程的 GDI 洩漏會影響其他進程嗎? (Can GDI leaks from one process affect other processes?)

BeginPath Textout EndPath 繪製反轉文本 (BeginPath Textout EndPath draws inverted text)

監控GDI通話 (Monitoring GDI calls)

如何捕獲(並希望修復)GDI 資源洩漏 (How to catch (and hopefully fix) a GDI resource leak)

如何使用 GDI(+) 在內存中渲染漸變 (How to render a gradient in memory using GDI(+))

使用 BITMAP::bmBits 與 GetDIBits 有什麼區別? (What is the difference between using BITMAP::bmBits vs GetDIBits?)

獲取背景窗口的縮略圖 (Get Thumbnail of background window)

AlphaBlend 返回“假”的原因可能是什麼 (What could be the reason for AlphaBlend to return 'false')

在 C++ 中查找像素 RGB 數據的最快方法是什麼? (What is the fastest method to lookup pixel RGB data in C++?)

逐行讀取文本框並將其視為單獨的文本 (Read a textbox line by line and treat it as a separate text)







留言討論