Click and drag implementation on a rectangle VB.NET - vb.net

So my problem is that I want to have multiple rectangles on a form at a time. However I also want these rectangles to be able to be clicked and dragged across the form.
This is my current code for clicking and dragging a rectangle that was drawn onto the form using the toolbox.
Public Class DragRectangle
Dim Go As Boolean
Dim LeftSet As Boolean
Dim TopSet As Boolean
Dim HoldLeft As Integer
Dim HoldTop As Integer
Dim OffLeft As Integer
Dim OffTop As Integer
Private Sub obj1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseUp
Go = False
LeftSet = False
TopSet = False
End Sub
Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseDown
Go = True
End Sub
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseMove
If Go = True Then
HoldLeft = (Control.MousePosition.X - Me.Left)
HoldTop = (Control.MousePosition.Y - Me.Top)
If TopSet = False Then
OffTop = HoldTop - sender.Top
TopSet = True
End If
If LeftSet = False Then
OffLeft = HoldLeft - sender.Left
LeftSet = True
End If
sender.Left = HoldLeft - OffLeft
sender.Top = HoldTop - OffTop
End If
End Sub
End Class
This works fine for one rectangle, although this requires the rectangles to be pre-drawn onto the form using the toolbox.
What I would like is a rectangle gets drawn by clicking a button on the form, and the newly drawn rectangle can also be clicked and dragged into a new location.
Is this possible?
Thanks for any help

Working example:
Public Class Form1
Private Property Rectangles As New List(Of DrgRectangle)
Private Property curRect As DrgRectangle
Private _x As Integer
Private _y As Integer
Private Sub loadme() Handles Me.Load
'load the rectangle in list
Rectangles.Add(New DrgRectangle With {.Rect = New Rectangle(20, 20, 20, 20)})
End Sub
Private Sub FormMouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
_x = e.X
_y = e.Y
For Each rect In Rectangles
If rect.Rect.Contains(e.X, e.Y) Then
curRect = rect
Exit For
End If
Next
End Sub
Private Sub FormMouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
If curRect IsNot Nothing Then
curRect.Rect = New Rectangle(New Point(curRect.Rect.Location.X + (e.X - _x), curRect.Rect.Location.Y + (e.Y - _y)), curRect.Rect.Size)
Me.Refresh()
End If
End If
_x = e.X
_y = e.Y
End Sub
Private Sub FormPaint(sender As Object, e As PaintEventArgs) Handles me.Paint
For Each rect In Rectangles
e.Graphics.DrawRectangle(Pens.Black, rect.Rect)
Next
End Sub
End Class
Public Class DrgRectangle
Public Rect As New Rectangle
'add more properties as needed
End Class

Related

How to save location of movable button in visual basic winform?

I have a movable button. the new location is save in my setting once it is mouse up event. when I move the button, sometimes the button disappear, after I re run it, it completely disappear.
How to save the button location before I close the app?
Public Class Form1
Dim x, y As Integer
Dim newpoint As New Point
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.button1_x70123454440211bghff > 0 Or My.Settings.button1_y70123454440211bghff > 0 Then
Me.Button1.Location = New Point(My.Settings.button1_x70123454440211bghff, My.Settings.button1_y70123454440211bghff)
End If
End Sub
Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
newpoint = Control.MousePosition
newpoint = Control.MousePosition
newpoint.X -= x
newpoint.Y -= y
Button1.Location = newpoint
End If
End Sub
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
x = Control.MousePosition.X - Button1.Location.X
y = Control.MousePosition.Y - Button1.Location.Y
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
My.Settings.button1_x70123454440211bghff = x
My.Settings.button1_y70123454440211bghff = y
My.Settings.Save()
End Sub
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
My.Settings.richtextbox1 = RichTextBox1.Text
End Sub
End Class
Follow this answer for button movement, and add some checks to keep the control within its parent container. For the setting, I like to keep things simple, and will take advantage of the ability of Visual Studio to store a struct in the settings, so instead I will use a System.Drawing.Point
Now you ask "How to save the button location before I close the app?" Well you can do it in Form_Close, or do it in the MouseUp event. Since you do it in the MouseUp, I will do the same. And I will load the last position in Form_Load
Public Class Form1
Private initialCursorPosition As System.Drawing.Point
Private intialButtonPosition As System.Drawing.Point
Private buttonIsMoving As Boolean = False
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
Dim b = DirectCast(sender, Button)
initialCursorPosition = System.Windows.Forms.Cursor.Position
intialButtonPosition = b.Location
buttonIsMoving = True
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
buttonIsMoving = False
My.Settings.Button1Location = DirectCast(sender, Button).Location
End Sub
Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
If buttonIsMoving Then
Dim b = DirectCast(sender, Button)
Dim newX = intialButtonPosition.X - (initialCursorPosition.X - Cursor.Position.X)
Dim newY = intialButtonPosition.Y - (initialCursorPosition.Y - Cursor.Position.Y)
newX = Math.Max(Math.Min(newX, b.Parent.ClientSize.Width - b.Width), 0)
newY = Math.Max(Math.Min(newY, b.Parent.ClientSize.Height - b.Height), 0)
b.Location = New Point(newX, newY)
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.Location = My.Settings.Button1Location
End Sub
End Class

Draw a rectangle with the mouse, but not when OpenfileDialog is busy

I have a Form1 and a PictureBox. I have also subscribed to the MouseDown, MouseMove and MouseUp events in order to be able to draw a rectangle on the PictureBox with the mouse. In itself, it works fine. Now I am using an OpenFileDialog. If I select the file in the window and click on 'OK', the dialog disappears, but – and this is my problem – a rectangle is drawn immediately because I moved the mouse. I don't want that to happen at the moment. I've already tried to use a Boolean variable to lock the MouseMove procedure, but unfortunately that didn't work.OpenFileDialog Here you can see the accidentally created rectangle
Imports Microsoft.WindowsAPICodePack.Dialogs
Public NotInheritable Class Form1
Private Mausstartpunkt As Point ' Mouse start point
Private Mausendpunkt As Point ' Mouse end point
Public Shared Property Picture1 As Bitmap 'dann bleibt es im Anwendungs-Scope, wenn die Klasse verfällt.
Private Pfad_Bild As String = ""
Private Pfad_speichern As String = ""
Private It_is_allowed_to_draw As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If PictureBox1.Image IsNot Nothing AndAlso It_is_allowed_to_draw AndAlso Not String.IsNullOrEmpty(Pfad_Bild) Then
Dim rect As Rectangle = PointsToRectangle(Mausstartpunkt, Mausendpunkt)
AllesGrafische.Paint_the_Rectangle(e.Graphics, rect)
End If
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
Mausstartpunkt = System.Windows.Forms.Control.MousePosition
Mausstartpunkt = New Point(Mausstartpunkt.X - 8, Mausstartpunkt.Y - 31)
End If
If e.Button = MouseButtons.Right Then ' clears the rectangle
Mausstartpunkt = New Point(0, 0)
Mausendpunkt = New Point(0, 0)
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then
Mausendpunkt = System.Windows.Forms.Control.MousePosition
Mausendpunkt = New Point(Mausendpunkt.X - 8, Mausendpunkt.Y - 31)
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button = MouseButtons.Left Then
Mausendpunkt = System.Windows.Forms.Control.MousePosition
Mausendpunkt = New Point(Mausendpunkt.X - 8, Mausendpunkt.Y - 31)
PictureBox1.Invalidate()
End If
End Sub
Public Shared Function PointsToRectangle(ByVal p1 As Point, ByVal p2 As Point) As Rectangle 'https://www.vb-paradise.de/index.php/Thread/20037-Rechteck-mit-Maus-zeichnen-Erledigt/?postID=124893#post124893
Dim r As New Rectangle With {
.Width = Math.Abs(p1.X - p2.X),
.Height = Math.Abs(p1.Y - p2.Y),
.X = Math.Min(p1.X, p2.X),
.Y = Math.Min(p1.Y, p2.Y)
}
Return r
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = Nothing
Using OFD As New CommonOpenFileDialog
OFD.Title = "Datei zum Öffnen auswählen"
OFD.Filters.Add(New CommonFileDialogFilter("Bilder", ".jpg;.jpeg;.bmp"))
OFD.Filters.Add(New CommonFileDialogFilter("PNG", ".png"))
OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If OFD.ShowDialog() = CommonFileDialogResult.Ok Then
Pfad_Bild = OFD.FileName
Pfad_speichern = Pfad_Bild.Substring(0, Pfad_Bild.LastIndexOf("\", StringComparison.Ordinal) + 1) ' for example "C:\Users\myName\Pictures\", or "C:\Users\myName\Desktop\"
Else
Return
End If
End Using
Picture1 = New Bitmap(Pfad_Bild)
PictureBox1.Image = Picture1
It_is_allowed_to_draw = True
End Sub
And this is the code for my graphics class
Imports System.Drawing.Drawing2D
Public NotInheritable Class AllesGrafische
Public Shared Sub Paint_the_Rectangle(ByVal g As Graphics, ByVal recta As Rectangle)
If g IsNot Nothing Then
g.SmoothingMode = SmoothingMode.AntiAlias
g.CompositingQuality = CompositingQuality.HighQuality
g.PixelOffsetMode = PixelOffsetMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBilinear
Using Pen_Hellblau As Pen = New Pen(Color.FromArgb(0, 200, 255), 1.0F)
g.DrawRectangle(Pen_Hellblau, recta)
End Using
End If
End Sub
End Class
Set a Boolean field to True on the MouseDown event and then only act on the MouseUp if that flag is set.

Get variable for two clicks on form

I am trying to assign two clicks to two variables on my Mouse_Down event on my form. Here is the starting code I am working with on Mouse_Down event. What I am trying to do is click two points on a form, get the X & Y location (these will then give me my button size). Example: First Click get X & Y, Second Click get X & Y then perform click of a button.... repeat this until I quit.
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
Dim XYClickOne, XYClickTwo
Dim count As Integer
count = 0
Do
XYClickOne = e.X & "," & e.Y
XYClickTwo = e.X & "," & e.Y
count = count + 1
Loop Until count = 2
Button1.PerformClick() 'After 2nd click, create button.
End Sub
Move those variables out to Form level so they are accessible by other methods, and will persist across clicks. Here's a quick example:
Public Class Form1
Private points(2) As Point
Private count As Integer = 0
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If e.Button = MouseButtons.Left Then
points(count) = New Point(e.X, e.Y)
count = count + 1
If count = 2 Then
count = 0
CreateButton(points(0), points(1))
End If
End If
End Sub
Private Sub CreateButton(ByVal ptA As Point, ByVal ptB As Point)
Dim pt As New Point(Math.Min(ptA.X, ptB.X), Math.Min(ptA.Y, ptB.Y))
Dim sz As New Size(Math.Abs(ptA.X - ptB.X) + 1, Math.Abs(ptA.Y - ptB.Y) + 1)
Dim btn As New Button
btn.Bounds = New Rectangle(pt, sz)
btn.Text = "X"
Me.Controls.Add(btn)
End Sub
End Class
Thought you might like a quick example of making a "rubber band selection". Click and DRAG on your form with the left mouse button:
The artifacts are from my screen recorder, in reality it drew smoothly with out leaving lines behind:
Code:
Public Class Form1
Private ptA, ptB As Point
Private count As Integer = 0
Private firstBoxDrawn As Boolean = False
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If e.Button = MouseButtons.Left Then
ptA = Me.PointToScreen(New Point(e.X, e.Y))
ptB = ptA
firstBoxDrawn = False
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If e.Button = MouseButtons.Left Then
If firstBoxDrawn Then
' erase the previous box by drawing it again
ControlPaint.DrawReversibleFrame(RectangleFromPoints(ptA, ptB), Color.Black, FrameStyle.Dashed)
End If
ptB = Me.PointToScreen(New Point(e.X, e.Y))
' draw the new box
ControlPaint.DrawReversibleFrame(RectangleFromPoints(ptA, ptB), Color.Black, FrameStyle.Dashed)
firstBoxDrawn = True
End If
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
If e.Button = MouseButtons.Left Then
' erase the box
ControlPaint.DrawReversibleFrame(RectangleFromPoints(ptA, ptB), Color.Black, FrameStyle.Dashed)
CreateButton(Me.PointToClient(ptA), Me.PointToClient(ptB))
End If
End Sub
Private Sub CreateButton(ByVal ptA As Point, ByVal ptB As Point)
Dim btn As New Button
btn.Text = "X"
btn.Bounds = RectangleFromPoints(ptA, ptB)
Me.Controls.Add(btn)
End Sub
Private Function RectangleFromPoints(ByVal ptA As Point, ByVal ptB As Point) As Rectangle
Dim pt As New Point(Math.Min(ptA.X, ptB.X), Math.Min(ptA.Y, ptB.Y))
Dim sz As New Size(Math.Abs(ptA.X - ptB.X) + 1, Math.Abs(ptA.Y - ptB.Y) + 1)
Return New Rectangle(pt, sz)
End Function
End Class

Need to change a mouse event args with a checkbox

I have a class that changes the mouse event args of a picturebox and allows the user to resize it during runtime. I am trying to add a checkbox for each picturebox that will maintain the aspect ratio of the picturebox when the checkbox is checked.
I can get it to work if I add the code separately for each checkbox on the form but I want to keep it in a separate class so that it works for any checkbox.
Here is the code for the class to resize the pictureboxes
Public Class ResizeableControl
Public WithEvents mControl As Control
Public mPreserveAspectRatio As Boolean
Dim AtRightEdge As Boolean = False
Dim AtBottomEdge As Boolean = False
Dim InBoxWidth As Boolean = False
Dim InBoxHeight As Boolean = False
Dim DraggingHorizontal As Boolean
Dim DraggingVerticle As Boolean
Dim DraggingCorner As Boolean
Const DragMarginWidth As Integer = 6
Const DragMarginHeight As Integer = 6
Public dragOrigin As Point
Dim MoveBox As Boolean = False
Dim LastPos As Point
Public Sub New(ByVal Control As Control, ByVal preserveAspectRatio As Boolean)
mControl = Control
mPreserveAspectRatio = preserveAspectRatio
End Sub
Private Sub mControl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseMove
If MoveBox Then
Dim movement As Point = Cursor.Position
' move image by the distance the mouse moved hoizontally and vertically
movement.Offset(-LastPos.X, -LastPos.Y)
mControl.Location = movement
ElseIf DraggingHorizontal Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y) 'How far did the mouse move? (Distance = newPoint - oldPoint)
If mPreserveAspectRatio = True Then
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
Else
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
End If
dragOrigin = e.Location 'Next time we will measure from the now-current mouse position
ElseIf DraggingVerticle Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
End If
dragOrigin = e.Location
ElseIf DraggingCorner Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
End If
dragOrigin = e.Location
Else
' Is mouse within right six-or-so pixels?
AtRightEdge = e.X > (mControl.Width - DragMarginWidth)
' Is mouse within bottom six-or-so pixels?
AtBottomEdge = e.Y > (mControl.Height - DragMarginHeight)
' Is mouse within the box?
InBoxWidth = e.X < (mControl.Width - DragMarginWidth)
InBoxHeight = e.Y < (mControl.Height - DragMarginHeight)
' Set the cursor accordingly
If (AtBottomEdge And AtRightEdge) Then
mControl.Cursor = Cursors.SizeNWSE
ElseIf (InBoxWidth And InBoxHeight) Then
mControl.Cursor = Cursors.SizeAll
ElseIf (AtBottomEdge) Then
mControl.Cursor = Cursors.SizeNS
ElseIf (AtRightEdge) Then
mControl.Cursor = Cursors.SizeWE
Else
mControl.Cursor = Cursors.Default
End If
End If
End Sub
Private Sub mControl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseDown
' If the user presses the mouse button at the bottom right corner, begin dragging
If (InBoxWidth And InBoxHeight) Then
Dim movement As Point = Cursor.Position
movement.Offset(-mControl.Location.X, -mControl.Location.Y)
LastPos = movement
MoveBox = True
ElseIf (AtBottomEdge And AtRightEdge) Then
dragOrigin = e.Location
DraggingCorner = True
ElseIf AtRightEdge Then
dragOrigin = e.Location
DraggingHorizontal = True
ElseIf AtBottomEdge Then
dragOrigin = e.Location
DraggingVerticle = True
End If
End Sub
Private Sub mControl_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseUp
' Stop dragging
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
End Sub
Private Sub mControl_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mControl.MouseLeave
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
mControl.Cursor = Cursors.Default
End Sub
End Class
I create a new instance of the class for each picturebox (I have just 2 here to keep it simple but have more) when the form loads but then I can't figure out how to change the aspect ratio boolean (mPreserveAspectRatio). I have tried just changing the boolean when the checkbox changes states but that doesn't work. And I think I may need to somehow create a public method to change the boolean but can't wrap my head around it. Here is the latest code I have been trying that doesn't work
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mControl As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox1, False)
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox2, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox2, False)
End If
End Sub
End Class
I figured it out. You pointed me in the right direction Jeff. I did need to add the control to the form. Here is what I did.
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Control1 As New ResizeableControl(PictureBox1, True)
Me.mControl1 = Control1
Dim Control2 As New ResizeableControl(PictureBox2, True)
Me.mControl2 = Control2
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Me.mControl1.mPreserveAspectRatio = True
Else
Me.mControl1.mPreserveAspectRatio = False
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Me.mControl2.mPreserveAspectRatio = True
Else
Me.mControl2.mPreserveAspectRatio = False
End If
End Sub
End Class

