您的位置:程序门 -> vb ->



如何在vb中利用api使form背景独自透明


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何在vb中利用api使form背景独自透明
发表于:2007-02-10 21:50:53 楼主
这个问题我一直没有好的解决方法,用api实现vb程序窗口form透明是很容易的事情,但是同时会出现一个问题,即form上的button、picture、label也同时透明,我知道在vb.net中可以通过设置参数使form中的某一种色彩透明,形成form背景单独透明的效果,但是在vb6中应该如何取实现呢?

注:不是指程序窗口透明,这个我懂
发表于:2007-02-10 21:57:281楼 得分:0
想让button、label透明很简单,都用label设置label1.backstyle   =1
picture透明做什么,你让图片透明?不知道你的picture里放的是什么
发表于:2007-02-10 22:02:542楼 得分:0
楼上的误会了,我是要让form的背景透明,不是让button之类的透明,举个例子,我在form上放了个swf,并将swf设置为透明,但是后面还有一个form,如果我用api把form也设置为透明的,则form上的swf也会跟着一起透明,我现在需要的是让form看不见,但是form上的所有东西都能看见,这就是我说的form背景单独透明的意思。
发表于:2007-02-11 11:38:033楼 得分:0
option   explicit
private   declare   function   getwindowlong   lib   "user32 "   alias   "getwindowlonga "   (byval   hwnd   as   long,   byval   nindex   as   long)   as   long
private   declare   function   setwindowlong   lib   "user32 "   alias   "setwindowlonga "   (byval   hwnd   as   long,   byval   nindex   as   long,   byval   dwnewlong   as   long)   as   long
private   declare   function   setlayeredwindowattributes   lib   "user32 "   (byval   hwnd   as   long,   byval   crkey   as   long,   byval   balpha   as   byte,   byval   dwflags   as   long)   as   long
const   ws_ex_layered   =   &h80000
const   gwl_exstyle   =   (-20)
const   lwa_alpha   =   &h2
const   lwa_colorkey   =   &h1
dim   sty   as   long
dim   cs   as   integer

private   sub   form_load()
sty   =   getwindowlong(me.hwnd,   gwl_exstyle)
sty   =   sty   or   ws_ex_layered
setwindowlong   me.hwnd,   gwl_exstyle,   sty
setlayeredwindowattributes   me.hwnd,   0,   192,   lwa_alpha
cs   =   255
end   sub

private   sub   timer1_timer()
cs   =   cs   -   5
setlayeredwindowattributes   me.hwnd,   0,   cs,   lwa_alpha
if   cs   <=   15   then
cs   =   255
end   if
end   sub

请观看timer控件中cs改变使窗体透明效果会发生变化.  
发表于: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


发表于:2007-02-11 15:48:545楼 得分:0
代码来自vb编程技巧10000例(源江科技),供参考:

option   explicit
        private   declare   function   setwindowlong   lib   "user32 "   alias   "setwindowlonga "   (byval   hwnd   as   long,   byval   nindex   as   long,   byval   dwnewlong   as   long)   as   long
        private   declare   function   setparent   lib   "user32 "   (byval   hwndchild   as   long,   byval   hwndnewparent   as   long)   as   long
        const   gwl_exstyle   =   (-20)
        const   ws_ex_transparent   =   &h20&
        dim   sj   as   boolean

private   sub   command3_click()
        form1.show
end   sub

private   sub   form_activate()
        setwindowlong   hwnd,   gwl_exstyle,   ws_ex_transparent
end   sub

private   sub   form_load()
        setwindowlong   hwnd,   gwl_exstyle,   ws_ex_transparent
end   sub
private   sub   command1_click()
        print   "hello "
end   sub
         
private   sub   command2_click()
        end
end   sub

private   sub   form_click()
        if   sj   then
                me.windowstate   =   2
        else
                me.windowstate   =   0
        end   if
        sj   =   not   sj
end   sub

发表于:2007-02-11 19:15:586楼 得分:0
楼上的用的api是这些,就是思路不好。应该是从form左上角开始到右下角。
遇到swf(应该是flash控件吧)的地方就不处理其他地方都是透明。
发表于:2007-03-28 16:53:187楼 得分:0
可是这样flash控件还是有一个长方的背景不是透明的
发表于:2007-03-28 17:03:488楼 得分:0
使用api:   setlayeredwindowattributes
定义:
declare   function   setlayeredwindowattributes   lib   "user32.dll "   (byval   hwnd   as   long,   byval   crkey   as   long,   byval   balpha   as   byte,   byval   dwflags   as   long)   as   long

ws_ex_layered   =   0x80000;
lwa_alpha   =   0x2;
lwa_colorkey=0x1  
其中dwflags有lwa_alpha和lwa_colorkey
lwa_alpha被设置的话,通过balpha决定透明度.
lwa_colorkey被设置的话,则指定被透明掉的颜色为crkey,其他颜色则正常显示

因此只要设置lwa_colorkey和crkey,并且将窗体背景色和控件颜色设为   不同的颜色,就可以做到楼主的要求,经实际测试可行
发表于:2007-03-28 17:05:259楼 得分:0
我上次用flashwindow函数的时候,把form1的autodraw属性设置为true,结果除了了窗口标题栏和控件可以看见以外,窗体背景就成了首次show的时候被遮盖的背景了


快速检索

最新资讯
热门点击