| 发表于:2007-02-11 11:57:444楼 得分:0 |
说明:表单一个form1,图片框一个picshape,在图片框内放置任何图片时,系统将使用图片框中的图片为窗体,并且屏蔽图片中白色部分,从而建立特效的变形窗体。这代码来自vb编程技巧10000例(源江科技),供参考: option explicit dim movetrue as boolean, oldx as long, oldy as long private 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 private declare function getbitmapbits lib "gdi32 " (byval hbitmap as long, byval dwcount as long, lpbits as any) as long private declare function getobject lib "gdi32 " alias "getobjecta " (byval hobject as long, byval ncount as long, lpobject as any) as long private declare function createrectrgn lib "gdi32 " (byval x1 as long, byval y1 as long, byval x2 as long, byval y2 as long) as long private declare function combinergn lib "gdi32 " (byval hdestrgn as long, byval hsrcrgn1 as long, byval hsrcrgn2 as long, byval ncombinemode as long) as long private declare function setwindowrgn lib "user32 " (byval hwnd as long, byval hrgn as long, byval bredraw as long) as long private declare function deleteobject lib "gdi32 " (byval hobject as long) as long private sub fittopicture() const rgn_or = 2 dim border_width as single dim title_height as single dim bm as bitmap dim bytes() as byte dim ints() as integer dim longs() as long dim r as integer dim c as integer dim start_c as integer dim stop_c as integer dim x0 as long dim y0 as long dim combined_rgn as long dim new_rgn as long dim offset as integer dim colourdepth as integer scalemode = vbpixels picshape.scalemode = vbpixels picshape.autoredraw = true picshape.picture = picshape.image ' 获取窗体的边框大小 border_width = (scalex(width, vbtwips, vbpixels) - scalewidth) / 2 title_height = scalex(height, vbtwips, vbpixels) - border_width - scaleheight ' 获取图片大小 x0 = picshape.left + border_width y0 = picshape.top + title_height '给出图片信息 getobject picshape.image, len(bm), bm select case bm.bmbitspixel case 15, 16: 'msgbox _ "图片框中图片的颜色大高。 ",vbexclamation + vbokonly colourdepth = 2 ' 分配空格给图片. redim ints(0 to bm.bmwidthbytes \ 2 - 1, 0 to bm.bmheight - 1) ' 给出图片表面数据 getbitmapbits picshape.image, bm.bmheight * bm.bmwidthbytes, ints(0, 0) ' 建立表单区域 for r = 0 to bm.bmheight - 2 c = 0 do while c < bm.bmwidth start_c = 0 stop_c = 0 ' 查找白色区域,屏蔽 do while c < bm.bmwidth if (ints(c, r) and &h7fff) <> &h7fff then exit do c = c + 1 loop start_c = c do while c < bm.bmwidth if (ints(c, r) and &h7fff) = &h7fff then exit do c = c + 1 loop stop_c = c if start_c < bm.bmwidth then if stop_c > = bm.bmwidth then stop_c = bm.bmwidth - 1 new_rgn = createrectrgn(start_c + x0, r + y0, stop_c + x0, r + y0 + 1) if combined_rgn = 0 then combined_rgn = new_rgn else combinergn combined_rgn, combined_rgn, new_rgn, rgn_or deleteobject new_rgn end if end if loop next r case 24: colourdepth = 3 redim bytes(0 to bm.bmwidthbytes - 1, 0 to bm.bmheight - 1) getbitmapbits picshape.image, bm.bmheight * bm.bmwidthbytes, bytes(0, 0) for r = 0 to bm.bmheight - 2 ' create a region for this row. c = 0 do while c < bm.bmwidth start_c = 0 stop_c = 0 offset = c * colourdepth do while c < bm.bmwidth if bytes(offset, r) <> 255 or _ bytes(offset + 1, r) <> 255 or _ bytes(offset + 2, r) <> 255 then exit do c = c + 1 offset = offset + colourdepth loop start_c = c do while c < bm.bmwidth if bytes(offset, r) = 255 and _ bytes(offset + 1, r) = 255 and _ bytes(offset + 2, r) = 255 _ then exit do c = c + 1 offset = offset + colourdepth loop stop_c = c if start_c < bm.bmwidth then if stop_c > = bm.bmwidth then stop_c = bm.bmwidth - 1 ' 建立区域 new_rgn = createrectrgn(start_c + x0, r + y0, stop_c + x0, r + y0 + 1) if combined_rgn = 0 then combined_rgn = new_rgn else combinergn combined_rgn, combined_rgn, new_rgn, rgn_or deleteobject new_rgn end if end if loop next r case 32: colourdepth = 4 redim longs(0 to bm.bmwidthbytes \ 4 - 1, 0 to bm.bmheight - 1) getbitmapbits picshape.image, bm.bmheight * bm.bmwidthbytes, longs(0, 0) for r = 0 to bm.bmheight - 2 c = 0 do while c < bm.bmwidth start_c = 0 stop_c = 0 do while c < bm.bmwidth if (longs(c, r) and &hffffff) <> &hffffff then exit do c = c + 1 loop start_c = c do while c < bm.bmwidth if (longs(c, r) and &hffffff) = &hffffff then exit do c = c + 1 loop stop_c = c if start_c < bm.bmwidth then if stop_c > = bm.bmwidth then stop_c = bm.bmwidth - 1 new_rgn = createrectrgn(start_c + x0, r + y0, stop_c + x0, r + y0 + 1) if combined_rgn = 0 then combined_rgn = new_rgn else combinergn combined_rgn, combined_rgn, new_rgn, rgn_or deleteobject new_rgn end if end if loop next r case else msgbox "对不起,程序必须在 16位, 24-位 或 32-位 颜色下。 ", _ vbexclamation + vbokonly exit sub end select ' 设置表单外观为建立区域 setwindowrgn hwnd, combined_rgn, true deleteobject combined_rgn end sub private sub form_load() move (screen.width - width) / 2, (screen.height - height) / 2 fittopicture end sub private sub picshape_dblclick() unload me end sub private sub picshape_mousedown(button as integer, shift as integer, x as single, y as single) movetrue = true oldx = x: oldy = y end sub private sub picshape_mousemove(button as integer, shift as integer, x as single, y as single) if movetrue = true then form1.left = form1.left + x - oldx form1.top = form1.top + y - oldy end if end sub private sub picshape_mouseup(button as integer, shift as integer, x as single, y as single) movetrue = false end sub '下面的方法仅适用于windows 2000/xp,因为setlayeredwindowattributes函数在其他系统中不支持。) public sub ntsetfrmrgn(picbox as picturebox, frm as form) '------------------------------------------------- ' 窗体形状及透明度 ' color (取得0,0处象素的颜色,即要裁减的区域的颜色 ' setlayeredwindowattributes 设置透明度及窗体形状 '------------------------------------------------- dim windowexs as long, color as long frm.picture = picbox.picture color = getpixel(picbox.hdc, 0, 0) windowexs = getwindowlong(frm.hwnd, gwl_exstyle) windowexs = windowexs or ws_ex_layered setwindowlong frm.hwnd, gwl_exstyle, windowexs setlayeredwindowattributes frm.hwnd, color, 180, lwa_colorkey or lwa_alpha end sub | | |
|