使用PNG实现半透明的窗体
来源:互联网 发布:servlet接收数据原理 编辑:程序博客网 时间:2024/05/16 23:56
看来必须的初始化gdiplus。
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
- 使用PNG实现半透明的窗体
- 实现png图片半透明
- win32下gdiplus的使用/实现PNG图片透明窗体
- 编程实现窗体的半透明效果
- Windows Mobile下的窗体半透明实现
- Windows Mobile下的窗体半透明实现
- Windows Mobile下的窗体半透明实现
- MFC下窗体半透明效果的实现
- AlphaBlend实现的透明PNG窗体
- 半透明窗体的制作
- 半透明的窗体
- vc实现异形半透明窗体
- 半透明子窗体MFC实现
- C++实现半透明按钮控件(PNG,GDI+)
- win7上的半透明窗体
- 在VC6.0中用GDI+调用png图片实现半透明渐变的特效窗口
- 二:在VC6.0中用GDI+调用png图片实现半透明渐变的特效窗口
- 在ie6下 能否实现png图片或是背景色的半透明
- 笑侃人生
- asp可用下列函数截取字符串
- 马云对创业者的经典点评
- 时间变量问题
- 领悟 JavaScript 中的面向对象
- 使用PNG实现半透明的窗体
- JAVA2核心技术卷I:基础知识(原书第7版) -- 第6章. 接口和内部类
- 错误信息如下: 统找不到指定的文件。
- JAVA2核心技术卷I:基础知识(原书第7版) -- 第11章. 异常与调试
- 面向接口编程VS《倚天屠龙记》里张三丰教无忌打太极
- JAVA2核心技术卷I:基础知识(原书第7版) -- 第12章. 流与文件
- JAVA2核心技术卷I:基础知识(原书第7版) -- 第13章. 泛型程序设计
- .net面试题
- JAVA2核心技术卷II:高级特性(原书第7版) -- 第1章. 多线程