S
seeinrain
Unregistered / Unconfirmed
GUEST, unregistred user!
如题,不要告诉我用bmp或jpg格式的图片生成不规则窗体。我用以下方法总是不完美
procedure TForm1.FormCreate(Sender: TObject);
var
Png: TPngObject;
Rect: TRect;
Rgn: HRGN;
begin
Png := TPngObject.Create;
Png.LoadFromFile('bg.png');
Rect.Left := 0;
Rect.Top := 0;
Rect.Right := Rect.Left + Png.Width;
Rect.Bottom := Rect.Top + Png.Height;
c_W := Png.Width;
c_H := Png.Height;
Png.Draw(Image1.Canvas, Rect);
Png.Free;
Width := c_W;
Height := c_H;
//GetValue;
Rgn := CreateRegion;
if Rgn <> 0 then
begin
SetWindowRgn(Handle, Rgn, True);
end;
end;
function TForm1.CreateRegion: HRGN;
var
Rgn, TempRgn: HRGN;
X, Y, BeginY: Integer;
Line: Boolean;
Color, wColor: TColor;
begin
wColor := 16777215;
BeginY := 0;
Rgn := CreateRectRgn(0, 0, 0, 0);
for X := 0 to c_W - 1 do
begin
Line := False;
for Y := 0 to c_H - 1 do
begin
Color := Image1.Canvas.Pixels[X, Y];
if not (Color = wColor) then
begin
if not Line then
begin
Line := True;
BeginY:= Y;
end;
end;
if (Color = wColor) or (Y = c_H - 1) then
begin
if Line then
begin
Line := False;
TempRgn := CreateRectRgn(X, BeginY, X + 1, Y);
CombineRgn(Rgn, Rgn, TempRgn, RGN_OR);
end;
end;
end;
end;
Result := Rgn;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Png: TPngObject;
Rect: TRect;
Rgn: HRGN;
begin
Png := TPngObject.Create;
Png.LoadFromFile('bg.png');
Rect.Left := 0;
Rect.Top := 0;
Rect.Right := Rect.Left + Png.Width;
Rect.Bottom := Rect.Top + Png.Height;
c_W := Png.Width;
c_H := Png.Height;
Png.Draw(Image1.Canvas, Rect);
Png.Free;
Width := c_W;
Height := c_H;
//GetValue;
Rgn := CreateRegion;
if Rgn <> 0 then
begin
SetWindowRgn(Handle, Rgn, True);
end;
end;
function TForm1.CreateRegion: HRGN;
var
Rgn, TempRgn: HRGN;
X, Y, BeginY: Integer;
Line: Boolean;
Color, wColor: TColor;
begin
wColor := 16777215;
BeginY := 0;
Rgn := CreateRectRgn(0, 0, 0, 0);
for X := 0 to c_W - 1 do
begin
Line := False;
for Y := 0 to c_H - 1 do
begin
Color := Image1.Canvas.Pixels[X, Y];
if not (Color = wColor) then
begin
if not Line then
begin
Line := True;
BeginY:= Y;
end;
end;
if (Color = wColor) or (Y = c_H - 1) then
begin
if Line then
begin
Line := False;
TempRgn := CreateRectRgn(X, BeginY, X + 1, Y);
CombineRgn(Rgn, Rgn, TempRgn, RGN_OR);
end;
end;
end;
end;
Result := Rgn;
end;