Reputation: 579
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
Reputation: 732
This methods works for most games but only in windowmode!
Disable your Windowboarders:
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Set your transperency key to black. Th
Me.TransparencyKey = Color.Black
Set background color to black:
Me.PictureBox1.BackColor = Color.Black
Set your window to foreground:
Me.TopMost = true
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.
Upvotes: 3