使用Direct2d和Win7是否可以使用透明窗口?

时间:2020-10-21 09:04:38

标签: winapi alpha direct2d alpha-transparency

我的系统是Windows 7,我想将Direct2D用于工具包项目。

我想在窗口的某些部分中创建一个按像素透明/半透明的窗口(我们可以看到透明或半透明的窗口,这些窗口在我的窗口下方)。

我看过https://docs.microsoft.com/en-us/archive/msdn-magazine/2014/june/windows-with-c-high-performance-window-layering-using-the-windows-composition-engine(请参阅此网页最底部的图像),似乎有可能,但只能使用扩展的窗口样式WS_EX_NOREDIRECTIONBITMAP,该样式在Win7上不可用(仅从win8起)。

我尝试使用的渲染目标是带有预乘alpha的bgra和扩展的窗口样式WS_EX_COMPOSITED | WS_EX_TRANSPARENT没有运气

这是我的代码(相关的代码,即渲染目标和布局的创建,位于render()函数中):

#include <iostream>

#ifdef _WIN32_WINNT
# undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x602

#include <d2d1.h>

static HINSTANCE instance = NULL;
static HWND win = NULL;
static ID2D1Factory *factory = NULL;
static ID2D1HwndRenderTarget *rt = NULL;

LRESULT CALLBACK _window_procedure(HWND   window,
                                   UINT   message,
                                   WPARAM window_param,
                                   LPARAM data_param);

static void
render()
{
    RECT r;
    HRESULT res;

    /* render target */

    GetClientRect(win, &r);

    D2D1_SIZE_U size = D2D1::SizeU(r.right, r.bottom);

    FLOAT dpix;
    FLOAT dpiy;
    factory->GetDesktopDpi(&dpix, &dpiy);

    D2D1_RENDER_TARGET_PROPERTIES rtp;
    rtp.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
    rtp.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM;
    rtp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
    rtp.dpiX = dpix;
    rtp.dpiY = dpiy;
    rtp.usage = D2D1_RENDER_TARGET_USAGE_NONE;
    rtp.minLevel = D2D1_FEATURE_LEVEL_10;

    D2D1_HWND_RENDER_TARGET_PROPERTIES wrtp;
    wrtp.hwnd = win;
    wrtp.pixelSize = size;
    wrtp.presentOptions = D2D1_PRESENT_OPTIONS_IMMEDIATELY;

    res =factory->CreateHwndRenderTarget(rtp, wrtp, &rt);

    if (FAILED(res))
    {
        std::cout << "CreateHwndRenderTarget failed" << std::endl;
        return;
    }

    /* brush */
    ID2D1SolidColorBrush *brush;
    /* rgba */
    const D2D1_COLOR_F color = D2D1::ColorF(0.5, 0.5, 0, 0.5);
    res = rt->CreateSolidColorBrush(color, &brush);
    if (FAILED(res))
    {
        std::cout << "CreateSolidColorBrush failed" << std::endl;
        return;
    }

    /* ellipse */
    D2D1_ELLIPSE ellipse;
    D2D1_SIZE_F sz = rt->GetSize();
    const float x = sz.width / 2;
    const float y = sz.height / 2;
    const float radius = std::min(x, y);
    ellipse = D2D1::Ellipse(D2D1::Point2F(x, y), radius, radius);

    rt->BeginDraw();

    rt->Clear( D2D1::ColorF(D2D1::ColorF::SkyBlue, 0.5f) );
    rt->FillEllipse(ellipse, brush);

    rt->EndDraw();
}


