画像編集アプリ用の textTool を作成しようとしているので、メインフォームに「テキストツール」というボタンがあります。クリックすると、新しいフォーム (モーダル) が表示され、フォントを選択して RichEdit にテキストを入力できます。
私の考えは、ユーザーがリッチエディットで自分のテキストを書式設定することです。満足したら、モーダル ボタンをクリックすると、(書式設定された) テキストが画像の新しいレイヤーに挿入されます。また、私の考えは、テキストをテキスト行として扱い、それらを新しい Bitmap32 で個別にレンダリングしてから、取得したビットマップを新しいレイヤーに割り当てることです。
そのために私はこの機能を使用します
function GetTextWidth(const Text: string; const Font: TFont): Integer;
var
Canvas: TCanvas;
begin
Canvas := TCanvas.Create;
try
Canvas.Handle := GetDC(0);
try
Canvas.Font.Assign(Font);
Result := Canvas.TextWidth(Text);
finally
ReleaseDC(0, Canvas.Handle);
end;
finally
Canvas.Free;
end;
end;
リッチエディットの行を解析し、次のようにそれぞれの textWidth を取得します。
for q := 0 to TextEditor.RichEdit1.Lines.Count-1 do
begin
latime:=GetTextWidth(TextEditor.RichEdit1.Lines[q], TextEditor.RichEdit1.Font);
Memo1.Lines.Add('Text row '+IntToStr(q)+' width='+IntToStr(latime));
end;
したがって、Bitmap32 の生成に使用する最大幅の線 (maxwidth) を簡単に取得できます。
したがって、私の Bitmap32 では、次のコードを使用します (残念ながら何も表示されません)。
// generating the Bitmap32 to place text on it
tmp := TBitmap32.Create;
tmp.SetSize(maxwidth, textLineHeight*TextLineCount);
for q := 0 to textLineCount-1 do
begin
tmp.RenderText(maxwidth,textLineHeight,TextEditor.RichEdit1.Lines[q],1,myFont.Color);
Memo1.Lines.Add('rendering line of text:'+TextEditor.RichEdit1.Lines[q]);
end;
// Generating the text layer
B := TBitmapLayer.Create(ImgView.Layers);
// Assigning the 'written' bitmap to the new layer
B.Bitmap.Assign(tmp);
B.Bitmap.DrawMode:=dmBlend;
// Positioning
with ImgView.GetViewportRect do
P := ImgView.ControlToBitmap(GR32.Point((Right + Left) div 2, (Top + Bottom) div 2));
// Sizing the layer
B.Location:=GR32.FloatRect(P.X - maxwidth, P.Y - textLineHeight, P.X + maxwidth, P.Y + textLineHeight);
// displaying the new text layer
imgView.Invalidate;
エラーは発生しませんが、新しいレイヤーが表示されません...何も起こらないようです。
幅と高さの固定値を使用してみました (両方とも 200 を使用しました) が、まだ ImageView に何も表示されないので、場所に何か問題があると思いますか?
この問題を解決するのを手伝ってください。
どうもありがとうございました