function TForm1.createregion(wmask: tbitmap; wcolor: tcolor;hcontrol: thandle)
: hrgn;
var
 dc,dc_c:HDC;
 rgn:HRGN;
 x,y:integer;
 coord:TPOINT;
 line:boolean;
 color:Tcolor;
begin
  dc:=getwindowdc(hcontrol);
  dc_c:=createcompatibledc(dc);
  selectobject(dc_c,wmask.handle);
  beginpath(dc);
  for x:=0 to wmask.width-1 do
    begin
      line:=false;
      for y:=0 to wmask.Height-1 do
        begin
          color:=getpixel(dc_c,x,y);
          if not (color=wcolor) then
            begin
              if not line then
                begin
                  line:=true;
                  coord.x:=x;
                  coord.y:=y;
                end;
            end;
          if (color=wcolor) or (y=wmask.Height-1) then
            begin
              if line then
                begin
                  line:=false;
                  movetoex(dc,coord.x,coord.y,nil);
                  lineto(dc,coord.x,y);
                  lineto(dc,coord.x+1,y);
                  lineto(dc,coord.x+1,coord.y);
                  closefigure(dc);
                end;
            end;
         end;
       end;
       endpath(dc);
       rgn:=pathtoregion(dc);
       releasedc(hcontrol,dc);
       result:=rgn;
end;