如何实现IMAGE控件的半透明效果,想用此做界面特效([阿胡]工作室)

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/5014/5014967.xml?temp=.5859796
    http://community.csdn.net/Expert/topic/5050/5050346.xml?temp=7.743472E-02
      

  2.   

    使用GDI+  + 基于alpha 通道的 png 图片  很容易实现。。可以去看看GDI+的使用
    http://www.2ccc.com/article.asp?articleid=3131
      

  3.   

    type
      TForm1 = class(TForm)
        Image1: TImage;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        OldWndProc: TWndMethod;
        procedure ImageWndProc(var Message: TMessage);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses GDIPOBJ, GDIPAPI;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      OldWndProc := Image1.WindowProc;
      Image1.WindowProc := ImageWndProc;
    end;procedure TForm1.ImageWndProc(var Message: TMessage);
    var
      g: TGpGraphics;
      Image: TGpImage;
      attr: TGpImageAttributes;
      clm: TColorMatrix;
      I: Integer;
    begin
      if (Message.Msg = WM_PAINT) and (Message.WParam <> 0) and
         not Image1.Picture.Bitmap.Empty then
      begin
        FillChar(clm, Sizeof(TColorMatrix), 0);
        for I := 0 to 4 do
          clm[I, I] := 1;
        clm[3, 3] := 0.5;
        attr := TGPImageAttributes.Create;
        try
          attr.SetColorMatrix(clm);
          g := TGpGraphics.Create(HDC(Message.WParam));
          try
            with Image1.Picture.Bitmap do
            Image := TGpBitmap.Create(Handle, Palette);
            try
              g.DrawImage(Image, MakeRect(0, 0, Image1.Width, Image1.Height), 0, 0, Image.GetWidth, Image.GetHeight, UnitPixel, attr);
            finally
              Image.Free;
            end;
          finally
            g.Free;
          end;
        finally
          attr.Free;
        end;
        Exit;
      end;
      OldWndProc(Message);
    end;
      

  4.   

    在0-1范围调整下面这句,0.5半透明
    clm[3, 3] := 0.5;
      

  5.   

    上面没考虑TImage设置问题,改进一下:procedure TForm1.ImageWndProc(var Message: TMessage);
    var
      g: TGpGraphics;
      Image: TGpImage;
      attr: TGpImageAttributes;
      clm: TColorMatrix;
      I, W, H: Integer;
    begin
      if (Message.Msg = WM_PAINT) and (Message.WParam <> 0) and
         not Image1.Picture.Bitmap.Empty then
      begin
        FillChar(clm, Sizeof(TColorMatrix), 0);
        for I := 0 to 4 do
          clm[I, I] := 1;
        clm[3, 3] := 0.5;
        attr := TGPImageAttributes.Create;
        try
          attr.SetColorMatrix(clm);
          g := TGpGraphics.Create(HDC(Message.WParam));
          try
            with Image1.Picture.Bitmap do
            Image := TGpBitmap.Create(Handle, Palette);
            try
              if not Image1.AutoSize and Image1.Stretch then
              begin
                W := Image1.Width;
                H := Image1.Height;
              end else
              begin
                W := Image.Width;
                H := Image.Height;
              end;          
              g.DrawImage(Image, MakeRect(0, 0, W, H), 0, 0, Image.GetWidth, Image.GetHeight, UnitPixel, attr);
            finally
              Image.Free;
            end;
          finally
            g.Free;
          end;
        finally
          attr.Free;
        end;
        Exit;
      end;
      OldWndProc(Message);
    end;