gpt4 book ai didi

delphi - 打印带有背景颜色的 Richedit

转载 作者:行者123 更新时间:2023-12-03 15:36:35 28 4
gpt4 key购买 nike

我正在将文本从delphi TRichedit 控件输出到打印机。有一个背景图像,所以我使用具有此逻辑的 EM_FORMATRANGE ...

myrichedit.Perform(EM_FORMATRANGE, 1, Longint(@Range));

...并且效果很好,只是当渲染文本时,无论 Richedit 的颜色如何,它始终具有白色背景。知道为什么吗?

编辑:来自发布的评论:

Range 是一个RANGEFORMAT,并被分配如下值:

Range.hdc := aCanvas.Handle; 
Range.hdcTarget := aCanvas.Handle;
LogX := GetDeviceCaps(Range.hdc, LOGPIXELSX);
LogY := GetDeviceCaps(Range.hdc, LOGPIXELSY);
Range.rc.Left := x * 1440 div LogX;
Range.rc.Right := (x+re.ClientWidth) * 1440 div LogX; // (1440=twips/inch)
Range.rc.Top := y * 1440 div LogY;
Range.rc.Bottom := 5000 * 1440 div LogY; // Some bigish number
Range.rcPage := Range.rc;
Range.chrg.cpMin := 0;
Range.chrg.cpMax := -1;

最佳答案

我发现解决这个问题的方法是,您可以使用代码设置单个字符的背景(在将文本添加到 Richedit 之前)...

var
Format: CHARFORMAT2;
begin
...
myrichedit.SelStart:=myrichedit.GetTextLen;
FillChar(Format, SizeOf(Format), 0);
with Format do begin
cbSize := SizeOf(Format);
dwMask := CFM_BACKCOLOR;
crBackColor := charbackgroundcolor;
myrichedit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
end;
myrichedit.SetText:='Hello';

...但要获取整个文本 block 的背景颜色,然后执行此操作来绘制文本...

var
size : Tsize;
Range: TFormatRange;
Rect: TRect;
LogX, LogY : Integer;
bm : tbitmap;
aCanvas : TCanvas;
ExStyle: DWord;
begin
aCanvas:=Printer.Canvas;

Range.hdc := aCanvas.Handle;
Range.hdcTarget := aCanvas.Handle;

LogX := GetDeviceCaps(Range.hdc, LOGPIXELSX);
LogY := GetDeviceCaps(Range.hdc, LOGPIXELSY);

Range.rc.Left := x * 1440 div LogX;
Range.rc.Right := (x+myrichedit.ClientWidth) * 1440 div LogX; // (1440=twips/inch)
Range.rc.Top := y * 1440 div LogY;
Range.rc.Bottom := 5000 * 1440 div LogY; // Some bigish number
Range.rcPage := Range.rc;
Range.chrg.cpMin := 0;
Range.chrg.cpMax := -1;
myrichedit.Perform(EM_FORMATRANGE, 0, Longint(@Range)); // Measure the formatted text
rect:=Range.rc;
rect.Left:=Range.rc.Left * LogX div 1440;
rect.Top:=Range.rc.Top * LogY div 1440;
rect.Right:=Range.rc.Right * LogX div 1440;
rect.Bottom:=Range.rc.Bottom * LogY div 1440;
acanvas.Brush.Color:=myblockcolor;
acanvas.FillRect(rect); // Fill the background rectangle

ExStyle := GetWindowLong(re.Handle, GWL_EXSTYLE); // Draw richedit transparently over coloured area
ExStyle := ExStyle or WS_EX_TRANSPARENT;
SetWindowLong(re.Handle, GWL_EXSTYLE, ExStyle);
myrichedit.Perform(EM_FORMATRANGE, 1, Longint(@Range));
end;

关于delphi - 打印带有背景颜色的 Richedit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9491115/

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