투명한 창에 투명한 DirectX 컨텐츠를 어떻게 그리나요?
-
02-07-2019 - |
문제
DirectX 컨텐츠를 그려서 데스크탑 위에 떠있는 것처럼 보이도록하고 싶습니다. 또한 DirectX 컨텐츠를 반 트랜스 펜트로 만들 수 있어야하므로 다른 것들이 보여줍니다. 이 작업을 수행하는 방법이 있습니까?
C#과 함께 관리되는 DX를 사용하고 있습니다.
해결책
OregonGhost가 제공 한 링크에서 시작하여 Vista에서 작동하는 솔루션을 찾았습니다. C# 구문의 기본 프로세스입니다. 이 코드는 형식에서 상속 된 클래스에 있습니다. 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% 투명합니다.
다른 팁
DirectComposition, LayeredWindows, DesktopWindowManager 또는 WPF를 사용할 수 있습니다. 모든 방법은 장점과 단점이 있습니다.
-방향성 구성은 가장 효율적인 것이지만 Windows 8이 필요하며 60Hz로 제한됩니다.
-layeredwindows는 DXGI를 사용하여 Direct2D 인터 로프를 통해 D3D로 작업하는 것이 까다 롭습니다.
-WPF는 D3dimage를 통해 상대적으로 사용하기 쉽지만 60Hz 및 DX9로 제한되며 MSAA가 없습니다. DXGI를 통해 더 높은 DX 버전으로의 인터 로프가 가능하며, MSAA-RenderTarget이 기본 NonMSAA 표면으로 분해 될 때 MSAA를 사용할 수 있습니다.
-desktopwindowmanager는 Windows Vista이므로 고성능에 적합하지만 Directx Versions는 DWM이 사용하는 버전 (Vista의 DX9)에 의해 제한되는 것으로 보입니다. DXGI를 통해 DXGI를 통해 DXGI를 통해 가능해야합니다.
픽셀 APLHA 당 필요하지 않은 경우 반 트랜스 펜트 형태의 불투명도 가치를 사용할 수도 있습니다.
또는 Window Global Alpha에 기본 Win32 메소드를 사용합니다 (0의 알파는 마우스 입력을 잡을 수 없습니다) :
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 및 Native Win32의 경우 작은 C ++-래퍼 코드가 필요했습니다. 초보자의 경우 WPF를 통해 가라고 제안합니다.
Windows XP를 지원하려면 데스크탑 윈도우 관리자를 사용하지 않고는 어려울 것 같습니다. DWM과 함께하는 것 같습니다 오히려 쉬운 그렇지만.
속도가 문제가되지 않으면 표면으로 렌더링 한 다음 렌더링 된 이미지를 레이어링 된 창에 복사 할 수 있습니다. 그래도 빨리 기대하지 마십시오.
WPF 또 다른 옵션입니다.
Microsoft가 개발 한 Windows Presentation Foundation (또는 WPF)은 Windows 기반 애플리케이션의 사용자 인터페이스를 렌더링하기위한 컴퓨터 소프트웨어 그래픽 하위 시스템입니다.