gpt4 book ai didi

c# - 如何在透明窗口中绘制透明的 DirectX 内容?

转载 作者:可可西里 更新时间:2023-11-01 13:28:18 29 4
gpt4 key购买 nike

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

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

最佳答案

我找到了一个适用于 Vista 的解决方案,从 OregonGhost 提供的链接开始。这是 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 设置为“无”,它将 100% 透明,只有您的内容 float 在所有内容之上。

关于c# - 如何在透明窗口中绘制透明的 DirectX 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/148275/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com