@bobnoordam

Making a form transparant

This code sample displays a method to seemingly draw anywhere freely on the screen. The key to this technique is the TransparanceKey offered by the .net framework, which makes this a fairly straightforward task. To make the background of a form transparant, the *only* thing we need to do is this;

oDrawform.AllowTransparency = True
oDrawform.TransparencyKey = oDrawform.BackColor

If we add code to that to hide the control boxes, title bar and maximize the form we get a drawing area that is full screen, and appears to be “not there” to the user. To archieve that we need the following settings:

oDrawform.Text = ""
oDrawform.MinimizeBox = False
oDrawform.MaximizeBox = False
oDrawform.ControlBox = False

Once the form is in place, the normal drawing controls can be used to display graphics on the screen. The full code is below

Public Class Form1
  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
        ' --- Create a new full screen form with transparant background
        Dim oDrawform As New Form
        oDrawform.WindowState = FormWindowState.Maximized
        oDrawform.AllowTransparency = True
        oDrawform.TransparencyKey = oDrawform.BackColor
        ' //
  
        ' --- Remove the titlebar and control box
        oDrawform.Text = ""
        oDrawform.MinimizeBox = False
        oDrawform.MaximizeBox = False
        oDrawform.ControlBox = False
        ' //
  
        ' --- Show the form, and put some lines on it
        oDrawform.Show()
  
        Dim iMaxhor As Integer = oDrawform.Width
        Dim iMaxver As Integer = oDrawform.Height
  
        Using oPen As New System.Drawing.Pen(System.Drawing.Color.Red)
            Dim oGraphics As System.Drawing.Graphics = oDrawform.CreateGraphics()
            For iHor = 0 To iMaxhor Step 10
                For iVer = 0 To iMaxver Step 10
                    oGraphics.DrawLine(oPen, 0 + iHor, 0 + iVer, iMaxhor - iHor, iMaxver - iVer)
                Next
            Next
        End Using
        ' //
  
        ' --- Clean up and go home
        oDrawform.Close()
        MsgBox("Thats all folks!")
        ' //
  
    End Sub
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
        Me.Width = 300
        Me.Height = 300
        Button1.Text = "Action!"
  
    End Sub
End Class

C# Version

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;  // dont forget to add
  
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
        this.Width = 300;
        this.Height = 300;
        this.button1.Text = "Action!";
        }
  
        private void button1_Click(object sender, EventArgs e)
        {
  
            // Define transparancy
            Form oDrawing = new Form();
            oDrawing.AllowTransparency=true;
            oDrawing.TransparencyKey=oDrawing.BackColor;
            oDrawing.WindowState=FormWindowState.Maximized;
  
            // remove the visible controles
            oDrawing.ControlBox=false;
            oDrawing.MaximizeBox=false;
            oDrawing.MinimizeBox=false;
  
            // Display the form
            oDrawing.Show();
  
            // Draw some lines on it
            int imaxhor = oDrawing.Width;
            int imaxver = oDrawing.Height;
  
            using (Pen oPen = new Pen(Color.Red))
            {
                Graphics oGr = oDrawing.CreateGraphics();
                for (int ix = 0; ix < imaxhor;)
                {
                    for (int iy = 0; iy < imaxver;)
                    {
                        oGr.DrawLine(oPen,iy,ix,imaxhor-iy,imaxver-ix);
                        iy=iy+20;
                    }
                    ix=ix+20;
                }
            }
  
            // End of the show, clean up
            oDrawing.Close();
            MessageBox.Show("Thats all folks!");
  
        }
    }
}