【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2030|回复: 0

让Windows 2000/XP中的任意窗口透明起来

[复制链接]
发表于 2005-11-23 16:09:04 | 显示全部楼层 |阅读模式
让Windows 2000/XP中的任意窗口透明起来
<></P>&nbsp;
<></P>
<></P>&nbsp;
<></P>简介
<></P>&nbsp;
<></P>  已经有很多的文章展示了如何通过使用新的系统函数在Windows 2000或Windows XP中建立透明窗口的应用程序,本文在此基础上为您展现了一种可以让任意应用程序窗口透明起来的方法,哪怕您根本没有那个应用程序的源程序。
<></P>  使用本文介绍的“WinTrans”程序,您只需把程序中的“魔棒”(程序左上角的那个图标)拖曳到另一个正在运行的程序的标题栏上就可以使它的窗口变得透明。您还可以通过拖到滑动杆来调节透明度。“WinTrans”的界面很像SPY程序的界面,它演示了如何使用Win32 API函数来捕获一个位于鼠标光标下的窗口并获取它的窗口类、窗口标题等信息。
<></P>  当您需要在一个最大化的窗口中工作而同时又需要查看另外一个在后台运行的程序的状态时,您会发现“WinTrans”程序是一个很实用的程序。
<></P>&nbsp;
<></P>背景
<P></P>&nbsp;
<P></P>  在Windows 2000和Windows XP中,User32.dll中添加了一个新的函数,名字是SetLayeredWindowAttributes。如果要在应用程序中使用这个函数,需要在创建窗口时为窗口的window style设置WS_EX_LAYERED(0x00080000)位,也可以在创建窗口后用SetWindowLong函数添加这个位。一旦这个标志位被设定了,我们就可以通过调用SetLayeredWindowsAttributes函数,并把窗口的句柄传给它来使得窗口或窗口上特定的颜色变得透明。这个函数的参数如下:
<P></P>HWND hWnd: 窗口的名柄 COLORREF col: 希望变透明的颜色 BYTE bAlpha: 如果这个值设为0,窗口会变得完全透明。如果设为255,窗口会变得完全不透明。 DWORD dwFlags: 如果这个标志设为1,只有col指定的颜色会变得透明。如果这个标志设为2,则整个窗口会按bAlpha指定的程度变得透明。代码解释
<P></P>&nbsp;
<P></P>  首先,在WinTransDlg.h中的主对话框类中加下如下的成员变量。
<P></P>bool m_bTracking;&nbsp;&nbsp; //&nbsp; 当鼠标被捕捉时会置为true
<P></P>HWND m_hCurrWnd;&nbsp;&nbsp;&nbsp; //&nbsp; 鼠标最后一次指向的窗口的句柄
<P></P>HCURSOR m_hCursor;&nbsp; //&nbsp; 魔棒光标
<P></P>  我们还定义了一个指向SetLayeredWindowAttributes函数的指针。这个函数位于User32.dll中。
<P></P>//&nbsp; 全局定义
<P></P>typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr,
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BYTE bAlpha, DWORD dwFlags);
<P></P>lpfn g_pSetLayeredWindowAttributes;
<P></P>  在OnInitDialog消息响应函数中,我们获取了SetLayeredWindowAttributes函数的地址并把它保存在g_pSetLayeredWindowAttributes变量中。同时,我们还加载了魔棒光标并把它的句柄放在m_hCursor中
<P></P>BOOL CWinTransDlg::OnInitDialog()
<P></P>{
<P></P>&nbsp;&nbsp;&nbsp; ....
<P></P>&nbsp;&nbsp;&nbsp; //&nbsp; 获取User32.dll中SetLayeredWindowAttributes 函数的地址
<P></P>&nbsp;&nbsp;&nbsp; HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL"));
<P></P>&nbsp;&nbsp;&nbsp; g_pSetLayeredWindowAttributes = (lpfn)GetProcAddress(hUser32,
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "SetLayeredWindowAttributes");
<P></P>&nbsp;&nbsp;&nbsp; if (g_pSetLayeredWindowAttributes == NULL)
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AfxMessageBox (
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Layering is not supported in this version of Windows",
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MB_ICONEXCLAMATION);
<P></P>&nbsp;
<P></P>&nbsp;&nbsp;&nbsp; // &nbsp;加载魔棒光标
<P></P>&nbsp;&nbsp;&nbsp; HINSTANCE hInstResource = AfxFindResourceHandle(
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MAKEINTRESOURCE(IDC_WAND), RT_GROUP_CURSOR);
<P></P>&nbsp;&nbsp;&nbsp; m_hCursor = :oadCursor( hInstResource, MAKEINTRESOURCE(IDC_WAND) );
<P></P>&nbsp;&nbsp;&nbsp; ...
<P></P>}
<P></P>  下面定义WM_LBUTTONDOWN, WM_LBUTTONUP和WM_MOUSEMOVE消息的响应函数。WM_LBUTTONDOWN的响应函数如下:
<P></P>void CWinTransDlg::OnLButtonDown(UINT nFlags, CPoint point)
<P></P>{
<P></P>&nbsp;&nbsp;&nbsp; ...
<P></P>&nbsp;&nbsp;&nbsp; SetCapture();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 使鼠标消息发回本窗口
<P></P>&nbsp;&nbsp;&nbsp; m_hCurrWnd = NULL; // 当前没有窗口被设成透明的
<P></P>&nbsp;&nbsp;&nbsp; m_bTracking = true;&nbsp;&nbsp;&nbsp;&nbsp; // 设置鼠标被捕获的标志
<P></P>&nbsp;&nbsp;&nbsp; ::SetCursor(m_hCursor); // 把鼠标光标变成魔棒光标
<P></P>&nbsp;&nbsp;&nbsp; ...
<P></P>}
<P></P>  对于处理鼠标移动消息的响应代码如下
<P></P>void CWinTransDlg::OnMouseMove(UINT nFlags, CPoint point)
<P></P>{
<P></P>&nbsp;&nbsp;&nbsp; ...
<P></P>&nbsp;&nbsp;&nbsp; if (m_bTracking)
<P></P>&nbsp;&nbsp;&nbsp; {
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; 把鼠标坐标转换为屏幕坐标
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ClientToScreen(&amp;point);
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; 获取鼠标位置的光标
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_hCurrWnd = ::WindowFromPoint(point);
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 显示窗口的详细情况,包括窗口类、标题等
<P></P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...
<P></P>&nbsp;&nbsp;&nbsp; }
<P></P>&nbsp;&nbsp;&nbsp; ...
<P></P>}
<P></P>  这样就使得只要在主窗口中按下鼠标按钮不松开就可以使鼠标光标变为魔棒光标,并且光标下的窗口的一些信息就会显示在WinTrans程序的对话框中。
<P></P>  当鼠标按钮松开时,WM_LBUTTONUP消息的响应函数就会被调用。
<P></P>void CWinTransDlg::OnLButtonUp(UINT nFlags, CPoint point)
<P></P>{
<P></P>&nbsp;&nbsp;&nbsp; ...
<P></P>&nbsp;&nbsp;&nbsp; //&nbsp; 停止捕获鼠标
<P></P>&nbsp;&nbsp;&nbsp; ReleaseCapture();
<P></P>&nbsp;&nbsp;&nbsp; m_bTracking = false;
<P></P>&nbsp;
<P></P>&nbsp;&nbsp;&nbsp; //&nbsp; 如果鼠标光标所指的窗口不是本应用程序本身
<P></P>&nbsp;&nbsp;&nbsp; //&nbsp; 就设置它的Layer样式位并按滑动杆的设置值来设置窗口的Alpha值
<P></P>
<P></P>
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-2-6 04:29

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表