If the OS is Windows 8 or higher, you can make the child window a layered window. To do this, add an application manifest and enable Windows 8.
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
Create a class that inherits UserControl.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class LayerdUserControl : UserControl
{
private Color _TransparencyKey = Color.Empty;
private double _Opacity = 1.0f;
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
var dwStyle = GetWindowLong(Handle, GWL_EXSTYLE);
dwStyle |= WS_EX_LAYERED;
SetWindowLong(Handle, GWL_EXSTYLE, dwStyle);
UpdateLayered();
BeginInvoke(new MethodInvoker(InvalidateChildren));
}
protected override void OnInvalidated(InvalidateEventArgs e) {
base.OnInvalidated(e);
InvalidateChildren();
}
protected virtual void InvalidateChildren() {
if (HasChildren) {
foreach (Control con in Controls) {
con.Invalidate(true);
}
}
}
private void UpdateLayered() {
if (DesignMode || !IsHandleCreated) {
return;
}
Color transparencyKey = TransparencyKey;
byte opacityAsByte = (byte)(Opacity * 255.0f);
bool result;
if (transparencyKey.IsEmpty) {
result = SetLayeredWindowAttributes(Handle, 0, opacityAsByte, LWA_ALPHA);
} else if (opacityAsByte == 255) {
result = SetLayeredWindowAttributes(Handle, ColorTranslator.ToWin32(transparencyKey), 0, LWA_COLORKEY);
} else {
result = SetLayeredWindowAttributes(Handle, ColorTranslator.ToWin32(transparencyKey),
opacityAsByte, LWA_ALPHA | LWA_COLORKEY);
}
if (!result) {
throw new Win32Exception();
}
}
[Category("LayerdWindow")]
public virtual Color TransparencyKey {
get {
return _TransparencyKey;
}
set {
if (_TransparencyKey != value) {
_TransparencyKey = value;
UpdateLayered();
}
}
}
protected virtual void ResetTransparencyKey() {
TransparencyKey = Color.Empty;
}
protected virtual bool ShouldSerializeTransparencyKey() {
return !_TransparencyKey.IsEmpty;
}
[Category("LayerdWindow")]
[DefaultValue(1.0)]
[TypeConverterAttribute(typeof(OpacityConverter))]
public virtual double Opacity {
get {
return _Opacity;
}
set {
if (_Opacity != value) {
_Opacity = value;
UpdateLayered();
}
}
}
private const int WS_EX_LAYERED = 0x80000;
private const int GWL_EXSTYLE = -20;
private const int LWA_COLORKEY = 1;
private const int LWA_ALPHA = 2;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);
}
How To Use:
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
var f = new LayerdUserControl();
f.Text = string.Empty;
f.Opacity = 0.5;
f.BackColor = Color.Black;
f.Dock = DockStyle.Fill;
Controls.Add(f);
f.Show();
f.BringToFront();
}
}