gpt4 book ai didi

c# - 如何让我的整个游戏窗口透明?

转载 作者:太空狗 更新时间:2023-10-30 01:34:43 27 4
gpt4 key购买 nike

我正在为暗黑破坏神 3 制作一个小覆盖图(仅供个人使用!)我只想在屏幕中间绘制一个文本字符串(我们稍后会看到字体)。但是使用 XNA 我找不到如何将背景设置为透明……到目前为止我的代码是:

        GraphicsDevice.Clear(new Color(0, 0, 0, 255));
spriteBatch.Begin();
spriteBatch.DrawString(font, this.TestToShow, new Vector2(23, 23), Color.White);
spriteBatch.End();

所以我只需要一件事:让这个黑色透明!

最佳答案

您似乎不明白 GraphicsDevice.Clear(Color) 的作用。XNA 打开一个 Windows 窗口并使用 DirectX 在其中绘图。

GraphicsDevice.Clear(Color) 清除使用 DirectX 绘制的缓冲区,但与窗口无关。要使窗口透明,您必须修改底层窗口。

为此,您必须首先添加对 System.WIndows.Forms 和 System.Drawing 的引用。

在 Game1 类的构造函数中,您执行以下操作:

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IntPtr hWnd = Window.Handle;
System.Windows.Forms.Control ctrl = System.Windows.Forms.Control.FromHandle(hWnd);
System.Windows.Forms.Form form = ctrl.FindForm();
form.TransparencyKey = System.Drawing.Color.Black;
}

让我们逐行分析:

好吧,前两个是自动生成的,我们不关心这些。

IntPtr hWnd = Window.Handle;

此行为您提供指向在 Windows 中注册的底层窗口的指针。

System.Windows.Forms.Control ctrl = System.Windows.Forms.Control.FromHandle(hWnd);

此行获取给定窗口中的 WindowsForms-Control

System.Windows.Forms.Form form = ctrl.FindForm();

此行获取控件所属的表单。

form.TransparencyKey = System.Drawing.Color.Black;

最后一行设置键-Color,它标识一个Color-值根本不被绘制。我用的是Black,但你也可以选择CornflowerBlue

这使您的窗口对于该 Color 内部透明。我建议您应该选择与您的透明 Color 相同的 Color

需要注意两点:

  1. 最佳做法是缓存您的 Form,这样您就可以从任何地方设置 TransparencyKey

  2. 您还可以通过这种方式使您的窗口无边框:

form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

希望我能帮上忙。

编辑:我才意识到这是几年前被问到的,但没有答案。因此,如果您偶然发现它,请随意使用它。

关于c# - 如何让我的整个游戏窗口透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665849/

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