How to calculate an area of a Windows region (HRGN) in pixels?

独自空忆成欢 提交于 2019-12-05 11:57:45

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!