透明化窗口SetLayeredWindowAttributes
来源:互联网 发布:联瑞汇桔怎么样 知乎 编辑:程序博客网 时间:2024/05/29 19:54
本文示例工程
前言
很多文章示范了使用Windows 2000/XP的层次特性来实现窗口的透明化。本文可以通过该特性使任意窗口透明化,即使你没有该程序的源代码。
使用 "WinTrans" 程序你可以选择任意正在运行的程序,用鼠标左键拖拽左上角框内的棒并将它压在该程序的标题栏上,然后放开,则该程序就可以变成透明。你可以调整滑动条的位置来控制透明度。"WinTrans" 有一个非常象 SPY 的界面,还可以示范Win32 APIs的如下用法:用鼠标指针定位窗口,获取例如类名、标题等窗口信息。
用法
在Windows 2000/XP中,User32.dll增加了一个新函数SetLayeredWindowAttributes。要使用该函数,我们必须在生成窗口或使用SetWindowLong函数中设置窗口风格WS_EX_LAYERED (0x00080000)。该风格一旦被设置,我们就可以调用该函数来透明化窗口。该函数所需参数如下:
- HWND hWnd: 窗口句柄
- COLORREF col: 透明化颜色
- BYTE bAlpha: =0:整个窗口透明, =255 完全不透明
- DWORD dwFlags: =1:仅颜色 col 透明, =2 :窗口按照bAlpha变量进行透明处理。
代码
首先定义对话框的成员变量(WinTransDlg.h)。
bool m_bTracking; // 当鼠标被捕捉时设置为TRUEHWND m_hCurrWnd; // 鼠标所在窗口的句柄HCURSOR m_hCursor; // 棒型光标句柄同时定义一个指向SetLayeredWindowAttributes函数的指针。该函数在User32.dll中定义。
// 全局变量typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr, BYTE bAlpha, DWORD dwFlags);lpfn g_pSetLayeredWindowAttributes;在OnInitDialog事件中获取SetLayeredWindowAttributes函数的指针并且保存在全局变量g_pSetLayeredWindowAttributes中。
BOOL CWinTransDlg::OnInitDialog(){ .... // 获取函数 SetLayeredWindowAttributes 在User32.dll中的指针 HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL")); g_pSetLayeredWindowAttributes = (lpfn)GetProcAddress(hUser32, "SetLayeredWindowAttributes"); if (g_pSetLayeredWindowAttributes == NULL) AfxMessageBox ( "Layering is not supported in this version of Windows", MB_ICONEXCLAMATION); // 装入棒形光标 HINSTANCE hInstResource = AfxFindResourceHandle( MAKEINTRESOURCE(IDC_WAND), RT_GROUP_CURSOR); m_hCursor = ::LoadCursor( hInstResource, MAKEINTRESOURCE(IDC_WAND) ); ...}然后定义事件 WM_LBUTTONDOWN, WM_LBUTTONUP 和 WM_MOUSEMOVE 的触发函数. M_LBUTTONDOWN 事件代码如下:
void CWinTransDlg::OnLButtonDown(UINT nFlags, CPoint point){ ... SetCapture(); //鼠标捕获设置到指定的窗口。在鼠标按钮按下的时候,这个窗口会为//当前应用程序或整个系统接收所有鼠标输入 m_hCurrWnd = NULL; //现在还没有窗口透明 m_bTracking = true; // 设置track标志 ::SetCursor(m_hCursor); // 将光标改为棒形 }WM_MOUSEMOVE事件处理函数:
void CWinTransDlg::OnMouseMove(UINT nFlags, CPoint point){ ... if (m_bTracking) { ... // 获取鼠标位置 ClientToScreen(&point); ... // 获取鼠标下面所在的窗口句柄 m_hCurrWnd = ::WindowFromPoint(point); ... // 显示该窗口的类、标题等信息… ... } ...}
一旦用鼠标左键在窗口内点击并且不释放,鼠标的指针将变为棒形,并且该窗口的信息将显示在WinTrans窗口上。 当鼠标左键被释放后,事件WM_LBUTTONUP处理函数就被调用。
void CWinTransDlg::OnLButtonUp(UINT nFlags, CPoint point){ ... //释放鼠标捕获 ReleaseCapture(); m_bTracking = false; //如果鼠标下面的窗口不是本程序WinTrans,我们就要设置层次样式并且通过设置滑动条来实现透明化。 if (g_pSetLayeredWindowAttributes && m_hCurrWnd != m_hWnd) { ::SetWindowLong(m_hCurrWnd, GWL_EXSTYLE, GetWindowLong(m_hCurrWnd, GWL_EXSTYLE) ^ WS_EX_LAYERED); g_pSetLayeredWindowAttributes(m_hCurrWnd, 0, (BYTE)m_slider.GetPos(), LWA_ALPHA); ::RedrawWindow(m_hCurrWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); } ...}原文作者:abhinaba,再此表示感谢!
- 透明化窗口SetLayeredWindowAttributes
- 透明化窗口SetLayeredWindowAttributes
- 透明化窗口SetLayeredWindowAttributes
- 透明化窗口SetLayeredWindowAttributes[转]
- 用SetLayeredWindowAttributes实现窗口透明
- 使用SetLayeredWindowAttributes API 实现透明窗口
- VC++ SetLayeredWindowAttributes 部分窗口透明鼠标穿透
- 使用SetLayeredWindowAttributes函数设置窗口透明色与透明度
- WinAPI透明窗体SetLayeredWindowAttributes
- (转)xp下实现窗体透明特效/异形窗口SetLayeredWindowAttributes函数
- WinAPI透明窗体SetLayeredWindowAttributes( 转)
- 设置窗口透明度 SetLayeredWindowAttributes函数
- Win32之WinAPI透明窗体SetLayeredWindowAttributes
- VC6.0使用SetLayeredWindowAttributes实现透明效果
- 窗口透明化代码
- 窗口透明化
- 窗口透明化 AlphaBlend
- VC 窗口透明化
- 关于今后的学习的思考
- hdu 4217
- UIIImageView 播放图片动画
- linux_取得路径文件名和目录名
- ssh使用的jar包介绍
- 透明化窗口SetLayeredWindowAttributes
- function object
- Win7系统中Telnet服务的安装和启动
- vi root用户语法高亮不成功的问题
- 深入理解Oracle索引(13):位图连接索引基础介绍
- POJ 2481 Cows
- 如何在java中计算两时间的差
- CentOS6.4下使用默认的文档查看器打开PDF文档乱码的解决方案
- 第8周项目1运算符重载