我正在尝试为我的图像编辑应用程序创建一个 textTool,所以我在主窗体上有一个按钮,上面写着“文本工具”。当我单击它时,会显示一个新表单(模式),允许我选择字体并在 RichEdit 中输入文本。
我的想法是让用户在 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;
并解析richedit的行并像这样获取每个行的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;
所以我很容易获得最大宽度线(maxwidth),我打算用它来生成我的Bitmap32。
因此,对于我的 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 bor),但我的 ImageView 上仍然没有显示任何内容,所以我认为位置可能有问题?
请帮助我解决这个问题。
非常感谢