option explicit
'------------------ structure ------------------------------------------------
type bitmap
bmtype as long
bmwidth as long
bmheight as long
bmwidthbytes as long
bmplanes as integer
bmbitspixel as integer
bmbits as long
end type
type mycolor24
b as byte
g as byte
r as byte
end type
type mycolor32
b as byte
g as byte
r as byte
a as byte
end type
'------------------ api ------------------------------------------------------
declare function getbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long
declare function setbitmapbits lib "gdi32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long
declare function getobjectapi& lib "gdi32" alias "getobjecta" (byval hobject as long, byval ncount as long, lpobject as any)
'----------------- global function -------------------------------------------
'功能:将前景位图经掩模运算后与背景混合,然后按整体透明度复制到目的位图。
'引用:(type)bitmap,(type)mycolor24,(api)getbitmapbits,(api)setbitmapbits,(api)getobjectapi
'说明:如果未指定hfsourcebmp(前景),掩模将失去作用;如果未指定hmaskbmp(掩模),前景将完全覆盖背景;如果未指定alpharate(透明度),将不做整体透明处理
'参数:
' hdestbmp 目的位图句柄
' dx 目的x坐标
' dy 目的y坐标
' dw 目的宽度
' dh 目的高度
' hbsourcebmp 背景位图句柄
' bsx 背景x坐标
' bsy 背景y坐标
' hfsourcebmp 前景位图句柄
' fsx 前景x坐标
' fsy 前景y坐标
' hmaskbmp 掩模位图句柄
' mx 掩模x坐标
' my 掩模y坐标
' alpha 整体透明度
sub abimage(byval hdest as long, byval dx as long, byval dy as long, byval dw as long, byval dh as long, byval hbsource as long, byval bsx as long, byval bsy as long, byval hfsource as long, byval fsx as long, byval fsy as long, byval hmask as long, byval mx as long, byval my as long, byval alpharate as byte)
'各位图的信息
dim dinfo as bitmap, bsinfo as bitmap, fsinfo as bitmap, minfo as bitmap
'各位图的数据
dim ddata() as mycolor24, bsdata() as mycolor24, fsdata() as mycolor24, mdata() as mycolor24
dim ddata32() as mycolor32, bsdata32() as mycolor32, fsdata32() as mycolor32, mdata32() as mycolor32
'x,y=循环变量;w,h=循环次数;offsetx,offsety=相对于目的位图坐标偏移
dim x as long, y as long, w as long, h as long, offsetx as long, offsety as long
'混合比率
dim rate as single
'得到各位图的信息
if getobjectapi(hdest, len(dinfo), dinfo) = 0 then exit sub
if getobjectapi(hbsource, len(bsinfo), bsinfo) = 0 then exit sub
if getobjectapi(hfsource, len(fsinfo), fsinfo) = 0 then exit sub
if getobjectapi(hmask, len(minfo), minfo) = 0 then exit sub
'确定循环次数及偏移
offsetx = iif(dx > 0, dx, 0)
offsety = iif(dy > 0, dy, 0)
if offsetx >= dinfo.bmwidth or offsety >= dinfo.bmheight then exit sub
if bsx > bsinfo.bmwidth or bsy > bsinfo.bmheight or fsx > fsinfo.bmwidth or fsy > fsinfo.bmheight or mx > minfo.bmwidth or my > minfo.bmheight then exit sub '保证范围有效
w = iif(dx >= 0, dw, dw + dx)
w = iif(offsetx + w > dinfo.bmwidth, dinfo.bmwidth - offsetx, w)
w = iif(bsinfo.bmwidth - bsx > w, w, bsinfo.bmwidth - bsx)
w = iif(fsinfo.bmwidth - fsx > w, w, fsinfo.bmwidth - fsx)
w = iif(minfo.bmwidth - mx > w, w, minfo.bmwidth - mx)
h = iif(dy >= 0, dh, dh + dy)
h = iif(offsety + h > dinfo.bmheight, dinfo.bmheight - offsety, h)
h = iif(bsinfo.bmheight - bsy > h, h, bsinfo.bmheight - bsy)
h = iif(fsinfo.bmheight - fsy > h, h, fsinfo.bmheight - fsy)
h = iif(minfo.bmheight - my > h, h, minfo.bmheight - my)
if w <= 0 or h <= 0 then exit sub '保证范围有效
'如果为24位位图
if dinfo.bmbitspixel = 24 and bsinfo.bmbitspixel = 24 and fsinfo.bmbitspixel = 24 and minfo.bmbitspixel = 24 then
'得到各位图数据
redim ddata(0) as mycolor24, bsdata(0) as mycolor24, fsdata(0) as mycolor24, mdata(0) as mycolor24
redim ddata(dinfo.bmwidth - 1, dinfo.bmheight - 1) as mycolor24
getbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 3, ddata(0, 0)
redim bsdata(bsinfo.bmwidth - 1, bsinfo.bmheight - 1) as mycolor24
getbitmapbits hbsource, bsinfo.bmwidth * bsinfo.bmheight * 3, bsdata(0, 0)
redim fsdata(fsinfo.bmwidth - 1, fsinfo.bmheight - 1) as mycolor24
getbitmapbits hfsource, fsinfo.bmwidth * fsinfo.bmheight * 3, fsdata(0, 0)
redim mdata(minfo.bmwidth - 1, minfo.bmheight - 1) as mycolor24
getbitmapbits hmask, minfo.bmwidth * minfo.bmheight * 3, mdata(0, 0)
'处理数据
for y = 0 to h - 1
for x = 0 to w - 1
rate = (clng(mdata(x + mx, y + my).r) + mdata(x + mx, y + my).g + mdata(x + mx, y + my).b) / 765 '\3/255 灰度化掩模
ddata(x + dx, y + dy).r = fsdata(x + fsx, y + fsy).r + (clng(bsdata(x + bsx, y + bsy).r) - fsdata(x + fsx, y + fsy).r) * rate 'alpha混合
ddata(x + dx, y + dy).g = fsdata(x + fsx, y + fsy).g + (clng(bsdata(x + bsx, y + bsy).g) - fsdata(x + fsx, y + fsy).g) * rate 'alpha混合
ddata(x + dx, y + dy).b = fsdata(x + fsx, y + fsy).b + (clng(bsdata(x + bsx, y + bsy).b) - fsdata(x + fsx, y + fsy).b) * rate 'alpha混合
next x
next y
'返回数据
setbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 3, ddata(0, 0)
erase ddata, bsdata, fsdata, mdata
'如果为32位位图
elseif dinfo.bmbitspixel = 32 and bsinfo.bmbitspixel = 32 and fsinfo.bmbitspixel = 32 and minfo.bmbitspixel = 32 then
'得到各位图数据
redim ddata32(0) as mycolor32, bsdata32(0) as mycolor32, fsdata32(0) as mycolor32, mdata32(0) as mycolor32
redim ddata32(dinfo.bmwidth - 1, dinfo.bmheight - 1) as mycolor32
getbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 4, ddata32(0, 0)
redim bsdata32(bsinfo.bmwidth - 1, bsinfo.bmheight - 1) as mycolor32
getbitmapbits hbsource, bsinfo.bmwidth * bsinfo.bmheight * 4, bsdata32(0, 0)
redim fsdata32(fsinfo.bmwidth - 1, fsinfo.bmheight - 1) as mycolor32
getbitmapbits hfsource, fsinfo.bmwidth * fsinfo.bmheight * 4, fsdata32(0, 0)
redim mdata32(minfo.bmwidth - 1, minfo.bmheight - 1) as mycolor32
getbitmapbits hmask, minfo.bmwidth * minfo.bmheight * 4, mdata32(0, 0)
'处理数据
for y = 0 to h - 1
for x = 0 to w - 1
rate = ((255 - clng(mdata32(x + mx, y + my).r)) + (255 - clng(mdata32(x + mx, y + my).g)) + (255 - clng(mdata32(x + mx, y + my).b))) / 765 '\3/255 灰度化掩模
ddata32(x + dx, y + dy).r = fsdata32(x + fsx, y + fsy).r + (clng(bsdata32(x + bsx, y + bsy).r) - fsdata32(x + fsx, y + fsy).r) * rate 'alpha混合
ddata32(x + dx, y + dy).g = fsdata32(x + fsx, y + fsy).g + (clng(bsdata32(x + bsx, y + bsy).g) - fsdata32(x + fsx, y + fsy).g) * rate 'alpha混合
ddata32(x + dx, y + dy).b = fsdata32(x + fsx, y + fsy).b + (clng(bsdata32(x + bsx, y + bsy).b) - fsdata32(x + fsx, y + fsy).b) * rate 'alpha混合
next x
next y
'返回数据
setbitmapbits hdest, dinfo.bmwidth * dinfo.bmheight * 4, ddata32(0, 0)
erase ddata32, bsdata32, fsdata32, mdata32
end if
end sub