未测试过, 如有问题请告之:
两种方法:
1. 继承TImage生成自定义的TActiveImage
TActiveImage = class(TImage)
procedure CMMSENTER(var Msg: TMessage);message CM_MOUSEENTER;
procedure CMMSLEAVE(var Msg: TMessage);message CM_MOUSELEAVE;
end;
implementation
procedure TActiveImage.CMMSENTER(var Msg: TMessage);
var
bmp: TBitmap;
dc,maskDC: HDC;
oldbmp: HBITMAP;
i,j,k: integer;
p: PByteArray;
begin
inherited;
if (parent=nil) or not visible
or (csDesigning in ComponentState) then
exit;
bmp:=TBitmap.Create;
with bmp do
begin
PixelFormat:=pf24bit;
height:=self.height;
width:=self.width;
end;
DC:=GetDC(parent.handle);
maskDC:=CreateCompatibleDC(DC);
oldbmp:=SelectObject(maskDC, picture.Bitmap.MaskHandle);
BitBlt(bmp.Canvas.Handle, 0, 0, DC, left, top, SRCCOPY);
for i:=0 to bmp.height-1 do
begin
p:=bmp.Scanline;
j:=bmp.width shl 1 + bmp.width; // bmp.width*3
k:=0;
for k:=0 to j do
p[k]:=p[k] shl 1; // red=red*2, blue=blue*2, green=green*2
end;
TransparentStretchBlt(DC, left, top,
width, height, bmp.Canvas.Handle,
0, 0, width, height, maskDC, 0,0);
SelectObject(maskDC, oldbmp);
DeleteDC(maskDC);
ReleaseDC(parent.handle, DC);
bmp.Free;
end;
procedure TActiveImage.CMMSLEAVE(var: Msg: TMessage);
var
DC,maskDC: HDC;
oldbmp: HBITMAP;
begin
inherited;
if (parent=nil) or not visible
or (csDesigning in ComponentState) then
exit;
DC:=GetDC(Parent.Handle);
maskDC:=CreateCompatibleDC(DC);
oldbmp:=SelectObject(maskDC, picture.Bitmap.MaskHandle);
TransparentStretchBlt(DC, left, top,
width, height,
picture.Bitmap.Canvas.Handle,
0, 0, width, height,
maskDC, 0,0);
SelectObject(maskDC, oldbmp);
DeleteDC(maskDC);
ReleaseDC(parent.handle, DC);
end;
2. 在application.OnMessage中判断是否需要激活图象
procedure TForm1.OnApplicationMessage(Msg: TMsg;var handled: boolean);
begin
if (Msg.Wnd=image1) then
if (Msg.message=CM_MOUSEENTER) then
begin
......... (处理方法同上)
end
else if (Msg.message=CM_MOUSELEAVE) then
begin
...... (处理方法同上)
end;
end;