GDI+ 我也不太懂......下面是个测试程序,最大的毛病就是显示不了普通控件了
RGN你是怎么做的? 我一般都是从一副图像里来获得窗口范围的
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,pngimage, StdCtrls, gdipapi, gdipobj, ExtCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
procedure FormMouseMove(Sender: TObject;
Shift: TShiftState;
X,
Y: Integer);
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
ox,oy:integer;
public
{ Public declarations }
end;
const
WS_EX_LAYERED = $80000;
LWA_COLORKEY = 1;
LWA_ALPHA = 2;
ULW_COLORKEY = 1;
ULW_ALPHA = 2;
ULW_OPAQUE = 4;
var
Form1: TForm1;
dc:hdc;
Function UpdateLayeredWindow(hWnd : HWND;
hdcDst : HDC;
pptDst : PPoint;
psize : PSize;
hdcSrc : HDC;
pptSrc : PPoint;
crKey : COLORREF;
pblend : PBlendFunction;
dwFlags : DWORD): BOOL;
stdcall;
implementation
{$R *.dfm}
Function UpdateLayeredWindow;
external 'user32.dll';
procedure DrawAlphiPic(AHandle:HWND;Pic:string;ACanvas:TCanvas;var ARect:Trect);
var
pt1, pt2 : TPoint;
sz : TSize;
bf : TBlendFunction;
bmp : HBITMAP;
gpbmp:tgpbitmap;
begin
SetWindowLong(AHandle, GWL_EXSTYLE, GetWindowLong(AHandle, GWL_EXSTYLE) or WS_EX_LAYERED);
gpbmp:=tgpbitmap.Create(Pic);
try
pt1 := Point(ARect.Left, ARect.Top);
//窗口做上角的坐标
pt2 := Point(0, 0);
sz.cx := gpbmp.GetWidth;
sz.cy := gpbmp.GetHeight;
ARect.Right:=ARect.Left+sz.cx;
ARect.Bottom:=ARect.Top+sz.cy;
bf.BlendOp := AC_SRC_OVER;
bf.BlendFlags := 0;
bf.SourceConstantAlpha := $ff;
bf.AlphaFormat := AC_SRC_ALPHA;
//-----------------------------------
gpbmp.GetHBITMAP(0,bmp);
finally
gpbmp.Free;
end;
DC := CreateCompatibleDC(ACanvas.Handle);
SelectObject(DC, bmp);
UpdateLayeredWindow(AHandle, ACanvas.Handle, @pt1, @sz, DC, @pt2,0, @bf,ULW_ALPHA);
DeleteDC(dc);
DeleteObject(bmp);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
tmprect:trect;
w,h:integer;
begin
tmprect:=BoundsRect;
DrawAlphiPic(Handle,extractfilepath(application.ExeName)+'test.png',Canvas,tmprect);
w:=tmprect.Right-tmprect.Left;
h:=tmprect.Bottom-tmprect.Top;
SetBounds(tmprect.Left,tmprect.Top,w,h);
end;
procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
ox:=mouse.CursorPos.X;
oy:=mouse.CursorPos.Y;
end;
procedure TForm1.FormMouseMove(Sender: TObject;
Shift: TShiftState;
X,
Y: Integer);
begin
if ssleft in shift then
begin
left:=left+mouse.CursorPos.X-ox;
top:=top+mouse.CursorPos.Y-oy;
ox:=mouse.CursorPos.X;
oy:=mouse.CursorPos.Y;
end;
end;
procedure TForm1.FormDblClick(Sender: TObject);
var
tmprect:trect;
w,h:integer;
begin
if OpenDialog1.Execute then
begin
tmprect:=BoundsRect;
DrawAlphiPic(Handle,OpenDialog1.FileName,Canvas,tmpRect);
w:=tmprect.Right-tmprect.Left;
h:=tmprect.Bottom-tmprect.Top;
SetBounds(tmprect.Left,tmprect.Top,w,h);
end;
end;
end.