• Firemonkey的旁门左道[八]


    前段时间,FMX中如何绘制锯齿线条让我苦恼了很长的时间,

    幸好有高手相助,这个问题也顺利的解决了。


    解决方案:

    分别修改
       FMX.Canvas.Mac.pas

       FMX.Canvas.GDIP.pas

    两个单元的代码,   对于D2D的绘制模式时,FMX.Canvas.GD2D.pas还是无能为力

    真的很奇怪,易博龙为什么取消了Canvas.Quality的设置,变成了只读(接口定义的是读写的)所以无法通过正常的流程去修改了。


    function TCanvasQuartz.DoBeginScene(const AClipRects: PClipRects = nil; AContextHandle: THandle = 0): Boolean;
    begin
      if FContext = nil then
      begin
        if (AContextHandle <> 0) then
          FContext := CGContextRef(AContextHandle)
        else if Bitmap <> nil then
        begin
          if Bitmap.Handle <> 0 then
          begin
            if (TQuartzBitmap(Bitmap.Handle).FImage <> nil) then
            begin
              CGImageRelease(TQuartzBitmap(Bitmap.Handle).FImage);
              TQuartzBitmap(Bitmap.Handle).FImage := nil;
            end;
            FContext := CGBitmapContextCreate(TQuartzBitmap(Bitmap.Handle).FData, Bitmap.Width, Bitmap.Height, 8,
              Bitmap.Width * 4, ColorSpace, kCGImageAlphaPremultipliedLast)
          end;
        end else if FPrinter is TPrinterMac then
          PMSessionGetCGGraphicsContext(TPrinterMac(FPrinter).PrintInfo.PMPrintSession, @FContext);
    
        if Assigned(FContext) then
          CGContextSetShouldAntialias(FContext, 0); // 锯齿
      end;
    
      FFontScale := 1;
      if Assigned(FPrinter) and (TPrinterMac(FPrinter).ActivePrinter.ActiveDPIIndex >= 0) then
        FFontScale := TPrinterMac(FPrinter).ActivePrinter.ActiveDPI.X / 96;
    
      Result := inherited DoBeginScene(AClipRects) and (FContext <> nil);
      if Result and (AClipRects <> nil) then
        SetClipRects(AClipRects^);
    end;
    constructor TCanvasGdiPlus.CreateFromWindow(const AParent: TWindowHandle; const AWidth, AHeight: Integer;
      const AQuality: TCanvasQuality = TCanvasQuality.ccSystemDefault);
    begin
      inherited CreateFromWindow(AParent, AWidth, AHeight, AQuality);
      WindowHandleToPlatform(Parent).CreateBuffer(Width, Height);
      FGPGraphics := TGPGraphics.Create(WindowHandleToPlatform(Parent).BufferHandle);
      FGPGraphics.SetPixelOffsetMode(PixelOffsetModeHalf);
    //  case Quality of
    //    TCanvasQuality.ccHighPerformance: FGPGraphics.SetSmoothingMode(SmoothingModeHighSpeed);
    //    TCanvasQuality.ccHighQuality: FGPGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
    //  else
    //    FGPGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
    //  end;
      FGPGraphics.SetSmoothingMode(SmoothingModeHighSpeed); //锯齿
      FGPGraphics.SetInterpolationMode(InterpolationModeHighQuality);
      FGPGraphics.SetTextContrast(TextContrast);
      if GlobalUseGDIPlusClearType then
        FGPGraphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit)
      else
        FGPGraphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
      FGPPen := TGPPen.Create($FF000000);
      FGPPenBrush := TGPSolidBrush.Create($FF000000);
      FGPBrush := TGPSolidBrush.Create($FFFFFFFF);
      FGPFamily := TGPFontFamily.Create('Tahoma');
      FFontScale := 1;
    
    end;
    
    
    constructor TCanvasGdiPlus.CreateFromBitmap(const ABitmap: TBitmap; const AQuality: TCanvasQuality = TCanvasQuality.ccSystemDefault);
    begin
      inherited CreateFromBitmap(ABitmap, AQuality);
      FGPGraphics := TGPGraphics.Create(TGPBitmap(Bitmap.Handle));
      FGPGraphics.SetPixelOffsetMode(PixelOffsetModeHalf);
    //  case Quality of
    //    TCanvasQuality.ccHighPerformance: FGPGraphics.SetSmoothingMode(SmoothingModeHighSpeed);
    //    TCanvasQuality.ccHighQuality: FGPGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
    //  else
    //    FGPGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
    //  end;
      FGPGraphics.SetSmoothingMode(SmoothingModeHighSpeed);; // 锯齿
      FGPGraphics.SetTextContrast(TextContrast);
      if GlobalUseGDIPlusClearType then
        FGPGraphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit)
      else
        FGPGraphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
      FGPPen := TGPPen.Create($FF000000);
      FGPPenBrush := TGPSolidBrush.Create($FF000000);
      FGPBrush := TGPSolidBrush.Create($FFFFFFFF);
      FGPFamily := TGPFontFamily.Create('Tahoma');
      if (Width > 0) and (Height > 0) and not SameValue(FGPGraphics.GetDpiX, 0.0, Epsilon) then
        FFontScale := 96 / FGPGraphics.GetDpiX
      else
        FFontScale := 1;
    end;
    
    procedure TCanvasGdiPlus.SetSize(const AWidth, AHeight: Integer);
    begin
      if Assigned(Parent) and ((AWidth <> Width) or (AHeight <> Height)) then
      begin
        inherited ;
        FreeAndNil(FGPGraphics);
        WindowHandleToPlatform(Parent).ResizeBuffer(Width, Height);
        FGPGraphics := TGPGraphics.Create(WindowHandleToPlatform(Parent).BufferHandle);
    //    FGPGraphics.SetSmoothingMode(SmothingDefault);
        FGPGraphics.SetSmoothingMode(SmoothingModeHighSpeed); //锯齿
        FGPGraphics.SetPixelOffsetMode(PixelOffsetModeHalf);
        FGPGraphics.SetInterpolationMode(InterpolationModeHighQuality);
        FGPGraphics.SetTextContrast(TextContrast);
        if GlobalUseGDIPlusClearType then
          FGPGraphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit)
        else
          FGPGraphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
        FGPPen := TGPPen.Create($FF000000);
        FGPPenBrush := TGPSolidBrush.Create($FF000000);
        FGPBrush := TGPSolidBrush.Create($FFFFFFFF);
        FGPFamily := TGPFontFamily.Create('Tahoma');
        FFontScale := 1;
      end
      else
        inherited ;
    end;



  • 相关阅读:
    文件操作相关utils
    读取excel工具utils
    下载EXCEL文件Utils
    日期操作utils
    常用的utils
    坐标系转换Utils
    C# vs2019 CS0006 编译器错误CS1704
    C# 控制台形式 owin 添加WebApi 和Swagger
    Windows 下 Redis服务自动停止 处理
    Dotfuscator 混淆C# .Net代码 netcore
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3265473.html
Copyright © 2020-2023  润新知