سؤال

أرغب في رسم محتوى DirectX بحيث يبدو وكأنه عائم فوق سطح المكتب وأي تطبيقات أخرى قيد التشغيل.أحتاج أيضًا إلى أن أكون قادرًا على جعل محتوى Directx شبه شفاف، حتى تظهر الأشياء الأخرى من خلاله.هل هناك طريقة للقيام بذلك؟

أنا أستخدم Managed DX مع C#.

هل كانت مفيدة؟

المحلول

لقد وجدت حلاً يعمل على نظام التشغيل Vista، بدءًا من الرابط المقدم من OregonGhost.هذه هي العملية الأساسية في بناء جملة 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 على "none" وسيكون شفافًا بنسبة 100% مع ظهور المحتوى الخاص بك فقط فوق كل شيء.

نصائح أخرى

يمكنك إما استخدام DirectComposition أو LayeredWindows أو DesktopWindowManager أو WPF.جميع الأساليب لها مزاياها وعيوبها:

-DirectComposition هو الأكثر كفاءة، ولكنه يحتاج إلى Windows 8 ويقتصر على 60 هرتز.

-LayeredWindows من الصعب العمل مع D3D عبر Direct2D-interop باستخدام DXGI.

-WPF سهل الاستخدام نسبيًا عبر D3DIImage، ولكنه يقتصر أيضًا على 60 هرتز وDX9 ولا يوجد MSAA.من الممكن التشغيل المتداخل لإصدارات DX الأعلى عبر DXGI، كما يمكن استخدام MSAA عندما يتم حل MSAA-Rendertarget على سطح غير MSAA الأصلي.

-DesktopWindowManager رائع للأداء العالي المتوفر منذ نظام التشغيل Windows Vista، ولكن يبدو أن إصدارات DirectX محدودة بالإصدار الذي يستخدمه DWM (لا يزال DX9 على نظام التشغيل Vista).يجب أن تكون الحلول البديلة لإصدارات DX الأعلى ممكنة عبر DXGI حيثما كان ذلك متاحًا.

إذا لم تكن بحاجة لكل بكسل aplha، فيمكنك أيضًا استخدام قيمة العتامة لنموذج شبه شفاف.

أو يمكنك استخدام طريقة Win32 الأصلية لـ Window alpha العمومية (تذكر أن ألفا 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 وWin32 الأصلي، كانت هناك حاجة إلى القليل من C++-Wrappercode.بالنسبة للمبتدئين أود أن أقترح الذهاب عبر WPF.

أعتقد أن ذلك سيكون صعبًا دون استخدام Desktop Window Manager، على سبيل المثال.إذا كنت تريد دعم نظام التشغيل Windows XP.مع DWM، يبدو أن يكون سهل إلى حد ما رغم ذلك.

إذا لم تكن السرعة مشكلة، فيمكنك التخلص من العرض على السطح ثم نسخ الصورة المعروضة إلى نافذة ذات طبقات.لا تتوقع أن يكون ذلك سريعًا بالرغم من ذلك.

WPF هو أيضا خيار آخر.

تم تطوير Windows Presentation Foundation (أو WPF) بواسطة Microsoft، وهو نظام فرعي رسومي لبرامج الكمبيوتر لعرض واجهات المستخدم في التطبيقات المستندة إلى Windows.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top