使任何应用程序透明的Windows 2000/XP

介绍 有许多文章演示了如何使用新的分层 Windows 2000或Windows XP的功能,以创建应用程序 半透明的。本文对此进行了补充,并展示了如何使用它们 特性,使任何应用程序透明,即使你不透明 有资料来源。 使用这个“WinTrans”应用程序,您将能够选择任何当前 运行应用程序,并通过拖放魔杖使其透明 (左上角的图标)在该应用程序的标题栏上。你可以 还可以使用滑块控制透明度。“WinTrans”有一个很好的界面 与SPY非常相似,还演示了如何使用Win32 api定位窗口 在鼠标指针下提取详细信息,比如与其关联的类, 窗口的标题等。 “WinTrans”派上用场,如果你想工作在一个最大化的窗口和 同时监视运行在。中的其他应用程序 背景。 背景 在Windows 2000和Windows XP中,一个新的函数命名为 SetLayeredWindowAttributes已添加到User32.dll。 要使用此函数,应用程序需要设置 窗口样式中的WS_EX_LAYERED (0x00080000)位 使用SetWindowLong函数创建或稍后创建。一旦这个 任何应用程序都可以调用这个函数来传递一个句柄给一个窗口 并使整个窗口或窗口上的特定颜色透明。 该函数接受以下参数。 如果这个值为0,窗口就会完全透明 透明,如果它是255,它就变成不透明DWORD dwFlags:如果这个标志是1,那么只有颜色的col 是透明的。如果是2,那么整个窗口 根据bAlpha值的指示变得透明 使用的代码 我们首先定义主对话框的以下成员变量 类WinTransDlg.h 隐藏,复制Code

bool m_bTracking;   //  will be true when the mouse is 
                    //  being tracked
HWND m_hCurrWnd;    //  Handle to the window over which 
                    //  the mouse was last present
HCURSOR m_hCursor;  //  The wand cursor

我们还定义了一个函数指针,它指向 SetLayeredWindowAttributes函数。函数定义在 User32.dll。 隐藏,复制Code

//  Global definition
typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr, 
              BYTE bAlpha, DWORD dwFlags);
lpfn g_pSetLayeredWindowAttributes;

在对话框的OnInitDialog事件处理程序中,我们得到 地址的SetLayeredWindowAttributes函数和存储 在g_pSetLayeredWindowAttributes。我们还加载魔杖光标 并将句柄存储在m_hCursor中 隐藏,复制Code

BOOL CWinTransDlg::OnInitDialog()
{
    ....
    //  get the function pointer for SetLayeredWindowAttributes 
    //  in 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);

    //  Load the wand cursor
    HINSTANCE hInstResource = AfxFindResourceHandle(
         MAKEINTRESOURCE(IDC_WAND), RT_GROUP_CURSOR);
    m_hCursor = ::LoadCursor( hInstResource, MAKEINTRESOURCE(IDC_WAND) );
    ...
}

然后我们为WM_LBUTTONDOWN, WM_LBUTTONUP和定义处理程序 WM_MOUSEMOVE事件。对于WM_LBUTTONDOWN的处理程序,我们执行以下操作 隐藏,复制Code

void CWinTransDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    ...
    SetCapture();      // make mouse move events to 
                       // be directed to this window
    m_hCurrWnd = NULL; // Currently no window is to be made transparent
    m_bTracking = true;     // set the tracking flag
    ::SetCursor(m_hCursor); // turn the mouse pointer into the wand cursor
    ...
}

对于鼠标移动事件处理程序,使用以下代码 隐藏,复制Code

void CWinTransDlg::OnMouseMove(UINT nFlags, CPoint point)
{
    ...
    if (m_bTracking)
    {
        ...
        //  convert mouse coordinates to screen
        ClientToScreen(&point);
        ...
        //  get the window at the mouse coords
        m_hCurrWnd = ::WindowFromPoint(point);
        ...
        // Show details of the window like class, caption, etc.
        ...
    }
    ...
}

只要在主对话框内的任何地方点击鼠标左键 而不被释放的鼠标指针则会变成魔杖和细节 指针下的窗口将显示在WinTrans对话框中 当按钮被释放时,WM_LBUTTONUP的事件处理程序被调用。 隐藏,复制Code

void CWinTransDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
    ...
    //  stop tracking the mouse
    ReleaseCapture();
    m_bTracking = false;

    //  If the window under the mouse is not of this 
    //  application we toggle its
    //  layer style flag and apply the alpha as set by the slider control
    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);
    }
    ...
}

的兴趣点 这个应用程序只有在魔棒掉落在标题上时才能正常工作 另一个应用程序的工具条或基于对话框的应用程序的主体上。如果因为 魔杖掉落在记事本的主体上,它将不会工作 要移除透明效果,只需再次拖放魔棒即可 在该应用程序。因为在OnLButtonUp中我们切换WS_EX_LAYERED位 透明度效果也将被切换 TransWand在命令窗口中无法工作 历史 这是初始版本。增加了撤消所有按钮和复选框,以清除透明度的效果 所有窗口时,WinTrans关闭 本文转载于:http://www.diyabc.com/frontweb/news12424.html

原文地址:https://www.cnblogs.com/Dincat/p/13474202.html