VB Setting up a click-through but visible windows form - vb.net

I have a form which is set to TopMost. It is basically a gaming tool which runs in-game and only accepts key bindings. Now, I need to disable all click events in the form and making "Click-through" so that the users will not be interrupted when they accidentally click or right-click the form. If I will not use a click-through form, clicking events will focus the form which shows the taskbar and makes the game slow. I'll just use a key binding to enable/disable that click-through feature. How do you set this in a Windows form using VB.Net?

Override CreateParams() in your Form and set the WS_EX_TRANSPARENT flag:
Public Class Form1
Private Const WS_EX_TRANSPARENT As Integer = &H20
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
End Class
This will give the "Click-through" functionality that you're looking for.

Related

How to prevent WinForms app from flickering

I found the following code which cancels the flickering in a WinForms application:
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H2000000
Return cp
End Get
End Property 'CreateParams
The issue comes when "minimizing and maximizing the form, all controls are invisible. Removing &H2000000 fix the problem, but the controls start to flicker" like it says in this comment on this question.
There's any update or way to solve this?

Prevent focus on Control

I'm working on a project where I have an input with validation.
Next to it there are a set of buttons which you can press. When you focus the input the buttons will be shown. But when you click on 1 of the buttons the focus of the input is gone and the validation will start and the buttons will be hidden.
What I want is that the input focus is not lost, at any time.
Is it possible to not activate/focus the buttons when you click on them or is it possible to force focus on the input without losing it?
I have tried this on the Control, but seems to be only working on a form:
Const WS_EX_NOACTIVATE As Integer = &H8000000
Const WS_EX_TOOLWINDOW As Integer = &H80
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim ret As CreateParams = MyBase.CreateParams
ret.ExStyle = ret.ExStyle Or WS_EX_NOACTIVATE Or WS_EX_TOOLWINDOW
Return ret
End Get
End Property
You can create your custom button and make it non-selectable:
Public Class MyButton
Inherits Button
Public Sub New()
SetStyle(ControlStyles.Selectable, False)
End Sub
End Class
If you don't want to create custom control, set CausesValidation = False for your normal button and it will get focus, but doesn't cause validation.
Set the TabStop property of the buttons to false.
By setting this to false you'll stop them receiving focus when the user tabs, but they'll still be clickable.

Alt + Tab is not showing the forms when Form.Owner is set

I have an application (VB.NET) with the name MainForm and other child forms without using MDI Container. The child forms are based on assign to the MainForm with Me.Owner = MainForm
When I press Alt + Tab, for switching between these form, the Windows is showing the MainForm only unless I remove Me.Owner = Nothing it's working as expected again.
I tried Call SetWindowLong on Onload function but not luck. I am still looking for the solution.
EDIT
Actually it's easy for reproduced, I created very simple project.
Simple Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Form2 As New Form2
Form2.Owner = Me ' Alt+Tab only Show Form1, not showing Form2.
Form2.Show()
End Sub
Disable Owner property is working fine again.
Please check my teamview recording. Actually it's original form without change anything.
#Royce Your solution still not working, it's throw Win32Exception from my side.
Try to change the ShowInTaskbar Property to True.
Ran your example on my machine (Windows 10) and both windows showed up in my Alt-Tab menu (screenshot).
In your original program, make sure that the FormBorderStyle property of either window is not set to one of the two ToolWindow options and that ShowInTaskbar is True.
Otherwise, you might try changing the CreateParams of your second form to exclude WS_EX_TOOLWINDOW extended style bit by doing something similar to this:
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle And (Not &H80)
Return cp
End Get
End Property

Disable Close Button without Disabling Icon [duplicate]

This question already has answers here:
Visually remove/disable close button from title bar .NET
(13 answers)
Closed 8 years ago.
my problem is so simple but I can't seem to get it solved.
I just want to remove the Close Button from my form and don't remove the icon.
I used ControlBox = false but it removes the form's icon as well, I just want to keep it.
Is there anyway I can do it either by code or properties?
Add it under Public Class Form ... :
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim Param As CreateParams = MyBase.CreateParams
Param.ClassStyle = Param.ClassStyle Or &H200
Return Param
End Get
End Property
It should work perfectly!
If you want something full of capabilities, then you could use my SystemMenuManager By Elektro Class.
Just add all the code into a single Class and use it like in the example below:
Public Class Form1
Dim SystemMenu As New SystemMenuManager(Me)
Private Shadows Sub Load() Handles MyBase.Load
' Disables the 'Close' button and 'Close' menu-item.
SystemMenu.SetItemState(SystemMenuManager.Item.Close,
SystemMenuManager.ItemState.Disabled)
End Sub
End Class

Visually remove/disable close button from title bar .NET

I have been asked to remove or disable the close button from our VB .NET 2005 MDI application. There are no native properties on a form that allow you to grey out the close button so the user cannot close it, and I do not remember seeing anything in the form class that will allow me to do this.
Is there perhaps an API call or some magical property to set or function to call in .NET 2005 or later to do this?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information:
I need to maintain the minimize/maximize functionality
I need to maintain the original title bar because the form's drawing methods are already very complex.
Based on the latest information you added to your question, skip to the end of my answer.
This is what you need to set to false: Form.ControlBox Property
BUT, you will lose the minimize and maximize buttons as well as the application menu (top left).
As an alternative, override OnClose and set Cancel to true (C# example):
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.ApplicationExitCall)
{
e.Cancel = true;
}
base.OnFormClosing(e);
}
If neither of these solutions are acceptable, and you must disable just the close button, you can go the pinvoke/createparams route:
How to disable close button from window form using .NET application
This is the VB version of jdm's code:
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads 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
You can disable the close button and the close menu item in the system menu by changing the "class style" of the window. Add the following code to your form:
const int CS_NOCLOSE = 0x200;
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_NOCLOSE;
return cp;
}
}
This will not just stop the window from getting closed, but it will actually grey out the button. It is C# but I think it should be easy to translate it to VB.
Here is a simple way to remove the close button:
1. Select the Form
2. Now go to Properties.
3. Find ControlBox and change the value to False.
This will remove all the Control Buttons (e.g. Minimize, Maximize, Exit) and also the icon also that is in the to left corner before the title.
You should be able to override the OnClose event of the form. This is common when an application minimizes to the System Tray when "closed".
When you press the X box on the form.
The Form1_Closing is done first, then the Form1_Closed is done.
The e.Cancel = True in the Form1_Closing - prevents Form1_Closed from being called therefore, leaving your form still active.
Prevent to close the form, but hide it:
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.WindowState = FormWindowState.Minimized
Me.Visible=false
e.Cancel = True
End Sub
You can set the ControlBox property to False, but the whole title bar will be gone but the title itself...
What jmweb said here is OK as well. The X close button won't go if you cancel the event on form closing. But doing so, you need to release the processes the form needs and then closing the form.
Me.Dispose()
Me.Close()
This worked for me using Menu Strip.
Select (or click) the form itself
Click on events in the property window (the little lightning bolt icon).
Look for Form.Closing and double click it.
Then type: e.cancel=true
Making a Form without a Titlebar in Visual Basic.
Go to Form Properties and set both ControlBox and ShowIcon to false.
Then, clear all the text from the form's text property.
go to properties and select from bored style as none
Just select the required form and in the properties section, set controlBox = false
That just worked for me :)
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Beep()
e.Cancel = True
End Sub