当前位置: 首页 > 图文教程 > 开发语言 > VC++ > Windows 2000/XP中对窗口进行透明化
| Windows 2000/XP中对窗口进行透明化 下载本文示例工程
代码 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_pSetLayeredWindowAttribute |