使用PNG實現半透明的窗體(使用GDI+)


  Delphi中標准控件是不支持png圖片的,據說從Window2000后增加 gdiplus.dll庫處理更多的gdi圖像,其中包括png。
  關鍵的幾個api
   GdipCreateBitmapFromFile(),從文件載入圖像(不單只Bitmap)
   GdipCreateBitmapFromStreamICM(),從流中入圖像
  GdipCreateHBITMAPFromBitmap(),獲取圖像的位圖
   GdipDisposeImage(),釋放圖像資源
 
  開始直接調用 GdipCreateBitmapFromFile沒有成功,返回18的錯誤
  查一下資料這個錯誤是: “GdiplusNotInitialized”
  看來必須的初始化gdiplus。
  網上找到一套“ TGPBitmap”相關的組件,封裝了gdiplus的調用。可以參考其中的代碼。
 
  png載入后,再取出其位圖。特別注意,這個位圖是32位的。包括了R、G、B、 Alpha四個色值,其中Alpha就是透明度。UpdateLayeredWindow()API函數可以支持Alpha風格。
 
  如何從流中載入?如何將VCL的流處理成IStream?看看代碼吧。
 
效果圖:

 
准備一張Png圖片,編寫rc文件,然后加入到工程中。
代碼:
CJ7.rc
Png_Cj7 PNG "CJ7.png"
 
CJ7Unit.pas

unit CJ7Unit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TFormCJ7 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormCJ7: TFormCJ7;

implementation

{$R *.dfm}

uses ActiveX;

type
  DebugEventLevel = (
    DebugEventLevelFatal,
    DebugEventLevelWarning
  );
  TDebugEventLevel = DebugEventLevel;

  DebugEventProc = procedure(level: DebugEventLevel; message: PChar); stdcall;

  GdiplusStartupInput = packed record
    GdiplusVersion: Cardinal;
    DebugEventCallback: DebugEventProc;
    SuppressBackgroundThread: BOOL;
    SuppressExternalCodecs: BOOL;
  end;                          
  TGdiplusStartupInput = GdiplusStartupInput;
  PGdiplusStartupInput = ^TGdiplusStartupInput;

  NotificationHookProc = function(out token: ULONG): Integer; stdcall;
  NotificationUnhookProc = procedure(token: ULONG); stdcall;

  GdiplusStartupOutput = packed record
    NotificationHook  : NotificationHookProc;
    NotificationUnhook: NotificationUnhookProc;
  end;
  TGdiplusStartupOutput = GdiplusStartupOutput;
  PGdiplusStartupOutput = ^TGdiplusStartupOutput;

function GdipCreateHBITMAPFromBitmap(bitmap: THandle; out hbmReturn: HBITMAP;
  background: Longword): Integer; stdcall; external 'gdiplus.dll';

function GdipCreateBitmapFromFile(filename: PWChar; out bitmap: THandle): Integer;
  stdcall; external 'gdiplus.dll';

function GdipCreateBitmapFromStreamICM(stream: ISTREAM;
  out bitmap: THandle): Integer; stdcall; external 'gdiplus.dll';

function GdipDisposeImage(image: THandle): Integer; stdcall;
  stdcall; external 'gdiplus.dll';

function GdiplusStartup(out token: ULONG; input: PGdiplusStartupInput;
  output: PGdiplusStartupOutput): Integer; stdcall; external 'gdiplus.dll';

procedure GdiplusShutdown(token: ULONG); stdcall; external 'gdiplus.dll';

procedure TFormCJ7.FormCreate(Sender: TObject);
var
  vGdip: THandle;
  vBitmap: HBITMAP;
  vOldBitmap: HBITMAP;
  vPoint1, vPoint2: TPoint;
  vSize: TSize;
  vBlendFunction: TBlendFunction;
  vDC: HDC;
  vBitmapInfo: TBitmapInfoHeader;
  vDIBSection: TDIBSection;
  vBuffer: PChar;
  vStream: IStream;
  vGlobal: THandle;
begin
  SetWindowLong(Handle, GWL_EXSTYLE,
    GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
   
  ///////Begin 從資源中載入 
  with TResourceStream.Create(HInstance, 'Png_Cj7', 'PNG') do try
    vGlobal := GlobalAlloc(GHND, Size);
    if vGlobal = 0 then Exit;
    vBuffer := GlobalLock(vGlobal);
    if not Assigned(vBuffer) then Exit;
    try
      Read(vBuffer^, Size);
    finally
      GlobalUnlock(vGdip);
    end;
    if CreateStreamOnHGlobal(vGlobal, False, vStream) <> S_OK then Exit;
    if GdipCreateBitmapFromStreamICM(vStream, vGdip) <> S_OK then Exit;
    GlobalFree(vGlobal);
  finally
    Free;
  end;
  ///////End 從資源中載入 

  if GdipCreateHBITMAPFromBitmap(vGdip, vBitmap, 0) <> S_OK then Exit;
 
  vBitmapInfo.biSize := SizeOf(vBitmapInfo);
  GetObject(vBitmap, SizeOf(vDIBSection), @vDIBSection);
  vPoint1 := Point(Left, Top);
  vPoint2 := Point(0, 0);
  vSize.cx := vDIBSection.dsBm.bmWidth;
  vSize.cy := vDIBSection.dsBm.bmHeight;
  vBlendFunction.BlendOp := AC_SRC_OVER;
  vBlendFunction.BlendFlags := 0;
  vBlendFunction.SourceConstantAlpha := $FF; // 透明度
  vBlendFunction.AlphaFormat := AC_SRC_ALPHA; //同上
  vDC := CreateCompatibleDC(Canvas.Handle);
  vOldBitmap := SelectObject(vDC, vBitmap);
  UpdateLayeredWindow(Handle, Canvas.Handle,
    @vPoint1, @vSize, vDC, @vPoint2, 0, @vBlendFunction, ULW_ALPHA);
  SelectObject(vDC, vOldBitmap);
  DeleteDC(vDC);
  DeleteObject(vBitmap);
  GdipDisposeImage(vGdip);
end;

procedure TFormCJ7.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  Perform(WM_SYSCOMMAND, SC_MOVE or HTCLIENT, 0); // 拖動
end;

var
  vStartupInput: TGDIPlusStartupInput;
  vToken: ULONG;

initialization
  vStartupInput.DebugEventCallback := nil;
  vStartupInput.SuppressBackgroundThread := False;
  vStartupInput.SuppressExternalCodecs   := False;
  vStartupInput.GdiplusVersion := 1;
  GdiplusStartup(vToken, @vStartupInput, nil);

finalization
  GdiplusShutdown(vToken);

end.

想了解gdi+的資料可以參考:

http://msdn2.microsoft.com/en-us/library/ms533798.aspx

 

http://blog.csdn.net/zswang/article/details/2119649


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM