更改bsBDiagonal线宽和间隙

时间:2015-07-16 00:56:42

标签: delphi delphi-xe7

我正在尝试在高DPI和标准DPI环境中生成一致的界面。我们有一个选择框,使用这样的东西涂料:

theCanvas.Brush.style := bsBDiagonal;
theCanvas.pen.style := psClear;
theCanvas.brush.color := clBlue;

是否有任何方法可以更改线条的宽度以及bsBDiagonal绘制的线条之间的间隙,因为这些不考虑显示器的DPI。具有高DPI系统的用户将看到非常精细的对角线非常接近,而具有常规DPI监视器的人将看到更远和更宽的绘画。

例如。左边的那个是普通DPI监视器上的用户将看到的,而右边的那个是高DPI等值。

Hatching

2 个答案:

答案 0 :(得分:1)

Hatch画笔始终在图形设备单元中工作。我以前用打印机遇到了这个问题,并制定了这个程序:

//Fillstep depends linearly on DPI

procedure PrintHatchPolygon(Canvas: TCanvas; Pts: array of TPoint;
  FillStep: Integer);
var
  ClipRgn: HRGN;
  r: TRect;
  i, MaxSize, OldPenColor, HatchStyle: Integer;

  procedure Line(X1, Y1, X2, Y2: Integer);
  begin
    Canvas.MoveTo(X1, Y1);
    Canvas.LineTo(X2, Y2);
  end;

begin
  case Canvas.Brush.Style of
    bsVertical:
      HatchStyle := 1;
    bsHorizontal:
      HatchStyle := 2;
    bsFDiagonal:
      HatchStyle := 4;
    bsBDiagonal:
      HatchStyle := 8;
    bsCross:
      HatchStyle := 3;
    bsDiagCross:
      HatchStyle := 12;
  else
    HatchStyle := 0;
  end;
  OldPenColor := Canvas.Pen.Color;
  Canvas.Pen.Color := Canvas.Brush.Color;
  ClipRgn := CreatePolygonRgn(Pts, High(Pts) + 1, ALTERNATE);
  GetRgnBox(ClipRgn, r);
  MaxSize := r.Bottom - r.Top;
  if MaxSize < (r.Right - r.Left) then
    MaxSize := r.Right - r.Left;
  SelectClipRgn(Canvas.Handle, ClipRgn);
  with r do begin
    if (HatchStyle and 1) > 0 then
      for i := 1 to (r.Right - r.Left) div FillStep do
        Line(Left + i * FillStep, Top, Left + i * FillStep, Bottom);
    if (HatchStyle and 2) > 0 then
      for i := 1 to (r.Bottom - r.Top) div FillStep do
        Line(Left, Top + i * FillStep, Right, Top + i * FillStep);

    //to equalize step
    //FillStep := 1414 * FillStep div 1000;

    if (HatchStyle and 4) > 0 then
      for i := 1 to 2 * MaxSize div FillStep do
        Line(Left, Bottom - i * FillStep, Left + i * FillStep, Bottom);
    if (HatchStyle and 8) > 0 then
      for i := 1 to 2 * MaxSize div FillStep do
        Line(Left, Top + i * FillStep, Left + i * FillStep, Top);
  end;
  SelectClipRgn(Canvas.Handle, 0);
  DeleteObject(ClipRgn);
  Canvas.Pen.Color := OldPenColor;
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  P: array [0 .. 2] of TPoint;
begin
  P[0] := Point(10, 10);
  P[1] := Point(100, 10);
  P[2] := Point(10, 200);
  Canvas.Brush.Style := bsDiagCross;
  Canvas.Brush.Color := clRed;

  //value 8 for usual monitor dpi (72?)
  //value 60 for 600dpi printer
  PrintHatchPolygon(Canvas, P, 8);
  Canvas.Brush.Style := bsClear;
  Canvas.Polygon(P);
end;

答案 1 :(得分:0)

另一种选择是使用自定义画笔。我无法使用透明度来使用自定义画笔选项。

procedure SetupHatchBitmapBrush(ABitmap: TBitmap; const ABrushStyle:
    TBrushStyle; const AFillStep: Integer; const APenColor: TColor);
var
  bitmapSize: TSize;
  rect: TRect;
  cntr: Integer;
  maxSize: Integer;
  oldPenColor: Integer;
  hatchStyle: Integer;

  procedure Line(bBitmap: TBitmap; bX1, bY1, bX2, bY2: Integer);
  begin
    bBitmap.Canvas.MoveTo(bX1, bY1);
    bBitmap.Canvas.LineTo(bX2, bY2);
  end;

begin
  case ABrushStyle of
    bsVertical:   hatchStyle := 1;
    bsHorizontal: hatchStyle := 2;
    bsFDiagonal:  hatchStyle := 4;
    bsBDiagonal:  hatchStyle := 8;
    bsCross:      hatchStyle := 3;
    bsDiagCross:  hatchStyle := 12;
  else
    hatchStyle := 0;
  end;

  oldPenColor := ABitmap.Canvas.Pen.Color;
  try
    ABitmap.Canvas.Pen.Color := APenColor;

    maxSize := ABitmap.Height;
    if maxSize < ABitmap.Width then
      maxSize := ABitmap.Width;
    if (hatchStyle and 1) > 0 then
      for cntr := 1 to ABitmap.Width div AFillStep do
        Line(ABitmap, cntr * AFillStep, 0, cntr * AFillStep, ABitmap.Height);
    if (hatchStyle and 2) > 0 then
      for cntr := 1 to ABitmap.Height div AFillStep do
        Line(ABitmap, 0, cntr * AFillStep, ABitmap.Width, cntr * AFillStep);

    if (hatchStyle and 4) > 0 then
      for cntr := 1 to 2 * maxSize div AFillStep do
        Line(ABitmap, 0, ABitmap.Height - cntr * AFillStep, cntr * AFillStep, ABitmap.Height);
    if (hatchStyle and 8) > 0 then
      for cntr := 1 to 2 * maxSize div AFillStep do
        Line(ABitmap, 0, cntr * AFillStep, cntr * AFillStep, 0);
  finally
    ABitmap.Canvas.Pen.Color := oldPenColor;
  end;
end;

function CreatePatternBitmap(const ABrushStyle: TBrushStyle; const APenColor,
    ABackgroundColor: TColor; const AScaleFactor: Double): TBitmap;
const
  DEFAULT_SIZE = 8;
var
  bitmapStep: Integer;
begin
  bitmapStep := Trunc(DEFAULT_SIZE * AScaleFactor);
  Result := TBitmap.Create;
  Result.Canvas.Brush.Color := clWhite;
  Result.Canvas.Brush.Style := bsSolid;
  Result.PixelFormat := pf32bit;
  Result.SetSize(bitmapStep * 2, bitmapStep * 2);

  SetupHatchBitmapBrush(Result, ABrushStyle, bitmapStep, APenColor);
end;

在某些时候我们需要创建位图。

begin
  FBitmap := CreatePatternBitmap(bsBDiagonal, clRed, clWhite, 1.5);
end;

绘画看起来像这样:

begin
  Canvas.Brush.Color := clBlue;
  Canvas.Pen.Style := psClear;
  Canvas.Brush.style := bsBDiagonal;
  Canvas.Brush.Bitmap := FBitmap;
  Canvas.Rectangle(Rect(10, 10, 100, 100));
end;