Transparent Overlay - vb.net

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

This methods works for most games but only in windowmode!
Place a picturebox on your Form and maximize yout picturebox
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.

Related

Custom checkbox - disabled state color

I have a custom checkbox witch allows resizing of the main rectangle. But when I want to set it as disabled, it doesnt gray out like normal checkbox control does. It just stays white.
Here is class for custom checkbox to allow resizing:
Public Class NewCheckBox
Inherits CheckBox
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
'Make the box you check 3/4 the height
Dim boxsize As Integer = Me.Height * 0.75
Dim rect As New Rectangle(
New Point(0, Me.Height / 2 - boxsize / 2),
New Size(boxsize, boxsize)
)
ControlPaint.DrawCheckBox(e.Graphics, rect, If(Me.Checked, ButtonState.Checked, ButtonState.Normal))
End Sub
End Class
Here is how i want to use it:
Private Sub txtFpo_TextChanged(sender As Object, e As EventArgs) Handles txtFpo.TextChanged
If functii.verificaLaser(txtFpo.Text) = True Then
NewCheckBox1.Enabled = False
'NewCheckBox1.ForeColor = Color.DarkGray
End If
I've tried to set the forecolor property, but to no avail.
How can i make the custom control to be greyed out in disabled mode?
I don't see anything in your code that specifies how to paint based on whether the control is disabled or not, so of course it doesn't change. Inactive is one of the ButtonState values so you need to specify that when the control is disabled.
Dim buttonState = ButtonState.Normal
If Me.Checked Then
buttonState = buttonState Or ButtonState.Checked
End If
If Not Me.Enabled Then
buttonState = buttonState Or ButtonState.Inactive
End If
ControlPaint.DrawCheckBox(e.Graphics, rect, buttonState)

How to Activate the pervious active Window in VB?

I am trying to create a windows form app using visual studio to capture the screen shot of an active window application. I created a capture button which captures the form itself and not the Previous active window. Thus i want to toggle to the previous active window and a screen shoot should be captured considering that as the current active window. I am relatively new to VB. kindly help me out with this issue.
thank you.
One approach is to prevent your Form from getting Focus, thus keeping the "previous" form as the active selected one.
See WS_EX_NOACTIVATE:
A top-level window created with this style does not become the
foreground window when the user clicks it.
Public Class Form1
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Private Const WM_MOVING As Integer = &H216
Private Const WS_EX_NOACTIVATE As Integer = &H8000000
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_NOACTIVATE
Return cp
End Get
End Property
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_MOVING Then
Dim r As RECT = DirectCast(Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, GetType(RECT)), RECT)
Me.Location = New Point(r.Left, r.Top)
End If
MyBase.WndProc(m)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Console.WriteLine("...your active screen capture code here...")
End Sub
End Class

VB.NET How can I popup a game-safe notification?

How can I, in VB.NET, display a notification on-screen, such as that this will be "game-safe".
Eg: "Theres only 30 minutes left until you will be logged out!".
The notification should not take focus from the game (eg "steal input") and the notification should only be shown for like 5-10 seconds and then disappear by itself.
The notification should also be safe when talking about anticheats, like Punkbuster, VAC and such.
Any ideas?
I don't know about "game safe" as I don't know what those games are looking for to trigger an alert.
What you can do is override ShowWithoutActivation() and return true so that your form does not get focus when shown. Additionally, you can set the WS_EX_TRANSPARENT extended window style so that all mouse messages literally go right through your form. Apps underneath won't even know your form is there. Finally, set Opacity so that you can see through it partially. Oh...the timer is in there to close it after ten seconds:
Public Class frmNotification
Private WithEvents Tmr As New System.Windows.Forms.Timer
Private Sub frmNotification_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Opacity = 0.5 ' Make it so you can see thru it partially
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
' make it appear in the bottom right of the screen
Me.StartPosition = FormStartPosition.Manual
Dim rc As Rectangle = Screen.GetWorkingArea(Me)
Me.Location = New Point(rc.Right - Me.Width, rc.Bottom - Me.Height)
Tmr.Interval = TimeSpan.FromSeconds(10).TotalMilliseconds
Tmr.Start()
End Sub
Private Const WS_EX_TRANSPARENT As Integer = &H20
' Make all mouse events PASS RIGHT THRU IT:
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
Return cp
End Get
End Property
' Show it without activating it:
Protected Overrides ReadOnly Property ShowWithoutActivation() As Boolean
Get
Return True
End Get
End Property
Private Sub Tmr_Tick(sender As Object, e As EventArgs) Handles Tmr.Tick
Me.Close()
End Sub
End Class

Set BackGround Color of Textbox to Transparent

I am trying to set the background color of textbox to transparent, to blend with my backcolor of my form.
I have tried the following below.
TextBox1.BackColor = Color.Transparent 'This doesn't work it stays white'
Is there something I am missing?
When I set TextBox.BackColor to Color.Transparent, it throws System.ArgumentException. I got this message :
Unvalid property value, The control does not support transparent colors background.
Hope am not late to the party, but this actually works for me. First create a class for the panel as below
Partial Public Class iPanel
Inherits Panel
Public Sub New()
SetStyle(ControlStyles.SupportsTransparentBackColor Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
BackColor = Color.Transparent
End Sub
End Class
Then create a RichTextBox (Instead of a Textbox) as below
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim CP As CreateParams = MyBase.CreateParams
CP.ExStyle = CP.ExStyle Or &H20
Return CP
End Get
End Property
Now compile the code and add the iRichTextBox inside the panel. Works for me
Private Sub TextBox1_Paint(sender As Object, e As PaintEventArgs) Handles TextBox1.Paint
TextBox1.ForeColor = Color.White
TextBox1.BackColor = Color.Transparent
End Sub
Instead of doing the first one, You can try this. Just put your code in Form Load
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.BackColor = color.(color of your Choice, same color of your background)
TextBox1.ForeColor = color.White
End Sub
As simple as that, it works for me
As far as I know textbox does not support transparent color property. But if you set the back color of the textbox to the same color as of its background component, still it can be considered as transparent.
How to do that - You can get the color name of the background component(in your case it is the form) and pass that name to the component which you want to be transparent.
Dim lname As String = Me.BackColor.ToString
Dim name As String = lname.Substring(7, lname.Length - 8)
txtbox1.BackColor = System.Drawing.Color.FromName(name)
Explanation -
The first line in the code gets you the name of the color, but there's a rub, it gets the name something like this - Color [Dark Orange] and we need only the name of the color i.e Dark Orange.
Thus the second line is to get the exact color name by removing this - Color [] part
And the last line to set that color same as of the background component color.
Hope it works, still have problem let me know...

how to re-enable the close(X) button

I am working on vb6.net winforms. I have created two buttons named yes and no Respectively.I have disabled the close button use the following code shown below:
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim myCp As CreateParams = MyBase.CreateParams
myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
Return myCp
End Get
End Property
My question is I want to re - enable it when the user completes his requirement by clicking on the yes button.
There is no need to Override the CreateParams. You could easily turn off/on the Close button changing the property ControlBox of your form
Private Sub ClickMe(sender as Object, e as EventArgs) Handles Button1.Click
Me.ControlBox = True
End Sub