将岛的图片分别分离出来,背景色也就是透明色设置为特殊颜色 根据图片创建一个区域,将一张图片放到一个Panel上,然后根据这个图片,设置Panel的区域(SetWindowRGN),在鼠标点击panel事件中,就可以知道是否点击了图片了。 这个是根据图片创建区域的代码: wMask: 就是岛的图片 wColor: 透明色(背景色) hControl:控件句柄 function Tform1.CreateRegion(wMask:TBitmap;wColor:TColor;hControl:THandle): HRGN; var dc, dc_c: HDC; rgn: HRGN; x, y: integer; coord: TPoint; line: boolean; color: TColor; begin dc := GetWindowDC(hControl); dc_c := CreateCompatibleDC(dc); SelectObject(dc_c, wMask.Handle); BeginPath(dc); for x:=0 to wMask.Width-1 do begin line := false; for y:=0 to wMask.Height-1 do begin color := GetPixel(dc_c, x, y); if not (color = wColor) then begin if not line then begin line := true; coord.x := x; coord.y := y; end; end; if (color = wColor) or (y=wMask.Height-1) then begin if line then begin line := false; MoveToEx(dc, coord.x, coord.y, nil); LineTo(dc, coord.x, y); LineTo(dc, coord.x + 1, y); LineTo(dc, coord.x + 1, coord.y); CloseFigure(dc); end; end; end; end; EndPath(dc); rgn := PathToRegion(dc); ReleaseDC(hControl, dc); Result := rgn; end;
根据图片创建一个区域,将一张图片放到一个Panel上,然后根据这个图片,设置Panel的区域(SetWindowRGN),在鼠标点击panel事件中,就可以知道是否点击了图片了。
这个是根据图片创建区域的代码: wMask: 就是岛的图片
wColor: 透明色(背景色)
hControl:控件句柄
function Tform1.CreateRegion(wMask:TBitmap;wColor:TColor;hControl:THandle): HRGN;
var
dc, dc_c: HDC;
rgn: HRGN;
x, y: integer;
coord: TPoint;
line: boolean;
color: TColor;
begin
dc := GetWindowDC(hControl);
dc_c := CreateCompatibleDC(dc);
SelectObject(dc_c, wMask.Handle);
BeginPath(dc);
for x:=0 to wMask.Width-1 do
begin
line := false;
for y:=0 to wMask.Height-1 do
begin
color := GetPixel(dc_c, x, y);
if not (color = wColor) then
begin
if not line then
begin
line := true;
coord.x := x;
coord.y := y;
end;
end;
if (color = wColor) or (y=wMask.Height-1) then
begin
if line then
begin
line := false;
MoveToEx(dc, coord.x, coord.y, nil);
LineTo(dc, coord.x, y);
LineTo(dc, coord.x + 1, y);
LineTo(dc, coord.x + 1, coord.y);
CloseFigure(dc);
end;
end;
end;
end;
EndPath(dc);
rgn := PathToRegion(dc);
ReleaseDC(hControl, dc);
Result := rgn;
end;
除非你愿意用FLASH等才可以不用楼上的方法,
我也曾经想过有没有方法实现,最后我发现了除了可以用GDI+的算法外只有楼上的方法了
但如果你在提问我相信算法是不敢跟你谈的了