Form with custom title bar & formborderstyle = None remains draggable when maximized

I used this code to maximize and restore my custom form. But when the form is maximized, it still remains draggable, I use a timer to drag the form.
Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click, lblTitle.MouseDoubleClick
Dim maximizeHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
Dim maximizeWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
Dim maximizeLocation As Point = New Point(0, 0)
Dim fullscreen As Boolean = False
If Me.Height = maximizeHeight Or Me.Width = maximizeWidth Or Me.Location = maximizeLocation Then
fullscreen = True
Else
fullscreen = False
End If
If fullscreen = True Then
Me.Size = New Size(1000, 500)
Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
ElseIf fullscreen = False Then
Me.Location = New Point(0, 0)
Me.Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End If
End Sub
Private Sub pnlBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseDown
MoveTmr.Start()
refpositions()
End Sub
Private Sub MoveTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MoveTmr.Tick
Me.Location = oloc - ocur + System.Windows.Forms.Cursor.Position
refpositions()
End Sub
Private Sub pnlBar_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseUp
MoveTmr.Stop()
refpositions()
End Sub
Private Sub RszTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles RszTmr.Tick
Me.Size = appSize - curPos + Cursor.Position
refpositions()
End Sub
make:
Dim fullscreen As Boolean = False
a class variable.
Then edit this code to fit your variables:
Private Sub Mover_Tick(sender As System.Object, e As System.EventArgs) Handles Mover.Tick
If fullscreen = false Then
Dim pt As New Point((Me.Location.X + (MousePosition.X - mPosX)), (Me.Location.Y + (MousePosition.Y - mPosY)))
Me.Location = pt
mPosX = MousePosition.X
mPosY = MousePosition.Y
End If
End Sub
EDIT:
Also implement this:
Private Sub Title_StartDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseDown
mPosX = MousePosition.X
mPosY = MousePosition.Y
If e.Button = Windows.Forms.MouseButtons.Left Then
Mover.Start()
End If
End Sub
Private Sub Title_StopDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseUp
Mover.Stop()
End Sub
also you may want to make it much simpler by saying
Me.WindowState = FormWindowState.Maximized
I use the Mousedown, Mouseup and Mousemove events to move my forms.
Public Class Form1
Private Is_Dragged As Boolean = False
Private M_DownX As Integer
Private M_DownY As Integer
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If e.Button = MouseButtons.Left Then
Is_Dragged = True
M_DownX = e.X
M_DownY = e.Y
End If
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
If e.Button = MouseButtons.Left Then
Is_Dragged = False
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If Is_Dragged Then
Dim tmp_pnt As Point = New Point()
tmp_pnt.X = Me.Location.X + (e.X - M_DownX)
tmp_pnt.Y = Me.Location.Y + (e.Y - M_DownY)
Me.Location = tmp_pnt
tmp_pnt = Nothing
End If
End Sub
End Class
and to maximize my form.
Private Sub Btn_Main_Max_Click(sender As Object, e As EventArgs) Handles Btn_Main_Max.Click
Static IsAlreadyResized As Boolean
If Not IsAlreadyResized Then
Me.WindowState = FormWindowState.Maximized
IsAlreadyResized = True
Exit Sub
End If
If IsAlreadyResized Then
Me.WindowState = FormWindowState.Normal
IsAlreadyResized = False
End If
End Sub
You can create a panel in code or visually and place dock.top then you can put a label on the side or in the middle and a picturebox on the left side of the window create the buttons to close minimize and maximize or others and add the friend code above to move the form by the titl bar as in windows