Mirror

How to extract coordinates from a region (Views: 699)


Problem/Question/Abstract:

I am trying to do regioning backwards. I am writing an application that will read in a bitmap, allow the user to set a transparent colour, and then calculate the point set that would be needed to make that region transparent. I then want to supply the user with the coordinates as a set of coordinates, i.e. I want to extract it from a region format. Why? Because that's how Winamp takes the data in to make its custom shaped forms. But I can't seem to figure out how to pull the data out.

Answer:

One thing I found is that you must create a region prior to GetWindowRgn. I thought that one was created by default. I made a function that does what you need:

procedure TForm1.ShowRgnInfo(Rgn: HRGN);
type
  RgnRects = array[0..1000] of TRect;
  PRgnRect = ^RgnRects;
var
  RgnData: PRgnData;
  Size: DWORD;
  i: Integer;
  R: TRect;
  RgnPtr: PRgnRect;
begin
  Size := GetRegionData(Rgn, 0, nil);
  Memo1.Lines.Add('Size = ' + IntToStr(Size));
  GetMem(RgnData, Size);
  GetRegionData(Rgn, Size, RgnData);
  Memo1.Lines.Add('Number of Rectangles = ' + IntToStr(RgnData.rdh.nCount));
  RgnPtr := @RgnData.Buffer;
  for i := 0 to RgnData.rdh.nCount - 1 do
  begin
    R := RgnPtr[i];
    Memo1.Lines.Add('Rect ' + IntToStr(i));
    Memo1.Lines.Add(IntToStr(R.Left) + ', ' + IntToStr(R.Top) + ', ' +
      IntToStr(R.Right) + ', ' + IntToStr(R.Bottom));
  end;
end;

<< Back to main page