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

I want to draw 2 rectangles that superimpose on one another with Windows.FillRect

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

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;

你的颜色颠倒了(你画的顺序错了),你的 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;