delphi根据不同图片生成不规则窗口的实现(仅限于BMP格式)

unit CreateImageForm;

interface
uses
 Windows, SysUtils, Variants, Classes, Graphics;

procedure CreatRgnForm( Bmap: TBitMap; WColor: TColor; hand:THandle);

implementation

procedure CreatRgnForm( Bmap: TBitMap; WColor: TColor; hand:THandle);
var
 rgn: HRgn;
 dc, cdc: HDC;
 x, y: integer;
 p: Tpoint;
 line: boolean;
 color: Tcolor;
begin
 dc := GetWindowDc(hand);
 cdc := CreateCompatibleDc( dc );
 SelectObject( cdc, Bmap.Handle );
 //WColor := Img.Picture.Bitmap.Canvas.Pixels[0, 0];
 //WColor := GetPixel( cdc, 0, 0 );
 BeginPath( dc );
 for x := 0 to Bmap.Width - 1 do
 begin
   line := false;
   for y := 0 to Bmap.Height - 1 do
   begin
     color := GetPixel( cdc, x, y );
     if not( color=WColor) then
     begin
       if not line then
       begin
         line := true;
         p.X := x;
         p.Y := y;
       end;
     end;

     if ( color=WColor)or( y=Bmap.Height - 1 ) then
     begin
       if line then
       begin
         line := false;
         MoveToEx( dc, p.X, p.Y, nil );
         LineTo(dc, p.X, y );
         LineTo(dc, p.X + 1, y );
         LineTo(dc, p.X + 1, p.Y );
         CloseFigure( dc );
       end;
     end;
   end;
 end;
 EndPath( dc );
 Rgn := PathToRegion( dc );
 ReleaseDc( hand, dc );
 SetWindowRgn( hand , rgn, true );
end;



end.


如此调用就可以了
procedure TForm1.FormCreate(Sender: TObject);
var
 color: TColor;
 w1: TBitMap;
begin
 w1 := TBitMap.Create;
 w1.Assign( Image1.Picture.Bitmap );
 color := w1.Canvas.Pixels[0, 0];
 CreatRgnForm( w1, color, handle);
end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值