如何在 CreatePolygonRgn 之后释放 HRGN?
How to free HRGN after CreatePolygonRgn?
我在下面完整地写了一个非常简单的方法,它在 TForm
上的所有组件周围创建了一个多边形。由于没有 HRGN.Free
方法,Delphi XE6 的编译器指出,我只是问我是否应该 Free
,好吧,任何东西 里面有,我以前从没用过HRGN
类型,想确定一下。谢谢。
PS: 我只是代表正确释放内存而问,如果这里需要的话,没有别的。
procedure TFormZoom.SetVisibleFormRegion;
var
VisibleRegionPoints: array[0..11] of TPoint;
VisibleFormRegion: HRGN;
begin
try
// XY points around all components that we want to be visible on the FormZoom
VisibleRegionPoints[0] := Point(0, 0);
VisibleRegionPoints[1] := Point(ZoomBoxPanel.Left + ZoomBoxPanel.Width, 0);
VisibleRegionPoints[2] := Point(ZoomBoxPanel.Left + ZoomBoxPanel.Width, ZoomBoxPanel.Height);
VisibleRegionPoints[3] := Point(ZoomBoxPanel.Left, ZoomBoxPanel.Height);
VisibleRegionPoints[4] := Point(ZoomBoxPanel.Left, 0);
VisibleRegionPoints[5] := Point(ColorBoxPanel.Width, 0);
VisibleRegionPoints[6] := Point(ColorBoxPanel.Width, InfoValuesPanel.Top + InfoValuesPanel.Height);
VisibleRegionPoints[7] := Point(0, InfoValuesPanel.Top + InfoValuesPanel.Height);
VisibleRegionPoints[8] := Point(0, InfoValuesPanel.Top);
VisibleRegionPoints[9] := Point(InfoValuesPanel.Width, InfoValuesPanel.Top);
VisibleRegionPoints[10] := Point(ColorBoxPanel.Width, ColorBoxPanel.Height);
VisibleRegionPoints[11] := Point(0, ColorBoxPanel.Height);
try
// we create a polygon region from the above points
VisibleFormRegion := CreatePolygonRgn(VisibleRegionPoints, Length(VisibleRegionPoints), ALTERNATE);
// finally, we set the FormZoom window region to the created polygon
SetWindowRgn(Self.Handle, VisibleFormRegion, True);
finally
VisibleFormRegion.Free; // <-- There is no method called Free!
end;
except
on E: Exception do
begin
// in case of error, we log the exception only
FormMain.ErrorLog.Add('TFormZoom.SetVisibleFormRegion: ' + E.ClassName + ' - ' + E.Message);
end;
end;
end;
如 CreatePolygonRgn()
, the returned handle must be freed with DeleteObject()
的文档中所述:
When you no longer need the HRGN object, call the DeleteObject
function to delete it.
更新
As Remy pointed out: in your case, you're calling SetWindowRgn()
,将区域的所有权转移给系统。这意味着你不需要释放它。
我在下面完整地写了一个非常简单的方法,它在 TForm
上的所有组件周围创建了一个多边形。由于没有 HRGN.Free
方法,Delphi XE6 的编译器指出,我只是问我是否应该 Free
,好吧,任何东西 里面有,我以前从没用过HRGN
类型,想确定一下。谢谢。
PS: 我只是代表正确释放内存而问,如果这里需要的话,没有别的。
procedure TFormZoom.SetVisibleFormRegion;
var
VisibleRegionPoints: array[0..11] of TPoint;
VisibleFormRegion: HRGN;
begin
try
// XY points around all components that we want to be visible on the FormZoom
VisibleRegionPoints[0] := Point(0, 0);
VisibleRegionPoints[1] := Point(ZoomBoxPanel.Left + ZoomBoxPanel.Width, 0);
VisibleRegionPoints[2] := Point(ZoomBoxPanel.Left + ZoomBoxPanel.Width, ZoomBoxPanel.Height);
VisibleRegionPoints[3] := Point(ZoomBoxPanel.Left, ZoomBoxPanel.Height);
VisibleRegionPoints[4] := Point(ZoomBoxPanel.Left, 0);
VisibleRegionPoints[5] := Point(ColorBoxPanel.Width, 0);
VisibleRegionPoints[6] := Point(ColorBoxPanel.Width, InfoValuesPanel.Top + InfoValuesPanel.Height);
VisibleRegionPoints[7] := Point(0, InfoValuesPanel.Top + InfoValuesPanel.Height);
VisibleRegionPoints[8] := Point(0, InfoValuesPanel.Top);
VisibleRegionPoints[9] := Point(InfoValuesPanel.Width, InfoValuesPanel.Top);
VisibleRegionPoints[10] := Point(ColorBoxPanel.Width, ColorBoxPanel.Height);
VisibleRegionPoints[11] := Point(0, ColorBoxPanel.Height);
try
// we create a polygon region from the above points
VisibleFormRegion := CreatePolygonRgn(VisibleRegionPoints, Length(VisibleRegionPoints), ALTERNATE);
// finally, we set the FormZoom window region to the created polygon
SetWindowRgn(Self.Handle, VisibleFormRegion, True);
finally
VisibleFormRegion.Free; // <-- There is no method called Free!
end;
except
on E: Exception do
begin
// in case of error, we log the exception only
FormMain.ErrorLog.Add('TFormZoom.SetVisibleFormRegion: ' + E.ClassName + ' - ' + E.Message);
end;
end;
end;
如 CreatePolygonRgn()
, the returned handle must be freed with DeleteObject()
的文档中所述:
When you no longer need the HRGN object, call the DeleteObject function to delete it.
更新
As Remy pointed out: in your case, you're calling SetWindowRgn()
,将区域的所有权转移给系统。这意味着你不需要释放它。