TobiasKnudsen
TobiasKnudsen

Reputation: 579

Transparent Overlay

I'm trying to make an application which can overlay my screen with a transparent image in the middle. My goal is to make a crosshair for a game with no crosshair. My thinking is to detect if the active window title matches the game name, and if so display the overlaying crosshair. How would i make a screen overlay? This is my current code:

Private Function GetCaption() As String
    Dim Caption As New System.Text.StringBuilder(256)
    Dim hWnd As IntPtr = GetForegroundWindow()
    GetWindowText(hWnd, Caption, Caption.Capacity)
    Return Caption.ToString()
End Function

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If GetCaption() = "Game NameOf" Then
        'Display Crosshair
    End If 
End sub

Upvotes: 0

Views: 1385

Answers (1)

al-eax
al-eax

Reputation: 732

This methods works for most games but only in windowmode!

  1. Place a picturebox on your Form and maximize yout picturebox
  2. Disable your Windowboarders:

    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None

  3. Set your transperency key to black. Th

    Me.TransparencyKey = Color.Black

  4. Set background color to black:

    Me.PictureBox1.BackColor = Color.Black

  5. Set your window to foreground:

    Me.TopMost = true

  6. Maximize your window:

    Me.WindowState = FormWindowState.Maximized

Now you can draw on your Picturebox in a Timer1_Tick or Form1_Paint event. Everything that is not Black will be drawn to your Desktop.

Dim g As = GraphicsPictureBox1.CreateGraphics
...
g.DrawLine(Pens.Red, 10, 10, 200, 200)

Important:

To pass input from mouse and keyboard through your window, your have to add the WS_EX_TRANSPARENT flag while .net creates your form. This can be done by overriding CreateParams proterty:

Const  WS_EX_TRANSPARENT As Long = &H20
...
Protected Overrides ReadOnly Property  CreateParams() As System.Windows.Forms.CreateParams
Get 
    Dim  SecPerm As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
    SecPerm.Demand()
    Dim  cp As CreateParams = MyBase.CreateParams
    cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
    Return cp
End Get
End Property

Hope I could help you.

enter image description here

Upvotes: 3

Related Questions