gpt4 book ai didi

delphi - 随着 Alpha 降低,在玻璃背景上绘制的文本变得模糊

转载 作者:行者123 更新时间:2023-12-03 14:41:09 27 4
gpt4 key购买 nike

我正在对 TIceTabSet(Chrome 选项卡)组件进行多项更新。这些变化之一是增加透明度。除了文本之外,一切都很好。随着背景的 Alpha channel 变低,文本变得越来越模糊。这是屏幕截图。

enter image description here

这是绘制选项卡的代码。其中大部分是原始的 TIceTabSet 代码。我只是添加了一些更改以使选项卡透明。对于示例屏幕截图,代码也进行了一些修改。底部的 DrawText 命令是将文本绘制到 Canvas 上的地方。

procedure TIceTabSet.InnerDraw(Canvas: TCanvas; TabRect: TRect; Item: TIceTab);
var
graphics : TGPGraphics;
Pen: TGPPen;
Brush: TGPSolidBrush;
path, linePath: TGPGraphicsPath;
linGrBrush: TGPLinearGradientBrush;
font: TGPFont;
solidBrush: TGPSolidBrush;
rectF: TGPRectF;
stringFormat: TGPStringFormat;
DC: HDC;
marginRight: integer;
iconY, iconX: integer;
textStart: Extended;
startColor, EndColor, textColor, borderColor: cardinal;
borderWidth: Integer;
TabProperties: TIceTabProperties;
Alpha: Byte;
begin
DC := Canvas.Handle;

TabProperties := GetTabProperties(Item);

Alpha := Item.Index * 50;

startColor := MakeGDIPColor(TabProperties.TabStyle.StartColor, Alpha);// TabProperties.TabStyle.Alpha);
endColor := MakeGDIPColor(TabProperties.TabStyle.StopColor, Alpha); //TabProperties.TabStyle.Alpha);
textColor := MakeGDIPColor(TabProperties.Font.Color, 255); //TabProperties.TabStyle.Alpha);
borderColor := MakeGDIPColor(TabProperties.BorderColor, TabProperties.TabStyle.Alpha);
borderWidth := TabProperties.BorderWidth;

graphics := TGPGraphics.Create(DC);
Brush := TGPSolidBrush.Create(borderColor);
Pen:= TGPPen.Create(borderColor);
Font := GetGDIPFont(Canvas, FTabActive.Font); //TabProperties.Font);
try
graphics.SetSmoothingMode(SmoothingModeHighQuality);

pen.SetWidth(borderWidth);

path := TGPGraphicsPath.Create();
try
path.AddBezier(TabRect.Left, TabRect.Bottom, TabRect.Left + FTabShape.LeftEdgeWidth / 2, TabRect.Bottom, TabRect.Left + FTabShape.LeftEdgeWidth / 2, TabRect.Top, TabRect.Left + FTabShape.LeftEdgeWidth, TabRect.Top);
path.AddLine(TabRect.Left + FTabShape.LeftEdgeWidth, TabRect.Top, TabRect.Right - FTabShape.RightEdgeWidth, TabRect.Top);
path.AddBezier(TabRect.Right - FTabShape.RightEdgeWidth, TabRect.Top, TabRect.Right - FTabShape.RightEdgeWidth / 2, TabRect.Top, TabRect.Right - FTabShape.RightEdgeWidth / 2, TabRect.Bottom, TabRect.Right, TabRect.Bottom);

linePath := TGPGraphicsPath.Create;
try
linePath.AddPath(path, false);
path.AddLine(TabRect.Right, TabRect.Bottom, TabRect.Left, TabRect.Bottom);

linGrBrush := TGPLinearGradientBrush.Create(
MakePoint(0, TabRect.Top),
MakePoint(0, TabRect.Bottom),
startColor,
endColor);
try
graphics.DrawPath(pen, linePath);

