漂浮术和动态透明漂浮物素材术是怎麽回事

Windows中半透明技术的原理及应用 - 简书
Windows中半透明技术的原理及应用
WINDOWS中半透明技术的原理及应用
摘要:探究Windows下的半透明技术的实现原理,技术细则,实现方法,其用户体验意义和更多应用。
半透明技术
实现 WINDOWS
一.基本概念:有一些屏幕翻译软件为了不影响用户操作其他软件,通常将显示翻译结果的窗体设置为半透明状。这就是半透明技术的一个显著应用。要实现半透明窗体的效果,需要使用多个API函数,下面简单介绍这些API函数。
1.SetLayeredWindowAttributes函数
使用该函数设置窗体的透明效果。该函数的原型如下:
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
函数中各参数的含义如下面所述。
hWnd:是透明窗体的句柄,通过API函数FindWindow取得指定窗体的句柄;
crKey:为颜色值;
bAlpha:是透明度,取值范围是0~255;
dwFlags:是透明方式,可以取以下两个值。
LWA_ALPHA:值为2。设置为该值时,crKey参数无效,bAlpha参数有效。
LWA_COLORKEY:值为1。设置为该值时,bAlpha参数有效而窗体中的所有颜色为crKey的地方将变为透明。
提示:要使窗体拥有透明效果,首先要使用以下语句定义WS_EX_LAYERED常量:
WS_EX_LAYERED = 0x80000
2.GetWindowLong函数
用该函数能够获得指定窗体的信息,其函数原型如下:
Private Declare Function GetWindowLongLib "user32" Alias "GetWindowLongA" _(ByVal hWnd As Long, ByVal nIndex As Long) As Long
两个参数的含义分别如下面所述。
hWnd:指定窗体的句柄;
nIndex:需要获得的信息的类型。该参数可设置为以下值之一。
GWL_EXSTYLE:得到扩展的窗体风格;
GWL_STYLE:得到窗体风格;
GWL_WNDPROC:得到窗体回调函数的地址,或者句柄。得到后必须使用CallWindowProc函数来调用;
GWL_HINSTANCE:得到应用程序运行实例的句柄;
GWL_HWNDPARENT:得到父窗体的句柄;
GWL_ID:得到窗体的标识符;
GWL_USERDATA:得到和窗体相关联的32位的值(每一个窗体都有一个有意留给创建窗体的应用程序使用的32位的值)。
在设置为透明窗体时,需先使用GetWindowLong函数得到扩展的窗体风格(GWL_ EXSTYLE)。
3.SetWindowLong函数
与GetWindowLong函数对应,SetWindowLong函数用来修改给定窗体的一个属性。该函数的原型如下:
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) AsLong
该函数前两个参数与GetWindowLong相同,最后的参数dwNewLong为修改窗体的属性值。
二.制作透明窗体的过程
了解以上API函数后,就可以在VBE中开始制作透明窗体了。具体步骤如下:
(1)在Excel中按快捷键Alt+F11进入VBE。
(2)单击菜单【插入】|【用户窗体】命令,增加一个用户窗体。向用户窗体中添加1个按钮,设置用户窗体及按钮控件的属性(3)双击窗体空白处打开代码窗体,在声明部分粘贴以下4个API函数的定义。
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
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
(4)在以上API函数中,将使用部分常数,也需要在代码的声明部分进行定义,具体如下:
Private Const WS_EX_LAYERED = &H80000Private Const GWL_EXSTYLE = (-20)Private Const LWA_ALPHA = &H2Private Const LWA_COLORKEY = &H1
(5)在窗体的初始化事件中编写以下代码,调用API函数完成透明窗体的设置:
Private Sub UserForm_Initialize()Dim rtn As Long, hWnd As LonghWnd = FindWindow(0&, Me.Caption)
'得到当前窗体的句柄rtn = GetWindowLong(hWnd, GWL_EXSTYLE) '获取扩展属性rtn = rtn Or WS_EX_LAYEREDSetWindowLong hWnd, GWL_EXSTYLE, rtn '设置扩展属性SetLayeredWindowAttributes hWnd, 0, 100, LWA_ALPHA '设置为透明窗体End Sub
在SetLayeredWindowAttributes函数中,设置参数bAlpha为0,窗体将完全透明,处于看不见的状态。设置参数bAlpha为255,窗体将不透明(与正常窗体一样)。
(6)关闭代码窗体,执行用户窗体,将得到如图29-6所示的半透明窗体效果。窗体处于半透明状,并将下方桌面的图像也显示出来了。
三.半透明窗体意义半透明窗体能够让用户看到当前窗体后面的东西,方便用户操作应用,同时具有朦胧的美感,半透明在Windows7中应用广泛,是Auro的重要特征之一(Authentic(真实)、Energetic(动感)、Reflective(反射)及Open(开阔))
四.半透明技术应用及未来我们注意到Windows 7的新版本中,已经包含了各种半透明效果,诸如文件管理器,应用程序边框等等,而开发者也可以根据新的WindwosAPI来实现这种半透明的效果,在更底层的方面,Windows采用了一个全新的技术----桌面合成(Desktop Composition)。半透明界面是基于的桌面窗口管理器(DWM)的。开启DWM后,所有窗口都有自己独立的缓存来进行渲染,然后由DWM进行管理并获取所有缓存的内容,再通过桌面合成组合出最终的桌面。
Dreamer,http://wdk.pw透明问题。场景中有多个透明物体时 会互相看穿?怎么办
726 || this.offsetHeight>700){if(this.offsetWidth/726 > this.offsetHeight/700){this.width=726;}else{this.height=700;}}" style="max-width:726max-height:700" title="点击查看原图" onclick="if(this.parentNode.tagName!='A') window.open('/forum/attachment/Mon_5_8dc2233bdd91575.jpg');" />前面是一道墙体。材质设置的transparent/diffuse ,房间内水管也是。房子不透明时候(alpha值=255),在房子外面能看见里面透明水管.这个问题有什么方法解决吗?&
要评论请先&或者&
没遇到过这种事```````
也许你可以换个材质试试
Using transparent objects in your game can be tricky, as there are traditional graphical programming problems that can present sorting issues in your game. For example, if you see odd results when looking through two windows at once, you're experiencing the classical problem with using transparency. The general rule is to be aware that there are some cases in which one transparent object may be drawn in front of another in an unusual way, especially if the objects are intersecting, enclose each other or are of very different sizes. For this reason, you should use transparent objects if you need them, and try not to let them become excessive. You should also make your designer(s) aware that such sorting problems can occur, and have them prepare to change some design to work around these issues. &在游戏中使用半透明的物体是比较有技巧性的,因为他们经常会导致排序问题。比如说,如果你透过两个窗口观察物体,发觉物体很异样,恭喜你,你正在经历着每个人都会遇到的使用半透明对象的问题。一条具有普遍性的规则是:有些时候一个半透明的对象会很诡异的绘制在另外一些对象前面,尤其当这些对象是互相交叉的,或者一个把另一个包围的抑或是。为此,当你需要半透明对象的时候,你应该使用它们,但是不要太过于依赖半透明对象(考虑到性能问题)。你也应该让你的设计者们了解到排序可能会出现问题,让他们做好为此调整的准备。
:Using transparent objects in your game can be tricky, as there are traditional graphical programming problems that can present sorting issues in your game. For example, if you see odd results when looking through two windows at once, you're experiencing t .. ( 12:38) 文档里提了可能出现问题。但是却没说怎么解决?谁告诉下
拉开一些。或者分层渲染。dx9下解决不了。dx10以上可以用多张深度图的办法解决。不能交叉。同时按照从距离排序。由远到近
:拉开一些。或者分层渲染。dx9下解决不了。dx10以上可以用多张深度图的办法解决。不能交叉。同时按照从距离排序。由远到近 ( 14:22) 分层渲染 &怎么分?
不知楼主问题解决了没有,我也遇到了这个问题,经过摸索自己搞定了.
问题出在渲染的层次上,同是 transparent所以分不出先后顺序,在某些角度下就会穿插.
楼主可以仔细阅读链接中对于SubShader中 tags 的描述, & &
Tags { &TagName1& = &Value1& &TagName2& = &Value2& }
&实际上 tag 中的 Transparent,Overlay, Geometry等都只代表整数值,渲染器根据这些值来区分渲染的先后顺序,每个类型都在一个整数区间内,所以只需稍微改变 value 即可.本例中可以自己新建一个 shader, 将前面的墙的 shader 中 tag 的值设高一点(Transparent代表3000,则可设为3001).层次区分出来了,则不会出现穿插的问题了!
:不知楼主问题解决了没有,我也遇到了这个问题,经过摸索自己搞定了.
问题出在渲染的层次上,同是 transparent所以分不出先后顺序,在某些角度下就会穿插.
我这是12年的帖子.哈哈。不过还是感谢提供方法。

参考资料

 

随机推荐