Custom minimize button loses original minimize button animation - vb.net

I would like to know if there is a way of keeping the Original Animation to keep my VB application as close to original as possible.
Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
End Sub
I expect the minimize action to play the animation that ocurs in most if not all of the windows applications.
[Edit]:
All the apps I have installed have the default minimize animation because they all use the same windows bar, but my program doesn't because I find it not very attractive, so I created my own Print By programming the button myself I lose the animation.

You could use the opacity to create an animation of sorts.
Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
For disapper As Single = 1.0! To 0 Step -0.2!
Me.Opacity = disapper
'Me.Refresh()
System.Threading.Thread.Sleep(50)
Next
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
Me.Opacity = 1.0!
End Sub

So I found that the problem is that my formborderstyle propety is set to none and the animation isn't shown when it is set to none I solved it by doing this, but even tho it works it worses things even more... For now I will stick with having no animation at all unless someone finds a solution. Thanks.
Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
Me.FormBorderStyle = FormBorderStyle.FixedSingle
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
Me.FormBorderStyle = FormBorderStyle.None
End Sub

Any app minimization animation is in windows settings, and can be turned off anyway. I Don't think you can force it if you have it disabled in your userprofile System Properties settings.
This can be found in sysdm.cpl in Advanced -> Performance

Related

Use mouse scroll anywhere on screen in VB.net

I'm trying to setup global shortcut using mouse button and scrolling combination, (scrolling up (or down) whlie pressing right mouse button).
I'm thinking I should try to react to mouse scroll, but I have no idea how to detect mouse wheel outside of form.
Right now I got it to react to mouse click with timer and setting form to always on top (no prolem with it being always on top), but no idea how to progress with it.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
TextBox1.Text = "YES"
Else
TextBox1.Text = "NO"
End If
End Sub
Goal is to make it minimize aplications set by user with that shortcut, just in case that info is helpful. I have rest of the code just cant figure that part out.
EDIT:
It works inside form with this:
Private Sub ListBoxCHOWAJ_MouseWheel(sender As Object, e As MouseEventArgs) Handles ListBoxCHOWAJ.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
If e.Delta > 0 Then
TextBox1.Text = "up"
Else
TextBox1.Text = "down"
End If
End If
End Sub
You need a mouse hook to detect scrolling outside your application. Have a look at my InputHelper library and its low-level mouse hook.
To include it in your project, download the compiled DLL and add it as a reference:
Right-click your project in the Solution Explorer and press Add Reference...
Go to the Browse tab.
Locate the InputHelper DLL-file, select it and press OK.
You can do all your logic in the hook's MouseWheel event handler, no need for the timer any longer:
Imports InputHelperLib
Public Class Form1
Dim WithEvents MouseHook As New InputHelper.Hooks.MouseHook
Private Sub MouseHook_MouseWheel(sender As Object, e As InputHelperLib.InputHelper.Hooks.MouseHookEventArgs) Handles MouseHook.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
'Do something...
End If
End Sub
End Class
There's no need to check e.Delta like you did because it will never be 0.

Improve UI responsiveness on windows form application

I am currently working on a project and decided to create a user interface for it using visual studio with a windows forms application(Visual Basic).
The problem I'm facing is that the user interface doesn't respond as quickly and smoothly as I'd like it to.
Mainly, I am using pictures as buttons to make the user form look more modern.
However, when I hover my mouse over a "button" it takes a while until the "highlighted button" appears.
P1 is the picture of the "normal button" and P2 is the picture of the "highlighted button".
Here is the short code I have for now:
Public Class Main
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_MouseHover(sender As Object, e As EventArgs) Handles P1.MouseHover
P1.Visible = False
P2.Visible = True
End Sub
Private Sub P2_MouseClick(sender As Object, e As MouseEventArgs) Handles P2.MouseClick
'Call cmdInit()
'Call cmdConnectRobot()
'Call cmdUnlock()
End Sub
Private Sub Main_MouseHover(sender As Object, e As EventArgs) Handles Me.MouseHover
If P2.Visible = True Then
P2.Visible = False
P1.Visible = True
End If
End Sub
Private Sub P4_Click(sender As Object, e As EventArgs) Handles P4.Click
End Sub
End Class
Another problem I'm facing is that when I call other subs, the user form becomes unresponsive while the sub is running.
I researched and found that I could implement multi threading or async tasks but I'm a bit lost and would be extremely grateful if someone could guide me or point me in the right direction.
Thanks in advance!!
In this case your UI is responsive, however the MouseHover event is only raised once the mouse cursor has hovered over the control for a certain amount of time (default is 400 ms), which is what is causing the delay.
What you are looking for is the MouseEnter event, which is raised as soon as the cursor enters ("touches") the control:
Private Sub P1_MouseEnter(sender As Object, e As EventArgs) Handles P1.MouseEnter
P1.Visible = False
P2.Visible = True
End Sub
You can then use that together with the MouseLeave event on the second picture box to switch back to the non-highlighted image:
Private Sub P2_MouseLeave(sender As Object, e As EventArgs) Handles P2.MouseLeave
P1.Visible = True
P2.Visible = False
End Sub
However switching picture boxes like this is not optimal. I recommend you to look into how you can use Application Resources, then modify your code to only switch the image that one picture box displays.
Here are the basic steps:
Right-click your project in the Solution Explorer and press Properties.
Select the Resources tab.
To add an image either:
a. Drag and drop the image onto the resource pane.
b. Click the arrow next to the Add Resource... button and press Add Existing File....
Now, in your code add this right below Public Class Form1:
Dim ButtonNormal As Image = My.Resources.<first image name>
Dim ButtonHighlighted As Image = My.Resources.<second image name>
Replace <first image name> and <second image name> with the names of your button images.
Now you only need one picture box for the button:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
P1.Image = ButtonNormal
End Sub
Private Sub P1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles P1.MouseEnter
P1.Image = ButtonHighlighted
End Sub
Private Sub P1_MouseLeave(sender As System.Object, e As System.EventArgs) Handles P1.MouseLeave
P1.Image = ButtonNormal
End Sub
I'll start by saying i'm not a programmer by trade, and i'm sure someone will point out better ways of doing these things, but in regards to the threading question it's fairly simple to implement.
Imports System.Threading
Public Class Form1
Dim WorkerThread As New Thread(AddressOf DoWork)
'WorkerThread' can be any name you like, and 'DoWork' is the name of the sub you want to run in the new thread, and is launched by calling:
WorkerThread.start()
However there is a catch, the new thread is not able to interact directly with the GUI, so you cannot change textbox text etc... I find the easiest way to get changes made to the GUI is to drag a timer onto your form, and have the new thread change variables (pre-defined just below Public Class Form1), then use the Timer1 Tick event to monitor the variables and update the GUI if there are any changes.

