I’m trying to make a transparent overlay that will stack on top of another control. It should have an opacity and text in the middle. I’m in the middle of a .NET Framework to .NET 8 upgrade and all the overlays stopped working.
This is the .NET Framework code that worked before. It works by painting the control Magenta and then we use the Magenta color as the key when we want to remove the color.
class OverlayControl : Control
{
public OverlayControl()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Magenta, this.DisplayRectangle); // NET8 no longer works
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// NET8 no longer works
NativeMethods.SetWindowLongPtr(this.Handle, NativeMethods.GWL_EXSTYLE, new IntPtr(NativeMethods.WS_EX_LAYERED));
NativeMethods.SetLayeredWindowAttributes(this.Handle, (uint)Color.Magenta.ToArgb(), 0, NativeMethods.LWA_COLORKEY);
}
}
New contributor
viktor.rosvall is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.