gpt4 book ai didi

delphi - 将字形和 WordWrap 文本一起绘制

转载 作者:行者123 更新时间:2023-12-01 19:25:01 26 4
gpt4 key购买 nike

我有一个 Canvas 、字形、文本和整数变量空间。我想根据 TRadioGroup 的 ItemIndex 将字形和文本绘制到 Canvas 上(例如,如果 ItemIndex = 1,则在文本的左侧绘制字形等)在任何情况下,必须在字形和文本之间插入空格作为空格。Glyph 和 Text 都必须在 Canvas 上居中,即使 Canvas 的宽度小于 Glyph 的宽度 + Text 的宽度。

我试图实现这个任务,但我对一些计算感到困惑。下面附上我的代码:

procedure TForm1.Button1Click(Sender: TObject);
var
R1, R2: TRect;
S: String;
G, D: Integer;
B, C: TBitmap;
begin
S := 'This is an example text to show how WordWrap works';
B := TBitmap.Create;
B.LoadFromFile('C:\g.bmp');

C := TBitmap.Create;
C.Width := 242;
C.Height := 96;

Form1.Repaint;
Form1.Canvas.Brush.Color := clGreen;
Form1.Canvas.FillRect(Rect(0, 0, TrackBar1.Position, C.Height));

// left
if RadioGroup1.ItemIndex = 0 then
begin
R1 := Rect(B.Width + TrackBar2.Position, 0, TrackBar1.Position, C.Height);
Form1.Canvas.TextRect(R1, S, [tfWordBreak, tfNoClip, tfCenter, tfCalcRect]);

Form1.Canvas.Brush.Color := clWebSkyBlue;
R2 := Rect(B.Width + TrackBar2.Position, (C.Height - R1.Bottom) div 2, TrackBar1.Position, C.Height);
Form1.Canvas.TextRect(R2, S, [tfWordBreak, tfNoClip, tfCenter]);

Form1.Canvas.Draw((TrackBar1.Position - R1.Right - TrackBar2.Position) div 2, (C.Height - B.Height) div 2, B);
end;

//right
if RadioGroup1.ItemIndex = 1 then
begin
R1 := Rect(B.Width + TrackBar2.Position, 0, TrackBar1.Position, C.Height);
Form1.Canvas.TextRect(R1, S, [tfWordBreak, tfNoClip, tfCenter, tfCalcRect]);

Form1.Canvas.Brush.Color := clWebSkyBlue;
R2 := Rect(0, (C.Height - R1.Bottom) div 2, TrackBar1.Position - B.Width - TrackBar2.Position, C.Height);
Form1.Canvas.TextRect(R2, S, [tfWordBreak, tfNoClip, tfCenter]);

Form1.Canvas.Draw((TrackBar1.Position + R1.Right - (B.Width*2) + TrackBar2.Position) div 2, (C.Height - B.Height) div 2, B);
end;

//top
if RadioGroup1.ItemIndex = 2 then
begin
R1 := Rect(0, B.Height + TrackBar2.Position, TrackBar1.Position, C.Height);
Form1.Canvas.TextRect(R1, S, [tfWordBreak, tfNoClip, tfCenter, tfCalcRect]);

G := B.Height + R1.Bottom - R1.Top;
D := (C.Height - G) div 2;

Form1.Canvas.Brush.Color := clWebSkyBlue;
R2 := Rect(0, D + TrackBar2.Position, TrackBar1.Position, C.Height);
Form1.Canvas.TextRect(R2, S, [tfWordBreak, tfNoClip, tfCenter]);

Form1.Canvas.Draw((TrackBar1.Position - B.Width) div 2, D - TrackBar2.Position, B);
end;

//bottom
if RadioGroup1.ItemIndex = 3 then
begin
R1 := Rect(0, B.Height + TrackBar2.Position, TrackBar1.Position, C.Height);
Form1.Canvas.TextRect(R1, S, [tfWordBreak, tfNoClip, tfCenter, tfCalcRect]);

G := B.Height + R1.Bottom - R1.Top;
D := (C.Height + G) div 2;

Form1.Canvas.Brush.Color := clWebSkyBlue;
R2 := Rect(0, D - R1.Bottom - R1.Top + B.Height + TrackBar2.Position, TrackBar1.Position, C.Height);
Form1.Canvas.TextRect(R2, S, [tfWordBreak, tfNoClip, tfCenter]);

Form1.Canvas.Draw((TrackBar1.Position - B.Width) div 2, D + TrackBar2.Position, B);
end;

FreeAndNil(B);
end;

如何使用此代码:将两个 TrackBar 放到 Form 上,并将第一个 TrackBar 的最大值设置为 242,第二个设置为 10;将 TRadioGroup 放到 Form 上,并按照下一个顺序在其 Items 属性中设置 4 个项目:左、右、上、下。

因此,当选择“左侧”或“右侧”项目时,代码可以正确工作(正确 - 正如我所期望的),但是当选择“顶部”或“底部”项目时,字形和文本绘制不正确。我的意思是,例如,我们选择项目“顶部”并单击 Button1。我们看到了什么?右图,Glyph 绘制在 Text 上(更多详细信息请参见图片)。不幸的是,我试图克服这一点,但没有成功:

Top glyph's issue

那么,有什么方法可以改进这段代码吗?或者我应该用另一种方式来达到我的目标?

附注抱歉我的英语不好;)

编辑更新源代码。

最佳答案

您对顶部和底部的计算是错误的。并且您可以直接在位图 Canvas 上而不是在表单 Canvas 上绘图。这是修复后的版本:

  //top
if RadioGroup1.ItemIndex = 2 then
begin
R1 := Rect(0, B.Height + TrackBar2.Position, TrackBar1.Position, C.Height);
C.Canvas.TextRect(R1, S, [tfWordBreak, tfNoClip, tfCenter, tfCalcRect]);
G := (C.Height div 2);
// D is top for centered text rec + glyph size and spacing
D := G + ((R1.Height + B.Height + Trackbar2.Position) div 2);
C.Canvas.Brush.Color := clWebSkyBlue;
R2 := Rect(0, D - R1.Height, TrackBar1.Position, D+R1.Height);
C.Canvas.TextRect(R2, S, [tfWordBreak, tfNoClip, tfCenter]);
C.Canvas.Draw((TrackBar1.Position - B.Width) div 2, R2.Top - B.Height - TrackBar2.Position, B);
end;

//bottom
if RadioGroup1.ItemIndex = 3 then
begin
R1 := Rect(0, 0, TrackBar1.Position, C.Height);
C.Canvas.TextRect(R1, S, [tfWordBreak, tfNoClip, tfCenter, tfCalcRect]);
// G is absolute middle of draw canvas
G := (C.Height div 2);
// D is top for centered text rec + glyph size and spacing
D := G - ((R1.Height + B.Height + Trackbar2.Position) div 2);
C.Canvas.Brush.Color := clWebSkyBlue;
R2 := Rect(0, D, TrackBar1.Position, D + R1.Height);
C.Canvas.TextRect(R2, S, [tfWordBreak, tfNoClip, tfCenter]);
C.Canvas.Draw((TrackBar1.Position - B.Width) div 2, R2.Bottom + TrackBar2.Position, B);
end;

关于delphi - 将字形和 WordWrap 文本一起绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33806534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com