bool m_bTracking; // 当鼠标被捕捉时设置为TRUE
HWND m_hCurrWnd; // 鼠标所在窗口的句柄
HCURSOR m_hCursor; // 棒型光标句柄
// 全局变量
typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr, BYTE bAlpha, DWORD dwFlags);
lpfn g_pSetLayeredWindowAttributes;
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_SIZEALL), RT_GROUP_CURSOR);
m_hCursor = ::LoadCursor( NULL, MAKEINTRESOURCE(IDC_CROSS) );
void CLayeredWindowAttributesDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture(); //鼠标捕获设置到指定的窗口。在鼠标按钮按下的时候,这个窗口会为//当前应用程序或整个系统接收所有鼠标输入
m_hCurrWnd = NULL; //现在还没有窗口透明
m_bTracking = true; // 设置track标志
::SetCursor(m_hCursor); // 将光标改为棒形
CDialog::OnLButtonDown(nFlags, point);
}
void CLayeredWindowAttributesDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bTracking)
{
// 获取鼠标位置
ClientToScreen(&point);
// 获取鼠标下面所在的窗口句柄
m_hCurrWnd = ::WindowFromPoint(point);
// 显示该窗口的类、标题等信息…
}
CDialog::OnMouseMove(nFlags, point);
}
void CLayeredWindowAttributesDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//释放鼠标捕获
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*/0x00080000);
g_pSetLayeredWindowAttributes(m_hCurrWnd, 0,
(BYTE)m_slider.GetPos(), /*LWA_ALPHA*/2);
::RedrawWindow(m_hCurrWnd, NULL, NULL,
RDW_ERASE | RDW_INVALIDATE |
RDW_FRAME | RDW_ALLCHILDREN);
}
CDialog::OnLButtonUp(nFlags, point);
}