我想用Windows.FillRect绘制两个相互叠加的矩形

时间:2015-01-05 12:20:14

标签: delphi delphi-xe2

我想绘制两个相互叠加的矩形。其中一个我希望它比另一个(B)更小(A),这样我就可以看到后面的那个(B)。

A is in Black and B is in aqua colour

procedure DrawRectangle(drawDC:HDC;cellBrush:TBrush);
var
    gridCellRect, gridCellRect1  :Trect ;   
begin
    gridCellRect.Top := 75;
    gridCellRect.Bottom := 150;
    gridCellRect.Left  := 192;
    gridCellRect.right := 200; 
    SetBkMode(drawDC, OPAQUE);
    cellBrush.color := claqua;

    Windows.FillRect(DrawDC, gridCellRect, cellBrush.Handle);

    gridCellRect1 := gridCellRect;
    // I tried to modify the top position to make it visible
    gridCellRect1.Top := gridCellRect -5; 
    cellBrush.color := clBlack;
    Windows.FillRect(DrawDC, gridCellRect, cellBrush.Handle);
end;

1 个答案:

答案 0 :(得分:2)

你的颜色反转了(你的订单错误),你的gridCellRect.LeftgridCellRect.Right太窄了(8像素),你不需要通话至SetBkMode

此外,你在gridCellRect - 5中有一个错误(甚至不会编译),你永远不会尝试绘制gridCellRect1中定义的矩形,即使它确实如此。 (您对FillRect的第二次通话使用gridCellRect代替gridCellRect1。)

以下是可以帮助您入门的代码的更正版本:

procedure DrawRectangle(drawDC:HDC;cellBrush:TBrush);
var
  gridCellRect, gridCellRect1  :Trect ;
begin
  gridCellRect.Top := 75;
  gridCellRect.Bottom := 150;
  gridCellRect.Left  := 125;    // Changed left and right to widen
  gridCellRect.right := 200;
  cellBrush.color := clBlack;
  Windows.FillRect(DrawDC, gridCellRect, cellBrush.Handle);

  gridCellRect1 := gridCellRect;
  gridCellRect1.Top := gridCellRect.Top + 5;
  gridCellRect1.Bottom := gridCellRect.Bottom - 5;
  cellBrush.color := clAqua;
  Windows.FillRect(DrawDC, gridCellRect1, cellBrush.Handle);
end;

使用

进行测试
procedure TForm1.FormPaint(Sender: TObject);
begin
  DrawRectangle(Canvas.Handle, Canvas.Brush);
end;