c++ - 部分透明窗口 OpenGL/Win32

标签 c++ winapi opengl

我已经阅读了关于这个棘手(利基)主题的所有现有问题,但我被卡住了。我有一个带有 OpenGL 上下文的 Win32 窗口。我希望我的窗口部分透明。

到目前为止,我的结果是整个窗口都是透明的。我只希望黑色区域是透明的,这样我就可以绘制一些 3D 对象,它们看起来就像是从窗口出来的。

enter image description here

首先,在我的窗口类中,我将 hbrBackground 设置为黑色。

Windows::WindowClass windowClass;
windowClass.cbSize = sizeof(Windows::WindowClass);
windowClass.style = Windows::CS_VREDRAW | Windows::CS_HREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = moduleHandle;
windowClass.hIcon = Windows::LoadIcon(0u, (X*)Windows::IDI_QUESTION);
windowClass.hCursor = Windows::LoadCursor(0u, (X*)Windows::IDC_ARROW);
windowClass.hbrBackground = Windows::CreateSolidBrush(0x00000000);
windowClass.lpszMenuName = nullptr;
windowClass.lpszClassName = (X*)name;
windowClass.hIconSm = Windows::LoadIcon(0u, (X*)Windows::IDI_QUESTION);

我已经使用 WS_EX_LAYERED 标志创建了我的窗口。

windowHandle = Windows::CreateWindow(Windows::WS_EX_LAYERED, (X*)name, "", Windows::WS_POPUP, w / 4, h / 4, w / 2, h / 2, 0u, 0u, moduleHandle, 0u);

在我的像素格式中,我启用了 alpha 和合成。

PixelFormatDescriptor format;
format.nSize = sizeof(PixelFormatDescriptor);
format.nVersion = 1;
format.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_SUPPORT_COMPOSITION;
format.iPixelType = PFD_TYPE_RGBA;
format.cColorBits = 32;
format.cRedBits = 0;
format.cRedShift = 0;
format.cGreenBits = 0;
format.cGreenShift = 0;
format.cBlueBits = 0;
format.cBlueShift = 0;
format.cAlphaBits = 8;
format.cAlphaShift = 0; 
format.cAccumBits = 0; 
format.cAccumRedBits = 0;
format.cAccumGreenBits = 0;
format.cAccumBlueBits = 0;
format.cAccumAlphaBits = 0;
format.cDepthBits = 24;
format.cStencilBits = 8; 
format.cAuxBuffers = 0; 
format.iLayerType = PFD_MAIN_PLANE;
format.bReserved = 0;
format.dwLayerMask = 0;
format.dwVisibleMask = 0; 
format.dwDamageMask = 0;

我试过模糊区域“技巧”,但没有效果。我的结果与这段代码无关。

struct DwmBlurBehind
{
    U4 dwFlags;
    S4 fEnable;
    X* blurRegionHandle;
    S4 fTransitionOnMaximized;
};

DwmBlurBehind blurBehind;
blurBehind.dwFlags = Windows::DWM_BB_ENABLE | Windows::DWM_BB_BLURREGION;
blurBehind.fEnable = true;
blurBehind.blurRegionHandle = Windows::CreateRectRgn(0, 0, -1, -1);
blurBehind.fTransitionOnMaximized = false;
Windows::DwmEnableBlurBehindWindow(windowHandle, &blurBehind);

最后,我设置了 LWA_COLORKEY 和 LWA_ALPHA 属性。这就是给我显示的效果。但是,似乎没有考虑颜色键(我也尝试过非零值)。

Windows::SetLayeredWindowAttributes(windowHandle, 0, 170, Windows::LWA_COLORKEY | Windows::LWA_ALPHA);

我没有忘记启用混合。

GL::Enable(GL::BLEND);
GL::BlendFunc(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);

最佳答案

您想要做的事情需要自 Windows Vista 以来一直存在的窗口合成,因此基本上您必须关心的每个 Windows 版本(Windows XP 和更早版本已停产)。

要采取的关键步骤是,通过启用“Blur Behind Window”并使用 WM_POPUP 窗口来启用 D​​WM 窗口内合成;如果您不使用 WM_POPUP 样式,窗口管理器将绘制装饰,您的 OpenGL 渲染将“悬停”在装饰之上。

        DWM_BLURBEHIND bb = {0};
        bb.dwFlags = DWM_BB_ENABLE;
        bb.fEnable = TRUE;
        bb.hRgnBlur = NULL;
        DwmEnableBlurBehindWindow(hWnd, &bb);

        MARGINS margins = {-1};
        impl_DwmExtendFrameIntoClientArea(hWnd, &margins);

接下来,您必须使用较新的“属性”API 创建一个 OpenGL 上下文,而不是使用像素格式描述符选择。使用属性 API,您可以选择具有 alpha 窗口格式的透明

int attribs[] = {
    WGL_DRAW_TO_WINDOW_ARB, TRUE,
    WGL_DOUBLE_BUFFER_ARB, TRUE,
    WGL_SUPPORT_OPENGL_ARB, TRUE, 
    WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
    WGL_TRANSPARENT_ARB, TRUE,
    WGL_COLOR_BITS_ARB, 32,
    WGL_RED_BITS_ARB, 8,
    WGL_GREEN_BITS_ARB, 8,
    WGL_BLUE_BITS_ARB, 8,
    WGL_ALPHA_BITS_ARB, 8,
    WGL_DEPTH_BITS_ARB, 24,
    WGL_STENCIL_BITS_ARB, 8,
    0, 0
};

INT iPF;
UINT num_formats_choosen;
if( !wglChoosePixelFormatARB(
        hDC, 
        attribs, 
        NULL,
        1,
        &iPF,
        &num_formats_choosen) ) {
    fprintf(stderr, "error choosing proper pixel format\n");
    return NULL;
}
if( !num_formats_choosen ) {
    return NULL;
}

PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
/* now this is a kludge; we need to pass something in the PIXELFORMATDESCRIPTOR 
 * to SetPixelFormat; it will be ignored, mostly. OTOH we want to send something
 * sane, we're nice people after all - it doesn't hurt if this fails. */
DescribePixelFormat(hDC, iPF, sizeof(pfd), &pfd);

if( !SetPixelFormat(hDC, iPF, &pfd) ) {
    fprintf(stderr, "error setting proper pixel format\n");
    ReleaseDC(hWnd, hDC);
    DestroyWindow(hWnd);

    return NULL;
}

我在 GitHub 上的 wglarb 包装器存储库中有一个完整的工作示例:

https://github.com/datenwolf/wglarb/blob/master/test/layered.c

关于c++ - 部分透明窗口 OpenGL/Win32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511634/

相关文章:

c++ - 这个c++继承结构可能吗?

c++ - 多 channel 核图像卷积

C++ 套接字 - 我只能发送字符吗?

c++ - 替换为 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

c++ - 渲染多个对象时opengl闪烁

c++ - 在函数参数中连接字符串和变量?

c++ - 将 IPv4 地址从 ifaddrs 结构分配给 sockaddr_in 结构

c# - 尝试获取客户端进程可执行路径时访问被拒绝错误

c++ - 如何访问 Windows shell 上下文菜单项?

c++ - ZZ坐标不匹配