5

我想绘制 DirectX 内容,使其看起来漂浮在桌面和任何其他正在运行的应用程序之上。我还需要能够使 directx 内容半透明,以便其他内容显示出来。有没有办法做到这一点?

我正在使用带有 C# 的托管 DX。

4

4 回答 4

5

从 OregonGhost 提供的链接开始,我找到了一个适用于 Vista 的解决方案。这是 C# 语法中的基本过程。此代码位于从 Form 继承的类中。如果在 UserControl 中,它似乎不起作用:

//this will allow you to import the necessary functions from the .dll
using System.Runtime.InteropServices;

//this imports the function used to extend the transparent window border.
[DllImport("dwmapi.dll")]
static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);

//this is used to specify the boundaries of the transparent area
internal struct Margins {
    public int Left, Right, Top, Bottom;
}
private Margins marg;

//Do this every time the form is resized. It causes the window to be made transparent.
marg.Left = 0;
marg.Top = 0;
marg.Right = this.Width;
marg.Bottom = this.Height;
DwmExtendFrameIntoClientArea(this.Handle, ref marg);

//This initializes the DirectX device. It needs to be done once.
//The alpha channel in the backbuffer is critical.
PresentParameters presentParameters = new PresentParameters();
presentParameters.Windowed = true;
presentParameters.SwapEffect = SwapEffect.Discard;
presentParameters.BackBufferFormat = Format.A8R8G8B8;

Device device = new Device(0, DeviceType.Hardware, this.Handle,
CreateFlags.HardwareVertexProcessing, presentParameters);

//the OnPaint functions maked the background transparent by drawing black on it.
//For whatever reason this results in transparency.
protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;

    // black brush for Alpha transparency
    SolidBrush blackBrush = new SolidBrush(Color.Black);
    g.FillRectangle(blackBrush, 0, 0, Width, Height);
    blackBrush.Dispose();

    //call your DirectX rendering function here
}

//this is the dx rendering function. The Argb clearing function is important,
//as it makes the directx background transparent.
protected void dxrendering() {
    device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0, 0), 1.0f, 0);

    device.BeginScene();
    //draw stuff here.
    device.EndScene();
    device.Present();
}

最后,具有默认设置的表单将具有玻璃状的部分透明背景。将 FormBorderStyle 设置为“none”,它将 100% 透明,只有您的内容漂浮在所有内容之上。

于 2008-09-30T08:57:30.467 回答
5

您可以使用 DirectComposition、LayeredWindows、DesktopWindowManager 或 WPF。所有方法都有其优点和缺点:

-DirectComposition 是最有效的一种,但需要 Windows 8 并且限制为 60Hz。

-LayeredWindows 很难通过 Direct2D-interop 使用 DXGI 来处理 D3D。

-WPF 通过 D3DImage 相对容易使用,但也仅限于 60Hz 和 DX9 且没有 MSAA。通过 DXGI 与更高 DX 版本的互操作是可能的,当 MSAA-Rendertarget 解析为原生非 MSAA 表面时,也可以使用 MSAA。

-DesktopWindowManager 非常适合自 Windows Vista 以来提供的高性能,但 DirectX 版本似乎受到 DWM 使用的版本的限制(在 Vista 上仍然是 DX9)。更高 DX 版本的解决方法应该可以通过 DXGI(如果可用)来实现。

如果不需要每像素 aplha,也可以使用半透明形式的 opacity-value。

或者您对 Window 全局 alpha 使用本机 Win32 方法(请记住 0 的 alpha 不会捕获鼠标输入):

SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
COLORREF color = 0;
BYTE alpha = 128;
SetLayeredWindowAttributes(hWnd, color, alpha, LWA_ALPHA);

我已经能够在 C# 和 SharpDX 中使用所有描述的技术,但在 DirectComposition、LayeredWindows 和本机 Win32 的情况下,需要一点 C++-Wrappercode。对于初学者,我建议通过 WPF。

于 2015-01-18T13:02:53.523 回答
2

我猜如果不使用桌面窗口管理器,即如果你想支持 Windows XP,那将很难。不过,使用 DWM 似乎相当容易

如果速度不是问题,您可以先渲染到表面,然后将渲染的图像复制到分层窗口。不要指望这会很快。

于 2008-09-29T11:12:02.757 回答
1

WPF也是另一种选择。

由 Microsoft 开发的 Windows Presentation Foundation(或 WPF)是一种计算机软件图形子系统,用于在基于 Windows 的应用程序中呈现用户界面。

于 2008-10-27T10:07:27.150 回答