【问题标题】:is it possible to have transparent window using Direct2d and Win7?使用 Direct2d 和 Win7 可以有透明窗口吗?
【发布时间】:2021-02-04 04:07:17
【问题描述】:

我的系统是 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 上实现我想要的吗?

谢谢

【问题讨论】:

  • 肯尼·科尔对此有一篇很好的文章:docs.microsoft.com/en-us/archive/msdn-magazine/2009/december/…
  • @JonathanPotter 在本文中,使用了分层窗口,这是我不想要的,正如我在帖子中所说的那样。
  • 如果您主动拒绝解决方案,那么您的问题就没有解决方案。
  • 我不拒绝这个解决方案,因为它不是一个解决方案:有人提出了我不想要的东西。你不能称之为解决方案。如果有人告诉我我想要的东西真的不可能,那么,我可以接受。
  • 使用分层窗口 Windows 7 上的解决方案。您不希望它成为解决方案的事实并不会改变这一点。要么使用分层窗口,要么不支持 Windows 7,要么干脆放弃使用半透明窗口来打造良好 UI 的想法。

标签: winapi alpha direct2d alpha-transparency


【解决方案1】:

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

  • 窗口必须有WS_POPUP风格,否则如果窗口风格有玻璃,窗口的背景会被模糊。

  • 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 没有设置areo 样式。

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

【讨论】:

    猜你喜欢
    • 2012-11-14
    • 2013-02-21
    • 2019-04-24
    • 2016-11-22
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 2015-08-15
    相关资源
    最近更新 更多