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

99封情书 提交于 2020-01-02 06:39:27

问题


What is the fastest way of getting the area of any arbitrary Windows region?

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?


回答1:


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;


来源:https://stackoverflow.com/questions/11543412/how-to-calculate-an-area-of-a-windows-region-hrgn-in-pixels

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