I'm trying to create a HUD using Windows Forms, C# and VisualStudio 2010, with Windows 7 64-bit. My HUD is just a set of semi-transparent boxes that must provide additional information to the window they are attached to. The problem is that I cannot make my hud element semi-transparent when it is inside a parent form. My hud element is a box that inherits from the Form class. It's nothing but a borderless form (BorderStyle is set to "None", 100% opacity) with some colored labels inside, no code was added when creating it. So, I have a main form class called Form1 and another form class called HUDBox which is the hud element I want to display inside Form1. Both inherit from the Form class.
If I set HUDBox object's opacity to anything less then 100%, the HUDBox object just won't display inside Form1 object. I also tried using TransparencyKey, SetWindowLong and SetLayeredWindowAttributes via PInvoke but it makes everything transparent, not just the HUDBox object. Here is the code:
Program.cs:
HUD.cs
Code for HUDBox.cs and Form1.cs are not relevant as they are simple auto-generated Forms. This program adds 4 copies of my hud element inside the main form. My Form1 class is just for testing, it will be replaced by a real application's window in the future.
Opacity works fine for my hud boxes when I don't use the SetParent function, because they are not set as Form1's children.
How can I make those hud boxes semi-transparent inside Form1 so that you can see the Form1 background behind them?
If I set HUDBox object's opacity to anything less then 100%, the HUDBox object just won't display inside Form1 object. I also tried using TransparencyKey, SetWindowLong and SetLayeredWindowAttributes via PInvoke but it makes everything transparent, not just the HUDBox object. Here is the code:
Program.cs:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace MyHUD
{
public static class Program
{
public static Form1 mainForm;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mainForm = new Form1();
HUD psHUD = new HUD();
Application.Run(mainForm);
}
}
}
HUD.cs
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Drawing;
namespace MyHUD
{
class HUD
{
private static int HOW_MANY_HUDBOXES = 4;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private List<HUDBox> hudBoxes = new List<HUDBox>(HOW_MANY_HUDBOXES);
public HUD()
{
initHudBoxes();
}
private void initHudBoxes()
{
for (int hb = 0; hb < HOW_MANY_HUDBOXES; hb++)
{
HUDBox newHudBox = new HUDBox();
hudBoxes.Add(newHudBox);
SetParent(newHudBox.Handle, Program.mainForm.Handle);
newHudBox.Show();
}
}
}
}
Code for HUDBox.cs and Form1.cs are not relevant as they are simple auto-generated Forms. This program adds 4 copies of my hud element inside the main form. My Form1 class is just for testing, it will be replaced by a real application's window in the future.
Opacity works fine for my hud boxes when I don't use the SetParent function, because they are not set as Form1's children.
How can I make those hud boxes semi-transparent inside Form1 so that you can see the Form1 background behind them?
Last edited: