我想绘制两个相互叠加的矩形。其中一个我希望它比另一个(B)更小(A),这样我就可以看到后面的那个(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;
答案 0 :(得分:2)
你的颜色反转了(你的订单错误),你的gridCellRect.Left
和gridCellRect.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;