Delphi - 调整表单大小后重新构建点击透明区域

Delphi - re-build click-through transparent area after form resizing

我正在使用 VCL 测试一些特殊形状的 window。

在(无边框)主窗体上有一个 TImage,我使用它通过利用 TForm.TransparentColorTForm.TransparentColorValue 制作矩形点击透明区域,如下所示:

imgTrans.Canvas.Brush.Color := self.TransparentColorValue; imgTrans.Canvas.FillRect(Rect(0, 0, imgTrans.ClientWidth, imgTrans.ClientHeight));

window的透明区域有效,除了在调整表单大小后,客户端对齐的 TImage 应该调整大小,因此透明区域也应该调整大小,但它没有。

我尝试了几种方法试图使透明区域与其父窗体一起调整大小,但失败了,我试过的方法:

我使用 xe4 并在 Win7 上测试它。

有什么建议吗?谢谢。

TImage.Canvas 属性 直接链接到底层位图图像。当您调整 TImage 控件大小时,您实际上并没有调整其位图的大小。

imgTrans.Picture.Bitmap.Width := imgTrans.Width;
imgTrans.Picture.Bitmap.Height := imgTrans.Height;

我也会使用 imgTrans.Picture.Bitmap.Canvas 而不是 imgTrans.Canvas 来更清楚地说明您的代码在做什么。

imgTrans.Picture.Bitmap.Canvas.Brush.Color := TransparentColorValue;
imgTrans.Picture.Bitmap.Canvas.FillRect(Rect(0, 0, imgTrans.Width, imgTrans.Height));

另一种更简单的方法是使用 TPaintBox 控件而不是 TImage。如果 TPaintBox 被重新对齐,那么它的 paint 方法将被调用并自动绘制适当的区域。这样你也可以避免 TImage Bitmap 一直坐在内存中。

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  PaintBox1.Canvas.Brush.Color := TransparentColorValue;
  PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.Width, PaintBox1.Height));
end;