graphics.FillPath(linGrBrush, path);
finally
linGrBrush.Free;
end;
finally
linePath.Free;
end;
finally
path.Free;
end;

marginRight := 0;

if TabDisplaysCloseButton(Item) then
begin
if (HighLightTabClose = Item) and
(FTabCloseButton.ShowCircle) then
begin
pen.SetWidth(1);

pen.SetColor(MakeGDIPColor(FTabCloseButton.CrossColorHotTrack, 255));
brush.SetColor(MakeGDIPColor(FTabCloseButton.CircleColorHotTrack, 255));

graphics.FillEllipse(brush, TabRect.Right - FTabShape.RightEdgeWidth - 7 - 2,
TabRect.Top + ((TabRect.Bottom - TabRect.Top - 7) div 2) - 3,
(TabRect.Right - FTabShape.RightEdgeWidth) - (TabRect.Right - FTabShape.RightEdgeWidth - 7) + 6,
(TabRect.Top + ((TabRect.Bottom - TabRect.Top + 7) div 2)) - (TabRect.Top + ((TabRect.Bottom - TabRect.Top - 7) div 2)) + 6);

graphics.DrawLine(pen, TabRect.Right - FTabShape.RightEdgeWidth - 5, TabRect.Top + ((TabRect.Bottom - TabRect.Top - 5) div 2),
TabRect.Right - FTabShape.RightEdgeWidth, TabRect.Top + ((TabRect.Bottom - TabRect.Top + 5) div 2));

graphics.DrawLine(pen, TabRect.Right - FTabShape.RightEdgeWidth, TabRect.Top + ((TabRect.Bottom - TabRect.Top - 5) div 2),
TabRect.Right - FTabShape.RightEdgeWidth - 5, TabRect.Top + ((TabRect.Bottom - TabRect.Top + 5) div 2));
end
else
begin
pen.SetWidth(2);

if HighlightTabClose = Item then
pen.SetColor(MakeGDIPColor(FTabCloseButton.CrossColorHotTrack, 255))
else
pen.SetColor(MakeGDIPColor(FTabCloseButton.CrossColorNormal, 255));

graphics.DrawLine(pen, TabRect.Right - FTabShape.RightEdgeWidth - 7, TabRect.Top + ((TabRect.Bottom - TabRect.Top - 7) div 2),
TabRect.Right - FTabShape.RightEdgeWidth, TabRect.Top + ((TabRect.Bottom - TabRect.Top + 7) div 2));

graphics.DrawLine(pen, TabRect.Right - FTabShape.RightEdgeWidth, TabRect.Top + ((TabRect.Bottom - TabRect.Top - 7) div 2),
TabRect.Right - FTabShape.RightEdgeWidth - 7, TabRect.Top + ((TabRect.Bottom - TabRect.Top + 7) div 2));
end;

marginRight := 10;
end;

solidBrush:= TGPSolidBrush.Create(MakeGDIPColor(textColor, 255));

stringFormat:= TGPStringFormat.Create;
stringFormat.SetAlignment(StringAlignmentNear);
stringFormat.SetLineAlignment(StringAlignmentCenter);
stringFormat.SetTrimming(StringTrimmingEllipsisCharacter);
stringFormat.SetFormatFlags(StringFormatFlagsNoWrap);

SelectClipRgn(Canvas.Handle, 0);
textStart := TabRect.Left + FTabShape.LeftEdgeWidth;
iconX := 0;
iconY := 0;

if Assigned(Images) and (Item.ImageIndex <> -1) then
begin
iconY := TabRect.Top + ((TabRect.Bottom - TabRect.Top - Images.Height) div 2);
iconX := Round(textStart);
textStart := textStart + Images.Width + 4;
end;

rectF := MakeRect(textStart, TabRect.Top, TabRect.Right - textStart - FTabShape.RightEdgeWidth - marginRight,
TabRect.Bottom - TabRect.Top);

// ****** Text is drawn here *******
if rectF.Width > 10 then
graphics.DrawString(format('Alpha: %d', [Alpha]), -1, font, rectF, stringFormat, solidBrush);
// *********************************

finally
font.Free;
solidBrush.Free;
Pen.Free;
graphics.Free;
end;

if Assigned(Images) and
(Item.ImageIndex <> -1) then
Images.Draw(Canvas, iconX, iconY, Item.ImageIndex, true);
end;

您可以下载完整源代码 here 。请注意,这是一项正在进行的工作。源码完成后将提交回原作者。

更新1

按照 TLama 的建议更改代码肯定有帮助,但并不能完全解决问题。以下是文本现在的样子:

enter image description here

...Google Chrome 浏览器的外观如下:

enter image description here

更新2

以下是 TextRenderingHintSingleBitPerPixelGridFit 的外观。

enter image description here

我已尝试了所有选项,TextRenderingHintAntiAlias 给出了最佳结果。

最佳答案

正如 Ian Boyd 在他关于 How to draw ClearType text on Aero glass ? 的精彩帖子中所建议的那样当您在玻璃板上渲染文本时,应该应用抗锯齿。因此,要解决您的问题,请尝试按以下方式修改您的代码:

if rectF.Width > 10 then
begin
if (GetParentForm.GlassFrame.Enabled) and (GetParentForm.GlassFrame.SheetOfGlass) then
graphics.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
graphics.DrawString(Item.DisplayCaption, -1, font, rectF, stringFormat, solidBrush);
end;

要模拟您的问题,只需在 Aero 玻璃板上渲染文本就足够了,就像下面的代码以 3 种不同的方式进行操作:

uses
GDIPAPI, GDIPOBJ;

procedure TForm1.FormCreate(Sender: TObject);
begin
Font.Color := clWhite;
GlassFrame.SheetOfGlass := True;
GlassFrame.Enabled := True;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
S: WideString;
GPFont: TGPFont;
GPGraphics: TGPGraphics;
GPSolidBrush: TGPSolidBrush;
GPGraphicsPath: TGPGraphicsPath;
begin
S := 'This is a sample text rendered on the sheet of Aero glass!';
GPFont := TGPFont.Create(Canvas.Handle, Font.Handle);
GPSolidBrush := TGPSolidBrush.Create(MakeColor(GetRValue(Font.Color),
GetGValue(Font.Color), GetBValue(Font.Color)));
GPGraphicsPath := TGPGraphicsPath.Create;
GPGraphicsPath.AddString(S, Length(S), TGPFontFamily.Create(Font.Name),
GPFont.GetStyle, GPFont.GetSize, MakePoint(20.0, 60.0), nil);
try
GPGraphics := TGPGraphics.Create(Canvas.Handle);
try
GPGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
GPGraphics.FillPath(GPSolidBrush, GPGraphicsPath);
GPGraphics.DrawString(S, Length(S), GPFont, MakePoint(20.0, 20.0),
nil, GPSolidBrush);
GPGraphics.SetTextRenderingHint(
TextRenderingHintSingleBitPerPixelGridFit);
GPGraphics.DrawString(S, Length(S), GPFont, MakePoint(20.0, 40.0),
nil, GPSolidBrush);
finally
GPGraphics.Free;
end;
finally
GPFont.Free;
GPSolidBrush.Free;
GPGraphicsPath.Free;
end;
end;

结果如下图所示:

  1. 第一个文本由 DrawString 渲染未启用文本抗锯齿功能
  2. 第二个由 DrawString 渲染启用文本抗锯齿功能,配置为 TextRenderingHintSingleBitPerPixelGridFit模式
  3. 第三个是通过路径填充渲染的,灵感来自 this article平滑模式设置为 SmoothingModeAntiAlias风格

enter image description here

关于delphi - 随着 Alpha 降低,在玻璃背景上绘制的文本变得模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11307509/

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