用法ide
在Windows 2000/XP中,User32.dll增長了一個新函數SetLayeredWindowAttributes。要使用該函數,咱們必須在生成窗口或使用SetWindowLong函數中設置窗口風格WS_EX_LAYERED (0x00080000)。該風格一旦被設置,咱們就能夠調用該函數來透明化窗口。該函數所需參數以下:函數
代碼
首先定義對話框的成員變量(WinTransDlg.h)。post
bool m_bTracking; // 當鼠標被捕捉時設置爲TRUE同時定義一個指向SetLayeredWindowAttributes函數的指針。該函數在User32.dll中定義。
HWND m_hCurrWnd; // 鼠標所在窗口的句柄
HCURSOR m_hCursor; // 棒型光標句柄
// 全局變量在OnInitDialog事件中獲取SetLayeredWindowAttributes函數的指針而且保存在全局變量g_pSetLayeredWindowAttributes中。
typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr,
BYTE bAlpha, DWORD dwFlags);
lpfn g_pSetLayeredWindowAttributes;
BOOL CWinTransDlg::OnInitDialog()而後定義事件 WM_LBUTTONDOWN, WM_LBUTTONUP 和 WM_MOUSEMOVE 的觸發函數. M_LBUTTONDOWN 事件代碼以下:
{
....
// 獲取函數 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) );
...
}
void CWinTransDlg::OnLButtonDown(UINT nFlags, CPoint point)WM_MOUSEMOVE事件處理函數:
{
...
SetCapture(); //鼠標捕獲設置到指定的窗口。在鼠標按鈕按下的時候,這個窗口會爲//當前應用程序或整個系統接收全部鼠標輸入
m_hCurrWnd = NULL; //如今尚未窗口透明
m_bTracking = true; // 設置track標誌
::SetCursor(m_hCursor); // 將光標改成棒形
}
void CWinTransDlg::OnMouseMove(UINT nFlags, CPoint point)
{
...
if (m_bTracking)
{
...
// 獲取鼠標位置
ClientToScreen(&point);
...
// 獲取鼠標下面所在的窗口句柄
m_hCurrWnd = ::WindowFromPoint(point);
...
// 顯示該窗口的類、標題等信息…
...
}
...
}
一旦用鼠標左鍵在窗口內點擊而且不釋放,鼠標的指針將變爲棒形,而且該窗口的信息將顯示在WinTrans窗口上。 當鼠標左鍵被釋放後,事件WM_LBUTTONUP處理函數就被調用。this
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); } ...}