为什么我的捕获 window 代码不起作用?

Why is my capture window code not working?

我是 winapi 的新手。我已经看到一个例子来捕获桌面,不包括 codeproject

的一些 windows

创建了 child window 并捕获了它。

hwndMag = CreateWindow(WC_MAGNIFIER, TEXT("MagnifierWindow"), 
        WS_CHILD | MS_SHOWMAGNIFIEDCURSOR | WS_VISIBLE,
        0, 0, m_ScreenX, m_ScreenY, 
        hostDlg->GetSafeHwnd(), NULL, hInstance, NULL ); 

我不想创建 child window,而是想创建 parent window。

我试过这个代码。

hwndMag = CreateWindow(WC_MAGNIFIER, TEXT("MagnifierWindow"), 
         MS_SHOWMAGNIFIEDCURSOR | WS_VISIBLE,
        0, 0, m_ScreenX, m_ScreenY, 
        NULL , NULL, hInstance, NULL ); 

可以看到一个新的 window 黑屏。即使我点击捕捉按钮,window 也卡住了。

为什么会这样,我怎样才能使它与新的 parent window 一起工作?

谢谢

放大镜 window 应该是 child window。因此它需要一个主机 parent window。 example code on MSDN 显示了如何操作:

BOOL CreateMagnifier(HINSTANCE hInstance)
{
   // Register the host window class.
    WNDCLASSEX wcex = {};
    wcex.cbSize = sizeof(WNDCLASSEX); 
    wcex.style          = 0;
    wcex.lpfnWndProc    = HostWndProc;
    wcex.hInstance      = hInstance;
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(1 + COLOR_BTNFACE);
    wcex.lpszClassName  = WindowClassName;
    
    if (RegisterClassEx(&wcex) == 0)
        return FALSE;

    // Create the host window. 
    hwndHost = CreateWindowEx(WS_EX_TOPMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT, 
        WindowClassName, WindowTitle, 
        WS_CLIPCHILDREN,
        0, 0, 0, 0,
        NULL, NULL, hInstance, NULL);
    if (!hwndHost)
    {
        return FALSE;
    }

    // Make the window opaque.
    SetLayeredWindowAttributes(hwndHost, 0, 255, LWA_ALPHA);

    // Create a magnifier control that fills the client area.
    hwndMag = CreateWindow(WC_MAGNIFIER, TEXT("MagnifierWindow"), 
        WS_CHILD | MS_SHOWMAGNIFIEDCURSOR | WS_VISIBLE,
        0, 0, 
        LENS_WIDTH, 
        LENS_HEIGHT, 
        hwndHost, NULL, hInstance, NULL );
    if (!hwndMag)
    {
        return FALSE;
    }

    return TRUE;
}

同一份文件还说:

The magnifier control must be hosted in a window created with the WS_EX_LAYERED extended style. After creating the host window, call SetLayeredWindowAttributes to set the opacity of the host window. The host window is typically set to full opacity to prevent the underlying screen content from showing though. The following example shows how to set the host window to full opacity:

SetLayeredWindowAttributes(hwndHost, NULL, 255, LWA_ALPHA);

If you apply the WS_EX_TRANSPARENT style to the host window, mouse clicks are passed to whatever object is behind the host window at the location of the mouse cursor. Be aware that, because the host window does not process mouse clicks, the user will not be able to move or resize the magnification window by using the mouse.

上面的 MSDN 示例说明了这一点。您 link 阅读的 CodeProject 文章也遵守这些规则。你也必须这样做。

如果有兴趣,我创建了一个示例应用程序,它使用放大倍数 API 前一段时间称为 "Windows 7 UI Automation Client API C# sample (focus tracking)",可在 https://code.msdn.microsoft.com/Windows-7-UI-Automation-6390614a 获得。该应用程序跟踪键盘焦点所在的位置,然后以放大 window 显示具有焦点的元素(并使用放大 API 反转颜色)。这是一个 C# 应用程序,因此它使用互操作来访问放大倍数 API。

结果截图如下所示。

谢谢,

家伙