function ExtractRegion( Image:TBitmap ): HRGN;
var
W: Integer;
H: Integer;
X: Integer;
Y: Integer;
P: Integer;
C: TColor;
T: TColor;
F: TForm;
L: TLabel;
A: Integer;
Z: Integer;
R: HRGN;
begin
Result := CreateRectRgn(0,0,0,0);
P := 0;
W := Image.Width;
H := Image.Height;
T := Image.TransparentColor and $00FFFFFF;
F := TForm.Create( Self );
F.BorderStyle := bsToolWindow;
F.BorderIcons := [];
F.Caption := 'DMS 初期化中...';
F.ClientWidth := 200;
F.ClientHeight := 40;
F.Position := poDesktopCenter;
L := TLabel.Create( F );
L.Parent := F;
L.Left := (F.ClientWidth - F.Canvas.TextWidth('00% 完了')) div 2;
L.Top := (F.ClientHeight - L.Height ) div 2;
Z := 0;
F.Show;
while (P div W < H) and (not Application.Terminated) do
begin
X := P mod W;
Y := P div W;
C := Image.Canvas.Pixels[X,Y] and $00FFFFFF;
if (C <> T) then
begin
R := CreateRectRgn( X, Y, X+1, Y+1 );
CombineRgn( Result, Result, R, RGN_OR );
DeleteObject( R );
end;
Inc( P );
A := (P*100) div (W*H);
if (A <> Z) then
begin
L.Caption := IntToStr(A) +'% 完了';
Application.ProcessMessages;
Z := A;
end;
end;
F.Release;
end;
function TForm1.GetRegion( ForceInitial:Boolean=False ): HRGN;
var
FN : TFileName;
AEN: TFileName;
BMP: TBitmap;
Rgn: HRGN;
Sz : DWORD;
pRD: PRgnData;
begin
FN := ChangeFileExt( Application.ExeName, '.rgn' );
AEN:= Application.ExeName;
if ForceInitial or not FileExists( FN ) then
begin
BMP := TBitmap.Create;
BMP.Assign( imgChar );
Result := ExtractRegion( BMP );
BMP.Free;
Rgn := ClockWork.GetWindowRectRgn;
CombineRgn( Result, Result, Rgn, RGN_OR );
DeleteObject( Rgn );
Rgn := CreateRectRgn( 0,0,0,0 );
CombineRgn( Rgn, Rgn, Result, RGN_OR );
Sz := GetRegionData( Result, 0, NIL );
pRD := AllocMem( Sz );
GetRegionData( Result, Sz, pRD );
DeleteObject( Rgn );
with TMemoryStream.Create do
begin
Write( pRD^, Sz );
SaveToFile( FN );
Free;
end;
FreeMem( pRD );
end else begin
with TMemoryStream.Create do
begin
LoadFromFile( FN );
Sz := Size;
pRD:= AllocMem( Sz );
Read( pRD^, Sz );
Free;
end;
Result := ExtCreateRegion( NIL, Sz, pRD^ );
FreeMem( pRD );
end;
end;
|