如何计算以像素为单位的Windows区域(HRGN)的面积? [英] How to calculate an area of a Windows region (HRGN) in pixels?
本文介绍了如何计算以像素为单位的Windows区域(HRGN)的面积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
获取任意Windows区域的最快方法是什么?
What is the fastest way of getting the area of any arbitrary Windows region?
我知道我可以枚举边界矩形的所有点并调用PtInRegion()
函数,但这似乎不是很快.也许您知道一些更快的方法?
I know I can enumerate all points of bounding rectangle and call the PtInRegion()
function but it seems not very fast. Maybe you know some faster way?
推荐答案
调用 GetRegionData
,您将获得组成该区域的非重叠矩形的列表.累加他们的区域,像这样:
When you call GetRegionData
, you'll get a list of non-overlapping rectangles that make up the region. Add up their areas, something like this:
function GetRegionArea(rgn: HRgn): Cardinal;
var
x: DWord;
Data: PRgnData;
Header: PRgnDataHeader;
Rects: PRect;
Width, Height: Integer;
i: Integer;
begin
x := GetRegionData(rgn, 0, nil);
Win32Check(x <> 0);
GetMem(Data, x);
try
x := GetRegionData(rgn, x, Data);
Win32Check(x <> 0);
Header := PRgnDataHeader(Data);
Assert(Header.iType = rdh_Rectangles);
Assert(Header.dwSize = SizeOf(Header^));
Rects := PRect(Cardinal(Header) + Header.dwSize);
// equivalent: Rects := PRect(@Data.Buffer);
Result := 0;
for i := 0 to Pred(Header.nCount) do begin
Width := Rects.Right - Rects.Left;
Height := Rects.Bottom - Rects.Top;
Inc(Result, Width * Height);
Inc(Rects);
end;
finally
FreeMem(Data);
end;
end;
这篇关于如何计算以像素为单位的Windows区域(HRGN)的面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文