Maximize form but do not cover the taskbar - vb.net

When I click the button to maximize my form, it covers the entire screen including the taskbar. I managed to find a solution and it works, I used my code in form load event but I cannot return the form into normal state.
Private Sub frmDashboard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Top = Screen.PrimaryScreen.WorkingArea.Top
Me.Left = Screen.PrimaryScreen.WorkingArea.Left
Me.Height = Screen.PrimaryScreen.WorkingArea.Height
Me.Width = Screen.PrimaryScreen.WorkingArea.Width
End Sub
Private Sub btnMaximizeMin_Click(sender As Object, e As EventArgs) Handles btnMaxMin.Click
If Me.WindowState = FormWindowState.Normal Then
'maximize but dont cover taskbar
Me.Top = Screen.PrimaryScreen.WorkingArea.Top
Me.Left = Screen.PrimaryScreen.WorkingArea.Left
Me.Height = Screen.PrimaryScreen.WorkingArea.Height
Me.Width = Screen.PrimaryScreen.WorkingArea.Width
Else
Me.WindowState = FormWindowState.Normal
End If
End Sub

The problem is that you aren't maximising the form. You specifically DON'T want to maximise the form because that covers the Windows Task Bar. You can't set the WindowState "back" to Normal because it's already in that state, because it never leaves that state. It's up to you to remember the state for yourself and also the previous bounds, e.g.
Private isMaximised As Boolean = False
Private normalBounds As Rectangle
Private Sub MaximiseOrRestore()
isMaximised = Not isMaximised
If isMaximised Then
normalBounds = Bounds
Bounds = Screen.PrimaryScreen.WorkingArea
Else
Bounds = normalBounds
End If
End Sub

Related

Autoscroll inconsistent

Im having a hardtime figuring What went wrong ,My program Has 3 buttons (Bread,Coffe,Pasta) and a panel, each buttons show a different form,
I currently set the Panel's autoscroll to true
MenuPanel.AutoScroll = True
The Coffee,bread and pasta button function is
Private Sub btnCoffee_Click(sender As Object, e As EventArgs) Handles btnCoffee.Click
MenuPanel.Controls.Clear()
CoffeeForm.TopLevel = False
CoffeeForm.WindowState = FormWindowState.Maximized
CoffeeForm.Visible = True
MenuPanel.Controls.Add(CoffeeForm)
CoffeeForm.Show()
End Sub
Private Sub btnBread_Click(sender As Object, e As EventArgs) Handles btnBread.Click
MenuPanel.Controls.Clear()
BreadForm.TopLevel = False
BreadForm.WindowState = FormWindowState.Maximized
BreadForm.Visible = True
MenuPanel.Controls.Add(BreadForm)
BreadForm.Show()
End Sub
Private Sub btnPasta_Click(sender As Object, e As EventArgs) Handles btnPasta.Click
MenuPanel.Controls.Clear()
PastaForm.TopLevel = False
PastaForm.WindowState = FormWindowState.Maximized
PastaForm.Visible = True
MenuPanel.Controls.Add(PastaForm)
PastaForm.Show()
End Sub
The autoscroll works for the first time and after Some clicks on the The buttons (Maybe 4-5 clicks and scrolled with Mousewheel)
the Vertical Scrollbar disappear from the panel
what went wrong?
I am currently using vb.net 2019
1:

Windows form to be shown topmost from taskbar

