I'm working on a program for my brother-in-law. He's a technician setting up live betting stations. He requested a program that overlays their software, displaying the MAC address. It would make things easier for him and his colleagues. They could ask HQ, but it would take at least a year to get the program, and his boss already gave the green light for this if I could make it. So far, I've managed to create the program in C#, but it won't overlay on fullscreen applications. Is there an easy way to achieve this?
using System;
using System.Net.NetworkInformation;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using Timer = System.Windows.Forms.Timer;
namespace TransparentOverlay {
public partial class DisplayMac : Form {
private Label macLabel;
public DisplayMac() {
InitializeComponent();
InitializeUI();
ShowMACAddress();
SetForegroundWindow(this.Handle);
}
private void InitializeUI() {
FormBorderStyle = FormBorderStyle.None;
BackColor = System.Drawing.Color.Black; // Set the color to the stroke color
TransparencyKey = System.Drawing.Color.Blue;
Size = new System.Drawing.Size(200, 30);
StartPosition = FormStartPosition.Manual;
Location = new System.Drawing.Point(Screen.PrimaryScreen.WorkingArea.Width - Width, Screen.PrimaryScreen.WorkingArea.Height - Height);
macLabel = new Label {
Text = "MAC Address: ",
Location = new System.Drawing.Point(10, 10),
AutoSize = true,
ForeColor = System.Drawing.Color.White,
BackColor = System.Drawing.Color.Black,
};
Controls.Add(macLabel); // Then add the main label on top
}
private void ShowMACAddress() {
string macAddress = GetMACAddress();
macLabel.Text += macAddress;
Timer timer = new Timer {
Interval = 1000 // Update every 1000 milliseconds (1 second)
};
timer.Tick += (sender, e) => macLabel.Text = "MAC Address: " + GetMACAddress();
timer.Start();
SetWindowExTransparent(this.Handle);
}
private string GetMACAddress() {
string mac = string.Empty;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.OperationalStatus == OperationalStatus.Up) {
byte[] physicalAddressBytes = nic.GetPhysicalAddress().GetAddressBytes();
mac = string.Join(":", physicalAddressBytes.Select(b => b.ToString("X2")));
break;
}
}
return mac;
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const int WS_EX_LAYERED = 0x80000;
private const int WS_EX_TRANSPARENT = 0x20;
private const uint SWP_NOMOVE = 0x2;
private const uint SWP_NOSIZE = 0x1;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
private const uint LWA_COLORKEY = 0x1;
private const uint LWA_ALPHA = 0x2;
private void SetWindowExTransparent(IntPtr handle) {
SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
SetLayeredWindowAttributes(handle, 0, 128, LWA_ALPHA);
SetWindowLong(handle, GWL_EXSTYLE, GetWindowLong(handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT);
}
private const int GWL_EXSTYLE = -20;
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DisplayMac());
}
}
}