【发布时间】:2015-03-18 04:04:41
【问题描述】:
我正在尝试为我的图像编辑应用程序创建一个 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 上仍然没有显示任何内容,所以我认为可能是位置有问题?
请帮助我解决这个问题。
非常感谢
【问题讨论】:
-
只是猜测蓝色而不涉及任何细节:您是否在调试器中检查了该位置以确保它没有放置在屏幕外?使用 'p.x - maxwidth' 它可以放置在 ImgView 的边界之外。
-
我尝试使用问题中提到的固定位置(在底部),但仍然没有任何显示。
标签: delphi delphi-xe graphics32