我正在尝试在Windows窗体中创建透明背景,以便在其上绘制一些文本,使其在其他正在运行的程序之前像HUD一样工作。
但是,Device.Clear(Color.Transparent)不起作用,背景变黑了,这是我当前的代码:
public HUD()
{
InitializeComponent();
var renderProp = new HwndRenderTargetProperties
{
Hwnd = this.Handle,
PixelSize = new Size2(Client.GameScreen.width, Client.GameScreen.height),
PresentOptions = PresentOptions.None
};
Device = new WindowRenderTarget(
new SharpDX.Direct2D1.Factory(),
new RenderTargetProperties(
new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)
),
renderProp);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Text = "";
this.AutoSize = false;
// Code to Set the Size Here....
this.UseWaitCursor = false;
this.Cursor = null;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.TopMost = true;
this.BackColor = Color.LimeGreen;
this.TransparencyKey = Color.LimeGreen;
this.ForeColor = Color.White;
this.DoubleBuffered = true;
this.PerformLayout();
this.ResumeLayout(false);
}
上面的代码是HUD类,它扩展了Form类,在我的主窗口中,我有一个由新线程启动的循环:
var hud = new HUD();
void RunHud()
{
while (true)
{
// Clear the target.
hud.Device.BeginDraw();
hud.Device.Clear(Color.Transparent.ToRaw()); //Extension method to convert to RawColor4
hud.Device.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Aliased;
hud.Device.AntialiasMode = AntialiasMode.Aliased;
// Draw a block of text that will be clipped against the specified layout rectangle.
hud.Device.DrawText("This text is long enough to overflow the designed region but w. ", textFormat, new SharpDX.Mathematics.Interop.RawRectangleF(500, 500, 700, 700), textBruch, DrawTextOptions.Clip);
hud.Device.EndDraw();
Thread.Sleep(10);
}
}
这只是测试SharpDX的“ Hello World”,我在Device.Clear()和Form BackColor / TransparencyColor中尝试了多种不同的颜色。
我正在使用Windows 7,VS2017和SharpDX 4.2,不确定是否丢失了某些内容。