Using VB.NET I create a form with TopMost=True, FormBorderStyle=FixedDialog, ControlBox=False, text="", size 400x50.
I display this form above Windows 10 taskbar. When I click on the taskbar, the form occupies only a taskbar's portion, the form is moved behind the taskbar. The form remains topmost if it is moved on the working area of the screen.
Is there a way to keep the form topmost above Windows 10 taskbar?
Sample code is given below. Using the arrows keys the form can be relocated for testing.
***UPDATE sample code added.
Public Class Form1
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Dim istep As Integer = 1
If e.Alt Then istep = 10
Select Case e.KeyCode
Case Keys.Down
Me.Top += istep
Case Keys.Up
Me.Top -= istep
Case Keys.Left
Me.Left -= istep
Case Keys.Right
Me.Left += istep
End Select
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim b As Rectangle = My.Computer.Screen.Bounds
Dim w As Rectangle = My.Computer.Screen.WorkingArea
Me.Top = w.Height
Me.Left = 500
End Sub
End Class
You can resize the dialog manually according to the screen size in the Load function of the form.
The code would look something like (posting c# code):
Location = new Point(0, 0);
TopMost = true;
var screen = Screen.FromHandle(Handle);
Size = new Size(screen.Bounds.Width, screen.Bounds.Height);

Temporarily disable all other windows vb.net

I'm making a color picker and I want it so on button press it will disable everything but the program (like what snipping tool does, but not add grey to the screen).
So when I click somewhere, it can capture the color from mouse pointer, and re-enable all other programs. The reason why I want it to disable all other programs is so when you click to pick the color then it won't interfere with anything and change the color.
Any help is very much appreciated.
In this sample, there are two form FrmDesktopDisplay and FrmCaller
Public Class FrmDesktopDisplay
Public myResultStts As Boolean = False
Public myResultValue As Integer = -1
Private Sub FrmDesktopDisplay_Click(sender As Object, e As EventArgs) Handles Me.Click
'Here your code, not just in click event, you must modify according to your need
'And for example the result as follow:
myResultStts = True
myResultValue = 30
Me.Hide()
End Sub
Private Sub FrmDesktopDisplay_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
'Here Just Hide, don't be closed or disposed, because you need the result
Me.Hide()
End If
End Sub
Private Sub FrmDesktopDisplay_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'First you must hide this form, to take desktop image
Me.Hide()
'This to make borderless
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.Refresh()
Dim g As Graphics
Dim bmp As Bitmap
'This to take size of screen
Dim myWidth As Integer = My.Computer.Screen.Bounds.Size.Width
Dim myHeight As Integer = My.Computer.Screen.Bounds.Size.Height
'This to make this form's Size is same as screen size
Me.Width = myWidth
Me.Height = myHeight
'Create Blank Bitmap with size as same as screen size
bmp = New Bitmap(myWidth, myHeight)
'Create graphic with composition of bitmap
g = Graphics.FromImage(bmp)
'Take Image from screen, and move to graphic g
g.CopyFromScreen(0, 0, 0, 0, bmp.Size)
'Relocate this form to (0,0)
Me.Top = 0
Me.Left = 0
'Set this form's backgroundimage with bitmap of bmp, that have been taken by graphic g
Me.BackgroundImage = bmp
Me.Show()
Me.Refresh()
End Sub
End Class
The Caller Form
Public Class FrmCaller
Private Sub ButtonToPick_Click(sender As Object, e As EventArgs) Handles ButtonToPick.Click
'Hide the caller form if don't need this form when you run your sniff or clip
Me.Hide()
FrmDesktopDisplay.ShowDialog()
'To show again this form
Me.Show()
'Take the result from FrmDesktopDisplay, you can do it, because the form still active
Dim myResultStts As Boolean = FrmDesktopDisplay.myResultStts
Dim myResultValue As Integer = FrmDesktopDisplay.myResultValue
'Here you close the FrmDesktopDisplay
FrmDesktopDisplay.Close()
FrmDesktopDisplay.Dispose()
PictureBox1.Visible = False
End Sub
End Class
'Here you must have PictureBox1 in Your Form, and the form must be as parent container
Private Sub ButtonToPick_Click(sender As Object, e As EventArgs) Handles ButtonToPick.Click
PictureBox1.Visible = False
Me.Refresh()
Dim g As Graphics
Dim bmp As Bitmap
bmp = New Bitmap(Me.Width, Me.Height)
g = Graphics.FromImage(bmp)
Dim myFRect As Rectangle
myFRect = Me.RectangleToScreen(Me.DisplayRectangle)
g.CopyFromScreen(myFRect.Left, myFRect.Top, 0, 0, bmp.Size)
PictureBox1.Top = 0
PictureBox1.Left = 0
PictureBox1.Width = Me.Width
PictureBox1.Height = Me.Height
PictureBox1.Image = bmp
PictureBox1.Visible = True
PictureBox1.BringToFront()
'Me.Refresh()
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
'Here you process before picture box be hidden again
PictureBox1.Visible = False
End Sub

MouseLeave and Enter on Form

I am trying to make a form fade away from the taskbar if the user is not currently hovering over the form with the mouse. (The form contains hyperlinks). On the adverse, i want the form to reset to its original position if the mouse comes back to the form. However, for whatever reason, it seems as thought the enter and leave evens fire in sync when either even occurs. If i leave the form with my mouse, both events fire. If i enter the form, both events fire. What is wrong?
Sub FormLeave()
MouseForm = False
Do Until y = Screen.PrimaryScreen.WorkingArea.height + 50
Sleep(10)
y = y + 1
Me.Location = New Point(x, y)
If MouseForm = True Then
Exit Sub
End If
Loop
End Sub
Sub FormEnter()
MouseForm = True
Me.Visible = True
x = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
Me.Location = New Point(x, y)
End Sub
Animation is best done with a timer (i.e. event driven) rather than sleeping.
You have to make sure the cursor has left the boundary of the form, as a leave event is fired on the form when the mouse enters a control within the form.
I added a longer delay when the mouse initially leaves the form as otherwise it is really annoying.
To demonstrate, use this code with a new Windows Forms project. Add controls to the form if you want to confirm it does not misbehave when you put the mouse over a control on the form.
Public Class Form1
Dim x As Integer
Dim y As Integer
Dim tim As Timer
Sub MoveFormAway(sender As Object, e As EventArgs)
' called on timer tick event
Dim destY = Screen.PrimaryScreen.WorkingArea.Height - 100
If y >= destY Then
tim.Enabled = False
Exit Sub
End If
' interval for form position changes
tim.Interval = 10
y += 2
Me.Location = New Point(x, y)
End Sub
Sub FormBodyLeave(sender As Object, e As EventArgs)
' If the mouse has not left the outside of the form,
' i.e. it has entered a control on the form, then exit:
If Me.ClientRectangle.Contains(PointToClient(Cursor.Position)) Then
Exit Sub
End If
If tim Is Nothing OrElse Not tim.Enabled Then
x = Me.Location.X
y = Me.Location.Y
tim = New Timer
' initial interval until form starts running away
tim.Interval = 1000
AddHandler tim.Tick, AddressOf MoveFormAway
tim.Enabled = True
End If
End Sub
Sub FormBodyEnter(sender As Object, e As EventArgs)
If tim IsNot Nothing Then
tim.Enabled = False
End If
MoveToDefaultPosition()
End Sub
Sub AddFormDisappearingHandlers()
AddHandler Me.MouseEnter, AddressOf FormBodyEnter
AddHandler Me.MouseLeave, AddressOf FormBodyLeave
End Sub
Sub MoveToDefaultPosition()
x = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
y = Screen.PrimaryScreen.WorkingArea.Height - Me.Height
Me.Location = New Point(x, y)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MoveToDefaultPosition()
AddFormDisappearingHandlers()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Tidy up.
If tim IsNot Nothing Then
tim.Dispose()
End If
End Sub
End Class

Resize Bordless Form is Glitchy

My code to resize my form from the right hand side works, but is really glitchy or has a lot of lag. All my images blur and my drawn rectangles flicker while I'm resizing. Everything is normal however once mouseup.
I put the formborderstyle from borderless to sizable and the form resizes normally.
Hoping someone might be able to point out what is wrong with my code.
Dim myresize As Boolean = False
Dim cursorx As Integer
Dim cursory As Integer
Private Sub TableLayoutPanel1_MouseDown(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseDown
If e.Location.X > Me.Width - 7 And e.Location.Y > 11 And e.Location.Y < Me.Height - 10 Then
myresize = True
cursorx = Windows.Forms.Cursor.Position.X - Me.Width
cursory = Windows.Forms.Cursor.Position.Y - Me.Height
End If
End Sub
Private Sub TableLayoutPanel1_MouseMove(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseMove
If myresize = True Then
Me.Width = Windows.Forms.Cursor.Position.X - cursorx
End If
End Sub
Private Sub TableLayoutPanel1_MouseUp(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseUp
myresize = False
End Sub
For the flickering, the form does not pass the DoubleBuffered property to its child controls, such as your TableLayoutPanel. Instead try adding a new class to your project that will doubleBuffer the tableLayoutPanel.
Public Class DoubleBufferedTableLayoutPanel
Inherits TableLayoutPanel
Public Sub New()
Me.DoubleBuffered = True
End Sub
End Class
Build your project and the new version of the TableLayoutPanel will be in your toolbox as DoubleBufferedTableLayoutPanel. From there just use it as you would the TableLayoutPanel in your code.