Un-maximize size of the form

I'm having a little something in my program that's bothering me and I would like some help please.
My form size is: 1366; 768 (that's the max resolution of the laptop I use). The form loads and it's a little bit smaller than my screen size, so I changed the load event of the form so that it starts maximized.
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
End Sub
Now, when I press the maximize button while the form is maximized (un-maximize), the form gets a little bit smaller. Can I choose what size to un-maximize? I want the form to be 800 x 600 when I press that button.
Thanks in advance.
Well, if you need the un-maximized event to be different than the designer size, then just set it in your OnLoad override:
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
Me.WindowState = FormWindowState.Maximized
Me.Size = New Size(800, 600)
End Sub

VB.net - Mousehover not sensitive

I made picturebox which would change if the mover is over the image but it takes too long. can someone help me please?
Here's my code:
Private Sub button2_MouseHover(sender As Object, e As EventArgs) Handles button2.MouseHover
button1.Visible = True
button2.Visible = False
End Sub
Private Sub button1_MouseLeave(sender As Object, e As EventArgs) Handles button1.MouseLeave
button1.Visible = False
button2.Visible = True
End Sub
Thanking you in advance
If you want to adjust the time the mouse must pause over the image, the docs say;
A typical use of MouseHover is to display a tool tip when the mouse pauses on a control within a specified area around the control (the "hover rectangle"). The pause required for this event to be raised is specified in milliseconds by the MouseHoverTime property.
To achieve that, simply change the value of SystemInformation.MouseHoverTime in your code.
Alternatively, as someone has commented, use a MouseEnter event instead, this will trigger instantly.

VB.net Borderless Form Maximize over Taskbar

When I maximize my borderless form, the form covers the entire screen including the taskbar, which I don't want it to do. I'm finding it very difficult to find a solution to my problem on the net, all I've come up with is the below code, which displays the taskbar for a short time, but then disappears and the form still takes up the entire screen.
Private Sub TableLayoutPanel1_DoubleClick(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.DoubleClick
If e.Location.Y < 30 Then
Me.WindowState = FormWindowState.Maximized
Me.ControlBox = True
End If
End Sub
I'm starting to think the only way to solve my problem is to find the screen size height minus the taskbar height to get the form height, but I'm hoping there might be a simpler answer.
Use the form's Load event to set its maximum size, like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.MaximumSize = Screen.FromRectangle(Me.Bounds).WorkingArea.Size
End Sub
Maximizing it now restricts the size to the monitor's working area, which is the monitor size minus the taskbars.
Use the "working area" of the screen:
Me.Bounds = Screen.GetWorkingArea(Me)
You should better do this:
Me.MaximumSize = Screen.FromControl(Me).WorkingArea.Size
Because some users use multiple monitors. So if you use Hans Passant's way, when user maximize application to secondary monitor, application will get primary's monitor working area size and will look awful!!!
I think this is a better way to solve your problem
Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Normal
Me.StartPosition = FormStartPosition.Manual
With Screen.PrimaryScreen.WorkingArea
Me.SetBounds(.Left, .Top, .Width, .Height)
End With
End Sub