LRESULT CALLBACK
_window_procedure(HWND   window,
                  UINT   message,
                  WPARAM window_param,
                  LPARAM data_param)
{
  switch (message)
    {
    case WM_CLOSE:
      PostQuitMessage(0);
      return 0;
    case WM_KEYUP:
      if (window_param == 'Q')
        {
          PostQuitMessage(0);
        }
      return 0;
      /* GDI notifications */
    case WM_PAINT:
      {
        RECT rect;

        std::cout << "paint" << std::endl;

        if (GetUpdateRect(window, &rect, FALSE))
          {
            PAINTSTRUCT ps;
            BeginPaint(window, &ps);

            render();

            EndPaint(window, &ps);
          }
        return 0;
      }
    default:
      return DefWindowProc(window, message, window_param, data_param);
    }
}

int main()
{
    /* class */
    WNDCLASS wc;

    instance = GetModuleHandle(NULL);
    if (!instance)
        return 1;

    memset (&wc, 0, sizeof (WNDCLASS));
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = _window_procedure;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = instance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
    wc.lpszMenuName =  NULL;
    wc.lpszClassName = "D2D";

    if(!RegisterClass(&wc))
        goto free_library;

    /* Window */
    int w;
    int h;
    RECT r;
    DWORD style;
    DWORD exstyle;

    w = 640;
    h = 480;

    style = WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
    exstyle = 0;
    exstyle |= WS_EX_COMPOSITED | WS_EX_TRANSPARENT;
    //exstyle |= WS_EX_NOREDIRECTIONBITMAP;

    r.left = 0;
    r.top = 0;
    r.right = w;
    r.bottom = h;
    if (!AdjustWindowRectEx(&r, style, FALSE, exstyle))
        goto unregister_class;

    win = CreateWindowEx(exstyle,
                         "D2D", "Test",
                         style,
                         100, 100,
                         r.right - r.left,
                         r.bottom - r.top,
                         NULL,
                         NULL, instance, NULL);
    if (!win)
        goto unregister_class;

    ShowWindow(win, SW_SHOWNORMAL);

    /* factory */
    if (FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
                                 &factory)))
      goto destroy_win;

    /* msg loop */
    while(1)
    {
        MSG msg;
        BOOL ret;

        ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
        if (ret)
        {
            do
            {
                if (msg.message == WM_QUIT)
                  goto beach;
                TranslateMessage(&msg);
                DispatchMessageW(&msg);
            } while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
        }
    }

 beach:
    factory->Release();
    DestroyWindow(win);
    UnregisterClass("D2D", instance);
    FreeLibrary(instance);

    return 0;

  destroy_win:
    DestroyWindow(win);
  unregister_class:
    UnregisterClass("D2D", instance);
  free_library:
    FreeLibrary(instance);

    return 1;
}

我也不想使用分层窗口。

有人知道在Windows 7上是否可以实现我想要的吗?

谢谢

1 个答案:

答案 0 :(得分:0)

在 Win7 上可以有一个没有 WS_EX_LAYERED 样式的透明窗口,但它有一些缺点:

  • window 必须有 WS_POPUP 样式,否则如果 Windows 样式有玻璃,窗口的背景会模糊。

  • windows上的DWM(Desktop Window Manager)必须开启,否则windows会不透明

  • 虽然窗口在视觉上是透明的,但鼠标点击不会穿过 100% 透明区域。也许有解决方案,但我不知道。

     // Transparent window setup
     #include <dwmapi.h>
     #pragma comment(lib, "Dwmapi")
     // ....
    
     MARGINS margins = { -1 };
     DwmExtendFrameIntoClientArea(m_hwnd, &margins);
     if (FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory)))
     {
         return -1;
     }
    
     // ...
    
     hr = pFactory->CreateHwndRenderTarget(
         D2D1::RenderTargetProperties(
             D2D1_RENDER_TARGET_TYPE_DEFAULT
             , D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)
         )
         , D2D1::HwndRenderTargetProperties(m_hwnd, size)
         , &pRenderTarget
     );
    

如果您的窗口不透明,您可能关闭了 DWM 或 Windows 没有设置区域样式。

https://weblogs.asp.net/kennykerr/direct2d-and-the-desktop-window-manager